Merge pull request #15 from davidgiven/dtrg-allocs

Fix various memory allocation bugs.
This commit is contained in:
David Given 2016-11-23 22:35:50 +01:00 committed by GitHub
commit 3b46aadf94
16 changed files with 96 additions and 29 deletions

View 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;
}

View file

@ -37,7 +37,7 @@ extern char** environ;
extern void _exit(int); extern void _exit(int);
extern pid_t getpid(void); extern pid_t getpid(void);
extern void* sbrk(intptr_t increment); extern void* sbrk(int increment);
extern int isatty(int d); extern int isatty(int d);
extern off_t lseek(int fildes, off_t offset, int whence); extern off_t lseek(int fildes, off_t offset, int whence);
extern int close(int d); extern int close(int d);

View file

@ -28,7 +28,7 @@ int brk(void* newend)
return 0; return 0;
} }
void* sbrk(intptr_t increment) void* sbrk(int increment)
{ {
char* old; char* old;

View file

@ -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;
}

View file

@ -12,7 +12,17 @@
static char* current = NULL; 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* old;
char* new; char* new;

View file

@ -67,7 +67,7 @@ extern char** environ;
extern void _exit(int); extern void _exit(int);
extern pid_t getpid(void); extern pid_t getpid(void);
extern int brk(void* ptr); extern int brk(void* ptr);
extern void* sbrk(intptr_t increment); extern void* sbrk(int increment);
extern int isatty(int d); extern int isatty(int d);
/* Signal handling */ /* Signal handling */

View file

@ -65,7 +65,7 @@ extern char** environ;
extern void _exit(int); extern void _exit(int);
extern pid_t getpid(void); extern pid_t getpid(void);
extern int brk(void* ptr); extern int brk(void* ptr);
extern void* sbrk(intptr_t increment); extern void* sbrk(int increment);
extern int isatty(int d); extern int isatty(int d);
/* Signal handling */ /* Signal handling */

View file

@ -65,7 +65,7 @@ extern char** environ;
extern void _exit(int); extern void _exit(int);
extern pid_t getpid(void); extern pid_t getpid(void);
extern int brk(void* ptr); extern int brk(void* ptr);
extern void* sbrk(intptr_t increment); extern void* sbrk(int increment);
extern int isatty(int d); extern int isatty(int d);
/* Signal handling */ /* Signal handling */

View file

@ -37,7 +37,7 @@ extern char** environ;
extern void _exit(int); extern void _exit(int);
extern pid_t getpid(void); extern pid_t getpid(void);
extern void* sbrk(intptr_t increment); extern void* sbrk(int increment);
extern int isatty(int d); extern int isatty(int d);
extern off_t lseek(int fildes, off_t offset, int whence); extern off_t lseek(int fildes, off_t offset, int whence);
extern int close(int d); extern int close(int d);

View file

@ -28,7 +28,7 @@ int brk(void* newend)
return 0; return 0;
} }
void* sbrk(intptr_t increment) void* sbrk(int increment)
{ {
char* old; char* old;

View file

@ -65,7 +65,7 @@ extern char** environ;
extern void _exit(int); extern void _exit(int);
extern pid_t getpid(void); extern pid_t getpid(void);
extern int brk(void* ptr); extern int brk(void* ptr);
extern void* sbrk(intptr_t increment); extern void* sbrk(int increment);
extern int isatty(int d); extern int isatty(int d);
/* Signal handling */ /* Signal handling */

View file

@ -28,7 +28,7 @@ int brk(void* newend)
return 0; return 0;
} }
void* sbrk(intptr_t increment) void* sbrk(int increment)
{ {
char* old; char* old;

View 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();
}

View 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();
}

View file

@ -59,7 +59,7 @@ extern char** environ;
extern void _exit(int); extern void _exit(int);
extern pid_t getpid(void); extern pid_t getpid(void);
extern void* sbrk(intptr_t increment); extern void* sbrk(int increment);
extern int isatty(int d); extern int isatty(int d);
extern off_t lseek(int fildes, off_t offset, int whence); extern off_t lseek(int fildes, off_t offset, int whence);
extern int close(int d); extern int close(int d);

View file

@ -30,7 +30,7 @@ int brk(void* newend)
return 0; return 0;
} }
void* sbrk(intptr_t increment) void* sbrk(int increment)
{ {
char* old; char* old;