in function `read_file', realloc() doesn't free the buffer if it can't

allocate new memory
This commit is contained in:
Roberto Ierusalimschy 2000-10-26 10:53:55 -02:00
parent b892f0a877
commit 89f98c0995
2 changed files with 12 additions and 3 deletions

6
bugs
View File

@ -229,3 +229,9 @@ Wed Sep 27 13:39:45 EST 2000
>> (e.g. «a = {print'foo'}») >> (e.g. «a = {print'foo'}»)
(by Edgar Toernig; since 4.0b, deriving from previous bug) (by Edgar Toernig; since 4.0b, deriving from previous bug)
** liolib.c
Thu Oct 26 10:50:46 EDT 2000
>> in function `read_file', realloc() doesn't free the buffer if it can't
>> allocate new memory
(by Mauro Vezzosi; since 4.0b)

View File

@ -1,5 +1,5 @@
/* /*
** $Id: liolib.c,v 1.87 2000/10/20 16:39:03 roberto Exp roberto $ ** $Id: liolib.c,v 1.88 2000/10/26 12:47:05 roberto Exp roberto $
** Standard I/O (and system) library ** Standard I/O (and system) library
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -345,9 +345,12 @@ static void read_file (lua_State *L, FILE *f) {
size_t size = BUFSIZ; size_t size = BUFSIZ;
char *buffer = NULL; char *buffer = NULL;
for (;;) { for (;;) {
buffer = (char *)realloc(buffer, size); char *newbuffer = (char *)realloc(buffer, size);
if (buffer == NULL) if (newbuffer == NULL) {
free(buffer);
lua_error(L, "not enough memory to read a file"); lua_error(L, "not enough memory to read a file");
}
buffer = newbuffer;
len += fread(buffer+len, sizeof(char), size-len, f); len += fread(buffer+len, sizeof(char), size-len, f);
if (len < size) break; /* did not read all it could */ if (len < size) break; /* did not read all it could */
size *= 2; size *= 2;