summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorotto <>2008-10-03 19:01:12 +0000
committerotto <>2008-10-03 19:01:12 +0000
commit30341d9d6b901701cfd466001d77c9467cbfe59c (patch)
treeb0983461469c55d06368c65e361b9fa060e6aaad /src
parent896595db3f4cfbe5a7b82a14a042df3281f544e7 (diff)
downloadopenbsd-30341d9d6b901701cfd466001d77c9467cbfe59c.tar.gz
openbsd-30341d9d6b901701cfd466001d77c9467cbfe59c.tar.bz2
openbsd-30341d9d6b901701cfd466001d77c9467cbfe59c.zip
save and restore errno on success. while it is not stricly needed for
non-syscalls, there's just too much code not doing the right thing on error paths; prompted by and ok deraadt@
Diffstat (limited to 'src')
-rw-r--r--src/lib/libc/stdlib/malloc.c18
1 files changed, 14 insertions, 4 deletions
diff --git a/src/lib/libc/stdlib/malloc.c b/src/lib/libc/stdlib/malloc.c
index a205253fd4..f1768cfe65 100644
--- a/src/lib/libc/stdlib/malloc.c
+++ b/src/lib/libc/stdlib/malloc.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: malloc.c,v 1.100 2008/10/03 18:44:29 otto Exp $ */ 1/* $OpenBSD: malloc.c,v 1.101 2008/10/03 19:01:12 otto Exp $ */
2/* 2/*
3 * Copyright (c) 2008 Otto Moerbeek <otto@drijf.net> 3 * Copyright (c) 2008 Otto Moerbeek <otto@drijf.net>
4 * 4 *
@@ -547,7 +547,7 @@ static int
547omalloc_init(struct dir_info *d) 547omalloc_init(struct dir_info *d)
548{ 548{
549 char *p, b[64]; 549 char *p, b[64];
550 int i, j, save_errno = errno; 550 int i, j;
551 size_t regioninfo_size; 551 size_t regioninfo_size;
552 552
553 rbytes_init(); 553 rbytes_init();
@@ -675,8 +675,6 @@ omalloc_init(struct dir_info *d)
675 " Will not be able to dump malloc stats on exit"); 675 " Will not be able to dump malloc stats on exit");
676#endif /* MALLOC_STATS */ 676#endif /* MALLOC_STATS */
677 677
678 errno = save_errno;
679
680 d->regions_bits = 9; 678 d->regions_bits = 9;
681 d->regions_free = d->regions_total = 1 << d->regions_bits; 679 d->regions_free = d->regions_total = 1 << d->regions_bits;
682 regioninfo_size = d->regions_total * sizeof(struct region_info); 680 regioninfo_size = d->regions_total * sizeof(struct region_info);
@@ -1157,6 +1155,7 @@ void *
1157malloc(size_t size) 1155malloc(size_t size)
1158{ 1156{
1159 void *r; 1157 void *r;
1158 int saved_errno = errno;
1160 1159
1161 _MALLOC_LOCK(); 1160 _MALLOC_LOCK();
1162 malloc_func = " in malloc():"; 1161 malloc_func = " in malloc():";
@@ -1180,6 +1179,8 @@ malloc(size_t size)
1180 wrterror("out of memory"); 1179 wrterror("out of memory");
1181 errno = ENOMEM; 1180 errno = ENOMEM;
1182 } 1181 }
1182 if (r != NULL)
1183 saved_errno = errno;
1183 return r; 1184 return r;
1184} 1185}
1185 1186
@@ -1248,6 +1249,8 @@ ofree(void *p)
1248void 1249void
1249free(void *ptr) 1250free(void *ptr)
1250{ 1251{
1252 int saved_errno = errno;
1253
1251 /* This is legal. */ 1254 /* This is legal. */
1252 if (ptr == NULL) 1255 if (ptr == NULL)
1253 return; 1256 return;
@@ -1261,6 +1264,7 @@ free(void *ptr)
1261 ofree(ptr); 1264 ofree(ptr);
1262 malloc_active--; 1265 malloc_active--;
1263 _MALLOC_UNLOCK(); 1266 _MALLOC_UNLOCK();
1267 errno = saved_errno;
1264} 1268}
1265 1269
1266 1270
@@ -1355,6 +1359,7 @@ void *
1355realloc(void *ptr, size_t size) 1359realloc(void *ptr, size_t size)
1356{ 1360{
1357 void *r; 1361 void *r;
1362 int saved_errno = errno;
1358 1363
1359 _MALLOC_LOCK(); 1364 _MALLOC_LOCK();
1360 malloc_func = " in realloc():"; 1365 malloc_func = " in realloc():";
@@ -1380,6 +1385,8 @@ realloc(void *ptr, size_t size)
1380 wrterror("out of memory"); 1385 wrterror("out of memory");
1381 errno = ENOMEM; 1386 errno = ENOMEM;
1382 } 1387 }
1388 if (r != NULL)
1389 errno = saved_errno;
1383 return r; 1390 return r;
1384} 1391}
1385 1392
@@ -1390,6 +1397,7 @@ void *
1390calloc(size_t nmemb, size_t size) 1397calloc(size_t nmemb, size_t size)
1391{ 1398{
1392 void *r; 1399 void *r;
1400 int saved_errno = errno;
1393 1401
1394 _MALLOC_LOCK(); 1402 _MALLOC_LOCK();
1395 malloc_func = " in calloc():"; 1403 malloc_func = " in calloc():";
@@ -1425,5 +1433,7 @@ calloc(size_t nmemb, size_t size)
1425 wrterror("out of memory"); 1433 wrterror("out of memory");
1426 errno = ENOMEM; 1434 errno = ENOMEM;
1427 } 1435 }
1436 if (r != NULL)
1437 errno = saved_errno;
1428 return r; 1438 return r;
1429} 1439}