summaryrefslogtreecommitdiff
path: root/src/lib
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib')
-rw-r--r--src/lib/libc/stdlib/malloc.c32
1 files changed, 31 insertions, 1 deletions
diff --git a/src/lib/libc/stdlib/malloc.c b/src/lib/libc/stdlib/malloc.c
index ae89f5d72b..9f7ceba080 100644
--- a/src/lib/libc/stdlib/malloc.c
+++ b/src/lib/libc/stdlib/malloc.c
@@ -8,7 +8,7 @@
8 */ 8 */
9 9
10#if defined(LIBC_SCCS) && !defined(lint) 10#if defined(LIBC_SCCS) && !defined(lint)
11static char rcsid[] = "$OpenBSD: malloc.c,v 1.72 2005/03/31 21:24:46 tdeval Exp $"; 11static char rcsid[] = "$OpenBSD: malloc.c,v 1.73 2005/05/24 16:39:05 tedu Exp $";
12#endif /* LIBC_SCCS and not lint */ 12#endif /* LIBC_SCCS and not lint */
13 13
14/* 14/*
@@ -1061,6 +1061,13 @@ malloc_bytes(size_t size)
1061} 1061}
1062 1062
1063/* 1063/*
1064 * magic so that malloc(sizeof(ptr)) is near the end of the page.
1065 */
1066#define PTR_GAP (malloc_pagesize - sizeof(void *))
1067#define PTR_SIZE (sizeof(void *))
1068#define PTR_ALIGNED(p) (((unsigned long)p & malloc_pagemask) == PTR_GAP)
1069
1070/*
1064 * Allocate a piece of memory 1071 * Allocate a piece of memory
1065 */ 1072 */
1066static void * 1073static void *
@@ -1075,6 +1082,11 @@ imalloc(size_t size)
1075 if (suicide) 1082 if (suicide)
1076 abort(); 1083 abort();
1077 1084
1085 if (malloc_guard && size == PTR_SIZE) {
1086 ptralloc = 1;
1087 size = malloc_pagesize;
1088 }
1089
1078 if ((size + malloc_pagesize) < size) { /* Check for overflow */ 1090 if ((size + malloc_pagesize) < size) { /* Check for overflow */
1079 result = NULL; 1091 result = NULL;
1080 errno = ENOMEM; 1092 errno = ENOMEM;
@@ -1090,6 +1102,8 @@ imalloc(size_t size)
1090 if (malloc_zero && result != NULL) 1102 if (malloc_zero && result != NULL)
1091 memset(result, 0, size); 1103 memset(result, 0, size);
1092 1104
1105 if (result && ptralloc)
1106 return ((char *)result + PTR_GAP);
1093 return (result); 1107 return (result);
1094} 1108}
1095 1109
@@ -1114,6 +1128,19 @@ irealloc(void *ptr, size_t size)
1114 return (NULL); 1128 return (NULL);
1115 } 1129 }
1116 1130
1131 if (malloc_guard && PTR_ALIGNED(ptr)) {
1132 if (size <= PTR_SIZE)
1133 return (ptr);
1134 else {
1135 p = imalloc(size);
1136 if (p)
1137 memcpy(p, ptr, PTR_SIZE);
1138 ifree(ptr);
1139 return (p);
1140 }
1141 }
1142
1143
1117 index = ptr2index(ptr); 1144 index = ptr2index(ptr);
1118 1145
1119 if (index < malloc_pageshift) { 1146 if (index < malloc_pageshift) {
@@ -1575,6 +1602,9 @@ ifree(void *ptr)
1575 if (suicide) 1602 if (suicide)
1576 return; 1603 return;
1577 1604
1605 if (malloc_guard && PTR_ALIGNED(ptr))
1606 ptr = (char *)ptr - PTR_GAP;
1607
1578 index = ptr2index(ptr); 1608 index = ptr2index(ptr);
1579 1609
1580 if (index < malloc_pageshift) { 1610 if (index < malloc_pageshift) {