diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1996-02-22 17:34:33 -0300 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1996-02-22 17:34:33 -0300 |
| commit | 8c1a9899d4460aa19780919f4245c08d7ebba0e9 (patch) | |
| tree | 685817907f64133330011eb7db845830c9327a3e /luamem.c | |
| parent | 05caf09a36cadaab401bc9a24e29e2cd6e4126d4 (diff) | |
| download | lua-8c1a9899d4460aa19780919f4245c08d7ebba0e9.tar.gz lua-8c1a9899d4460aa19780919f4245c08d7ebba0e9.tar.bz2 lua-8c1a9899d4460aa19780919f4245c08d7ebba0e9.zip | |
functions "luaI_free" and "luaI_realloc" (or macro "growvector") may be
called with NULL.
Diffstat (limited to 'luamem.c')
| -rw-r--r-- | luamem.c | 19 |
1 files changed, 8 insertions, 11 deletions
| @@ -3,7 +3,7 @@ | |||
| 3 | ** TecCGraf - PUC-Rio | 3 | ** TecCGraf - PUC-Rio |
| 4 | */ | 4 | */ |
| 5 | 5 | ||
| 6 | char *rcs_mem = "$Id: mem.c,v 1.6 1996/01/22 14:15:13 roberto Exp roberto $"; | 6 | char *rcs_mem = "$Id: mem.c,v 1.7 1996/02/04 16:59:12 roberto Exp roberto $"; |
| 7 | 7 | ||
| 8 | #include <stdlib.h> | 8 | #include <stdlib.h> |
| 9 | #include <string.h> | 9 | #include <string.h> |
| @@ -27,8 +27,11 @@ static void mem_error (void) | |||
| 27 | 27 | ||
| 28 | void luaI_free (void *block) | 28 | void luaI_free (void *block) |
| 29 | { | 29 | { |
| 30 | *((int *)block) = -1; /* to catch errors */ | 30 | if (block) |
| 31 | free(block); | 31 | { |
| 32 | *((int *)block) = -1; /* to catch errors */ | ||
| 33 | free(block); | ||
| 34 | } | ||
| 32 | } | 35 | } |
| 33 | 36 | ||
| 34 | 37 | ||
| @@ -43,16 +46,10 @@ void *luaI_malloc (unsigned long size) | |||
| 43 | 46 | ||
| 44 | void *luaI_realloc (void *oldblock, unsigned long size) | 47 | void *luaI_realloc (void *oldblock, unsigned long size) |
| 45 | { | 48 | { |
| 46 | void *block = realloc(oldblock, (size_t)size); | 49 | void *block = oldblock ? realloc(oldblock, (size_t)size) : |
| 50 | malloc((size_t)size); | ||
| 47 | if (block == NULL) | 51 | if (block == NULL) |
| 48 | mem_error(); | 52 | mem_error(); |
| 49 | return block; | 53 | return block; |
| 50 | } | 54 | } |
| 51 | 55 | ||
| 52 | |||
| 53 | char *luaI_strdup (char *str) | ||
| 54 | { | ||
| 55 | char *newstr = luaI_malloc(strlen(str)+1); | ||
| 56 | strcpy(newstr, str); | ||
| 57 | return newstr; | ||
| 58 | } | ||
