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 | |
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
-rw-r--r-- | bugs | 6 | ||||
-rw-r--r-- | liolib.c | 9 |
2 files changed, 12 insertions, 3 deletions
@@ -229,3 +229,9 @@ Wed Sep 27 13:39:45 EST 2000 | |||
229 | >> (e.g. «a = {print'foo'}») | 229 | >> (e.g. «a = {print'foo'}») |
230 | (by Edgar Toernig; since 4.0b, deriving from previous bug) | 230 | (by Edgar Toernig; since 4.0b, deriving from previous bug) |
231 | 231 | ||
232 | ** liolib.c | ||
233 | Thu Oct 26 10:50:46 EDT 2000 | ||
234 | >> in function `read_file', realloc() doesn't free the buffer if it can't | ||
235 | >> allocate new memory | ||
236 | (by Mauro Vezzosi; since 4.0b) | ||
237 | |||
@@ -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; |