Skip to content

Commit

Permalink
Add proper max malloc and errno handling to std malloc/realloc
Browse files Browse the repository at this point in the history
Allow malloc libc test to run since max alloc is rejected properly
  • Loading branch information
ghaerr committed Nov 20, 2024
1 parent 2effc02 commit a0bc4eb
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
6 changes: 1 addition & 5 deletions elkscmd/test/libc/malloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,14 @@ TEST_CASE(malloc_malloc_free) {
free(p);
}

/* TODO:BUG: causes hang at 100% CPU */
#if 0
errno = 0;
p = malloc((size_t) - 1);
p = malloc((size_t)-1);
if (p == NULL) {
EXPECT_EQ(errno, ENOMEM);
} else {
EXPECT_EQ(errno, 0);
memset(p, 0xff, (size_t) - 1);
free(p);
}
#endif

/* strange sizes are fine; memory is writable and free-able */
for (int i = 1; i < 1024; i += 123) {
Expand Down
12 changes: 11 additions & 1 deletion libc/malloc/malloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

#include <malloc.h>
#include <unistd.h>
#include <errno.h>

#include "_malloc.h"

Expand Down Expand Up @@ -190,8 +191,16 @@ malloc(size_t size)
register mem __wcnear *ptr = 0;
register unsigned int sz;

errno = 0;
if (size == 0)
return 0; /* ANSI STD */
return 0; /* ANSI STD, no error */

/* Minor oops here, sbrk has a signed argument */
if((int)size < 0 || size > (((unsigned)-1) >> 1) - sizeof(mem) * 3)
{
errno = ENOMEM;
return 0;
}

sz = size + sizeof(mem) * 2 - 1;
sz /= sizeof(mem);
Expand Down Expand Up @@ -281,6 +290,7 @@ malloc(size_t size)
#ifndef MCHUNK
ptr = __mini_malloc(size);
#endif
if( ptr == 0) errno = ENOMEM;
#ifdef VERBOSE
if( ptr == 0 )
__noise("MALLOC FAIL", 0);
Expand Down

0 comments on commit a0bc4eb

Please sign in to comment.