diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2000-10-26 10:53:55 -0200 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2000-10-26 10:53:55 -0200 |
commit | 89f98c099576591f8f65b9526d0f24de6dec95e8 (patch) | |
tree | 153ee308d5f8240e1299802dedb5a9c93c35b6a7 /liolib.c | |
parent | b892f0a8774f573d7ec9b02617428871b8d3a2b3 (diff) | |
download | lua-89f98c099576591f8f65b9526d0f24de6dec95e8.tar.gz lua-89f98c099576591f8f65b9526d0f24de6dec95e8.tar.bz2 lua-89f98c099576591f8f65b9526d0f24de6dec95e8.zip |
in function `read_file', realloc() doesn't free the buffer if it can't
allocate new memory
Diffstat (limited to 'liolib.c')
-rw-r--r-- | liolib.c | 9 |
1 files changed, 6 insertions, 3 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: liolib.c,v 1.87 2000/10/20 16:39:03 roberto Exp roberto $ | 2 | ** $Id: liolib.c,v 1.88 2000/10/26 12:47:05 roberto Exp roberto $ |
3 | ** Standard I/O (and system) library | 3 | ** Standard I/O (and system) library |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -345,9 +345,12 @@ static void read_file (lua_State *L, FILE *f) { | |||
345 | size_t size = BUFSIZ; | 345 | size_t size = BUFSIZ; |
346 | char *buffer = NULL; | 346 | char *buffer = NULL; |
347 | for (;;) { | 347 | for (;;) { |
348 | buffer = (char *)realloc(buffer, size); | 348 | char *newbuffer = (char *)realloc(buffer, size); |
349 | if (buffer == NULL) | 349 | if (newbuffer == NULL) { |
350 | free(buffer); | ||
350 | lua_error(L, "not enough memory to read a file"); | 351 | lua_error(L, "not enough memory to read a file"); |
352 | } | ||
353 | buffer = newbuffer; | ||
351 | len += fread(buffer+len, sizeof(char), size-len, f); | 354 | len += fread(buffer+len, sizeof(char), size-len, f); |
352 | if (len < size) break; /* did not read all it could */ | 355 | if (len < size) break; /* did not read all it could */ |
353 | size *= 2; | 356 | size *= 2; |