summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authortdeval <>2001-12-05 22:54:01 +0000
committertdeval <>2001-12-05 22:54:01 +0000
commite0fe449a9f991163722343b2b40f45c42b13972b (patch)
treea2a6c5940e4b0f2c50fa90ffec6dd12daef7942c /src
parentb8e9b8d08ebac2bb596ff4717b4b79653d3acd48 (diff)
downloadopenbsd-e0fe449a9f991163722343b2b40f45c42b13972b.tar.gz
openbsd-e0fe449a9f991163722343b2b40f45c42b13972b.tar.bz2
openbsd-e0fe449a9f991163722343b2b40f45c42b13972b.zip
correct an alignment mis-conception for malloc(0) returned regions.
OK deraadt@
Diffstat (limited to 'src')
-rw-r--r--src/lib/libc/stdlib/malloc.c39
1 files changed, 25 insertions, 14 deletions
diff --git a/src/lib/libc/stdlib/malloc.c b/src/lib/libc/stdlib/malloc.c
index 7c98beed9d..2234bd64a8 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.44 2001/11/01 07:00:51 mickey Exp $"; 11static char rcsid[] = "$OpenBSD: malloc.c,v 1.45 2001/12/05 22:54:01 tdeval Exp $";
12#endif /* LIBC_SCCS and not lint */ 12#endif /* LIBC_SCCS and not lint */
13 13
14/* 14/*
@@ -726,19 +726,28 @@ malloc_make_chunks(bits)
726 } 726 }
727 } 727 }
728 728
729 bp->size = (1UL<<bits);
730 bp->shift = bits;
731 bp->total = bp->free = malloc_pagesize >> bits;
732 bp->page = pp;
733
734 /* memory protect the page allocated in the malloc(0) case */ 729 /* memory protect the page allocated in the malloc(0) case */
735 if (bits == 0) { 730 if (bits == 0) {
731
732 bp->size = 0;
733 bp->shift = 1;
734 i = malloc_minsize-1;
735 while (i >>= 1)
736 bp->shift++;
737 bp->total = bp->free = malloc_pagesize >> bp->shift;
738 bp->page = pp;
739
736 k = mprotect(pp, malloc_pagesize, PROT_NONE); 740 k = mprotect(pp, malloc_pagesize, PROT_NONE);
737 if (k < 0) { 741 if (k < 0) {
738 ifree(pp); 742 ifree(pp);
739 ifree(bp); 743 ifree(bp);
740 return 0; 744 return 0;
741 } 745 }
746 } else {
747 bp->size = (1UL<<bits);
748 bp->shift = bits;
749 bp->total = bp->free = malloc_pagesize >> bits;
750 bp->page = pp;
742 } 751 }
743 752
744 /* set all valid bits in the bitmap */ 753 /* set all valid bits in the bitmap */
@@ -831,7 +840,7 @@ malloc_bytes(size)
831 k += (lp-bp->bits)*MALLOC_BITS; 840 k += (lp-bp->bits)*MALLOC_BITS;
832 k <<= bp->shift; 841 k <<= bp->shift;
833 842
834 if (malloc_junk && bp->shift != 0) 843 if (malloc_junk && bp->size != 0)
835 memset((char *)bp->page + k, SOME_JUNK, bp->size); 844 memset((char *)bp->page + k, SOME_JUNK, bp->size);
836 845
837 return (u_char *)bp->page + k; 846 return (u_char *)bp->page + k;
@@ -924,7 +933,7 @@ irealloc(ptr, size)
924 } else if (*mp >= MALLOC_MAGIC) { /* Chunk allocation */ 933 } else if (*mp >= MALLOC_MAGIC) { /* Chunk allocation */
925 934
926 /* Check the pointer for sane values */ 935 /* Check the pointer for sane values */
927 if (((u_long)ptr & ((*mp)->size-1))) { 936 if ((u_long)ptr & ((1UL<<((*mp)->shift))-1)) {
928 wrtwarning("modified (chunk-) pointer.\n"); 937 wrtwarning("modified (chunk-) pointer.\n");
929 return 0; 938 return 0;
930 } 939 }
@@ -957,7 +966,7 @@ irealloc(ptr, size)
957 if (p) { 966 if (p) {
958 /* copy the lesser of the two sizes, and free the old one */ 967 /* copy the lesser of the two sizes, and free the old one */
959 /* Don't move from/to 0 sized region !!! */ 968 /* Don't move from/to 0 sized region !!! */
960 if (osize != 1 && size != 0) { 969 if (osize != 0 && size != 0) {
961 if (osize < size) 970 if (osize < size)
962 memcpy(p, ptr, osize); 971 memcpy(p, ptr, osize);
963 else 972 else
@@ -1121,7 +1130,7 @@ free_bytes(ptr, index, info)
1121 /* Find the chunk number on the page */ 1130 /* Find the chunk number on the page */
1122 i = ((u_long)ptr & malloc_pagemask) >> info->shift; 1131 i = ((u_long)ptr & malloc_pagemask) >> info->shift;
1123 1132
1124 if (((u_long)ptr & (info->size-1))) { 1133 if ((u_long)ptr & ((1UL<<(info->shift))-1)) {
1125 wrtwarning("modified (chunk-) pointer.\n"); 1134 wrtwarning("modified (chunk-) pointer.\n");
1126 return; 1135 return;
1127 } 1136 }
@@ -1131,19 +1140,21 @@ free_bytes(ptr, index, info)
1131 return; 1140 return;
1132 } 1141 }
1133 1142
1134 if (malloc_junk && info->shift != 0) 1143 if (malloc_junk && info->size != 0)
1135 memset(ptr, SOME_JUNK, info->size); 1144 memset(ptr, SOME_JUNK, info->size);
1136 1145
1137 info->bits[i/MALLOC_BITS] |= 1UL<<(i%MALLOC_BITS); 1146 info->bits[i/MALLOC_BITS] |= 1UL<<(i%MALLOC_BITS);
1138 info->free++; 1147 info->free++;
1139 1148
1140 mp = page_dir + info->shift; 1149 if (info->size != 0)
1150 mp = page_dir + info->shift;
1151 else
1152 mp = page_dir;
1141 1153
1142 if (info->free == 1) { 1154 if (info->free == 1) {
1143 1155
1144 /* Page became non-full */ 1156 /* Page became non-full */
1145 1157
1146 mp = page_dir + info->shift;
1147 /* Insert in address order */ 1158 /* Insert in address order */
1148 while (*mp && (*mp)->next && (*mp)->next->page < info->page) 1159 while (*mp && (*mp)->next && (*mp)->next->page < info->page)
1149 mp = &(*mp)->next; 1160 mp = &(*mp)->next;
@@ -1169,7 +1180,7 @@ free_bytes(ptr, index, info)
1169 page_dir[ptr2index(info->page)] = MALLOC_FIRST; 1180 page_dir[ptr2index(info->page)] = MALLOC_FIRST;
1170 1181
1171 /* If the page was mprotected, unprotect it before releasing it */ 1182 /* If the page was mprotected, unprotect it before releasing it */
1172 if (info->shift == 0) { 1183 if (info->size == 0) {
1173 mprotect(info->page, malloc_pagesize, PROT_READ|PROT_WRITE); 1184 mprotect(info->page, malloc_pagesize, PROT_READ|PROT_WRITE);
1174 /* Do we have to care if mprotect succeeds here ? */ 1185 /* Do we have to care if mprotect succeeds here ? */
1175 } 1186 }