-
Notifications
You must be signed in to change notification settings - Fork 116
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1767 from ccoffing/malloc-fixes
Malloc fixes
- Loading branch information
Showing
3 changed files
with
112 additions
and
93 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,29 @@ | ||
#include <errno.h> | ||
#include <malloc.h> | ||
#include <string.h> | ||
|
||
void * | ||
calloc(unsigned int elm, unsigned int sz) | ||
{ | ||
register unsigned int v; | ||
register void *ptr; | ||
unsigned int v; | ||
void *ptr; | ||
|
||
ptr = malloc(v = elm * sz); | ||
if(ptr) | ||
memset(ptr, 0, v); | ||
#ifdef __GNUC__ | ||
if (__builtin_umul_overflow(elm, sz, &v)) { | ||
errno = ENOMEM; | ||
return 0; | ||
} | ||
#else | ||
v = elm * sz; | ||
if (sz != 0 && v / sz != elm) { | ||
errno = ENOMEM; | ||
return 0; | ||
} | ||
#endif | ||
|
||
return ptr; | ||
ptr = malloc(v); | ||
if (ptr) | ||
memset(ptr, 0, v); | ||
|
||
return ptr; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,40 @@ | ||
#if defined(VERBOSE) | ||
# include <string.h> | ||
# include <unistd.h> | ||
#include <errno.h> | ||
#include <string.h> | ||
#include <unistd.h> | ||
|
||
# include "_malloc.h" | ||
#include "_malloc.h" | ||
|
||
/* NB: Careful here, stdio may use malloc - so we can't */ | ||
static void | ||
phex(int val) | ||
{ | ||
static char hex[] = "0123456789ABCDEF"; | ||
int i; | ||
static char hex[] = "0123456789ABCDEF"; | ||
int i; | ||
|
||
for (i = sizeof(int) * 8 - 4; i >= 0; i -= 4) | ||
write(2, hex + ((val >> i) & 0xF), 1); | ||
for (i = sizeof(int) * 8 - 4; i >= 0; i -= 4) | ||
write(2, hex + ((val >> i) & 0xF), 1); | ||
} | ||
|
||
void | ||
__noise(char *y, mem *x) | ||
__noise(char *y, mem * x) | ||
{ | ||
write(2, "Malloc ", 7); | ||
phex((int)x); | ||
write(2, " sz ", 4); | ||
if(x) | ||
phex(m_size(x)); | ||
else | ||
phex(0); | ||
write(2, " nxt ", 5); | ||
if (x) | ||
phex((int)m_next(x)); | ||
else | ||
phex(0); | ||
write(2, " is ", 4); | ||
write(2, y, strlen(y)); | ||
write(2, "\n", 1); | ||
int saved_errno = errno; | ||
write(2, "Malloc ", 7); | ||
phex((int)x); | ||
write(2, " sz ", 4); | ||
if (x) | ||
phex(m_size(x)); | ||
else | ||
phex(0); | ||
write(2, " nxt ", 5); | ||
if (x) | ||
phex((int)m_next(x)); | ||
else | ||
phex(0); | ||
write(2, " is ", 4); | ||
write(2, y, strlen(y)); | ||
write(2, "\n", 1); | ||
errno = saved_errno; | ||
} | ||
#endif |