Merge pull request #15 from davidgiven/dtrg-allocs
Fix various memory allocation bugs.
This commit is contained in:
commit
3b46aadf94
23
lang/cem/libcc.ansi/malloc/calloc.c
Normal file
23
lang/cem/libcc.ansi/malloc/calloc.c
Normal file
|
@ -0,0 +1,23 @@
|
|||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <unistd.h>
|
||||
|
||||
void* calloc(size_t nmemb, size_t size)
|
||||
{
|
||||
size_t bytes = nmemb * size;
|
||||
void* ptr;
|
||||
|
||||
/* Test for overflow.
|
||||
* See http://stackoverflow.com/questions/1815367/multiplication-of-large-numbers-how-to-catch-overflow
|
||||
*/
|
||||
|
||||
if ((nmemb == 0) || (size == 0) || (nmemb > (SIZE_MAX / size)))
|
||||
return NULL;
|
||||
|
||||
ptr = malloc(bytes);
|
||||
if (!ptr)
|
||||
return NULL;
|
||||
|
||||
memset(ptr, 0, bytes);
|
||||
return ptr;
|
||||
}
|
|
@ -37,7 +37,7 @@ extern char** environ;
|
|||
|
||||
extern void _exit(int);
|
||||
extern pid_t getpid(void);
|
||||
extern void* sbrk(intptr_t increment);
|
||||
extern void* sbrk(int increment);
|
||||
extern int isatty(int d);
|
||||
extern off_t lseek(int fildes, off_t offset, int whence);
|
||||
extern int close(int d);
|
||||
|
|
|
@ -28,7 +28,7 @@ int brk(void* newend)
|
|||
return 0;
|
||||
}
|
||||
|
||||
void* sbrk(intptr_t increment)
|
||||
void* sbrk(int increment)
|
||||
{
|
||||
char* old;
|
||||
|
||||
|
|
|
@ -1,17 +0,0 @@
|
|||
/* $Source: /cvsroot/tack/Ack/plat/linux386/libsys/brk.c,v $
|
||||
* $State: Exp $
|
||||
* $Revision: 1.1 $
|
||||
*/
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <errno.h>
|
||||
#include "libsys.h"
|
||||
|
||||
int brk(void* end)
|
||||
{
|
||||
int e = _syscall(__NR_brk, (quad) end, 0, 0);
|
||||
if (e == -1)
|
||||
errno = ENOMEM;
|
||||
return e;
|
||||
}
|
|
@ -12,7 +12,17 @@
|
|||
|
||||
static char* current = NULL;
|
||||
|
||||
void* sbrk(intptr_t increment)
|
||||
int brk(void* end)
|
||||
{
|
||||
int e = _syscall(__NR_brk, (quad) end, 0, 0);
|
||||
if (e == -1)
|
||||
errno = ENOMEM;
|
||||
else
|
||||
current = end;
|
||||
return e;
|
||||
}
|
||||
|
||||
void* sbrk(int increment)
|
||||
{
|
||||
char* old;
|
||||
char* new;
|
||||
|
|
|
@ -67,7 +67,7 @@ extern char** environ;
|
|||
extern void _exit(int);
|
||||
extern pid_t getpid(void);
|
||||
extern int brk(void* ptr);
|
||||
extern void* sbrk(intptr_t increment);
|
||||
extern void* sbrk(int increment);
|
||||
extern int isatty(int d);
|
||||
|
||||
/* Signal handling */
|
||||
|
|
|
@ -65,7 +65,7 @@ extern char** environ;
|
|||
extern void _exit(int);
|
||||
extern pid_t getpid(void);
|
||||
extern int brk(void* ptr);
|
||||
extern void* sbrk(intptr_t increment);
|
||||
extern void* sbrk(int increment);
|
||||
extern int isatty(int d);
|
||||
|
||||
/* Signal handling */
|
||||
|
|
|
@ -65,7 +65,7 @@ extern char** environ;
|
|||
extern void _exit(int);
|
||||
extern pid_t getpid(void);
|
||||
extern int brk(void* ptr);
|
||||
extern void* sbrk(intptr_t increment);
|
||||
extern void* sbrk(int increment);
|
||||
extern int isatty(int d);
|
||||
|
||||
/* Signal handling */
|
||||
|
|
|
@ -37,7 +37,7 @@ extern char** environ;
|
|||
|
||||
extern void _exit(int);
|
||||
extern pid_t getpid(void);
|
||||
extern void* sbrk(intptr_t increment);
|
||||
extern void* sbrk(int increment);
|
||||
extern int isatty(int d);
|
||||
extern off_t lseek(int fildes, off_t offset, int whence);
|
||||
extern int close(int d);
|
||||
|
|
|
@ -28,7 +28,7 @@ int brk(void* newend)
|
|||
return 0;
|
||||
}
|
||||
|
||||
void* sbrk(intptr_t increment)
|
||||
void* sbrk(int increment)
|
||||
{
|
||||
char* old;
|
||||
|
||||
|
|
|
@ -65,7 +65,7 @@ extern char** environ;
|
|||
extern void _exit(int);
|
||||
extern pid_t getpid(void);
|
||||
extern int brk(void* ptr);
|
||||
extern void* sbrk(intptr_t increment);
|
||||
extern void* sbrk(int increment);
|
||||
extern int isatty(int d);
|
||||
|
||||
/* Signal handling */
|
||||
|
|
|
@ -28,7 +28,7 @@ int brk(void* newend)
|
|||
return 0;
|
||||
}
|
||||
|
||||
void* sbrk(intptr_t increment)
|
||||
void* sbrk(int increment)
|
||||
{
|
||||
char* old;
|
||||
|
||||
|
|
28
plat/qemuppc/tests/brk_c.c
Normal file
28
plat/qemuppc/tests/brk_c.c
Normal file
|
@ -0,0 +1,28 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <errno.h>
|
||||
#include <limits.h>
|
||||
#include "test.h"
|
||||
|
||||
int main(int argc, const char* argv[])
|
||||
{
|
||||
void* p;
|
||||
|
||||
ASSERT(-1 == (intptr_t)brk((void*)0xffffffff));
|
||||
ASSERT(ENOMEM == errno);
|
||||
|
||||
p = sbrk(0);
|
||||
ASSERT(p == sbrk(0));
|
||||
ASSERT(p == sbrk(8));
|
||||
ASSERT(p != sbrk(0));
|
||||
ASSERT(p != sbrk(-8));
|
||||
ASSERT(p == sbrk(0));
|
||||
|
||||
/* We assume the test environment has less than 2GB of RAM. */
|
||||
ASSERT(-1 == (intptr_t)sbrk(INT_MAX));
|
||||
ASSERT(-1 == (intptr_t)sbrk(INT_MIN));
|
||||
|
||||
finished();
|
||||
}
|
||||
|
23
plat/qemuppc/tests/calloc_c.c
Normal file
23
plat/qemuppc/tests/calloc_c.c
Normal file
|
@ -0,0 +1,23 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include "test.h"
|
||||
|
||||
int main(int argc, const char* argv[])
|
||||
{
|
||||
const char* p;
|
||||
int i;
|
||||
|
||||
ASSERT(0 == calloc(0, 0));
|
||||
ASSERT(0 == calloc(0, 1));
|
||||
ASSERT(0 == calloc(1, 0));
|
||||
ASSERT(0 == calloc(SIZE_MAX/2, 3));
|
||||
ASSERT(0 == calloc(SIZE_MAX/2, 2));
|
||||
ASSERT(0 != calloc(1, 1));
|
||||
|
||||
p = calloc(10, 1);
|
||||
for (i=0; i<10; i++)
|
||||
ASSERT(0 == p[i]);
|
||||
|
||||
finished();
|
||||
}
|
|
@ -59,7 +59,7 @@ extern char** environ;
|
|||
|
||||
extern void _exit(int);
|
||||
extern pid_t getpid(void);
|
||||
extern void* sbrk(intptr_t increment);
|
||||
extern void* sbrk(int increment);
|
||||
extern int isatty(int d);
|
||||
extern off_t lseek(int fildes, off_t offset, int whence);
|
||||
extern int close(int d);
|
||||
|
|
|
@ -30,7 +30,7 @@ int brk(void* newend)
|
|||
return 0;
|
||||
}
|
||||
|
||||
void* sbrk(intptr_t increment)
|
||||
void* sbrk(int increment)
|
||||
{
|
||||
char* old;
|
||||
|
||||
|
|
Loading…
Reference in a new issue