summaryrefslogtreecommitdiff
path: root/liolib.c
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2014-11-21 10:17:33 -0200
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2014-11-21 10:17:33 -0200
commit1735c05ac75797976e98720cd72be318b91b4aa3 (patch)
treeadf59ab766e3cf8661921ac2f9fd4d10b0661a3c /liolib.c
parent5fbd40dbe5d6d3f7bd4717073eb2beacc8ebe7c9 (diff)
downloadlua-github-1735c05ac75797976e98720cd72be318b91b4aa3.tar.gz
avoid memory errors while a file is locked (when reading a line)
Diffstat (limited to 'liolib.c')
-rw-r--r--liolib.c23
1 files changed, 16 insertions, 7 deletions
diff --git a/liolib.c b/liolib.c
index 85d42677..e6d2df2c 100644
--- a/liolib.c
+++ b/liolib.c
@@ -1,5 +1,5 @@
/*
-** $Id: liolib.c,v 2.139 2014/11/02 19:19:04 roberto Exp roberto $
+** $Id: liolib.c,v 2.140 2014/11/02 19:33:33 roberto Exp roberto $
** Standard I/O (and system) library
** See Copyright Notice in lua.h
*/
@@ -466,12 +466,21 @@ static int read_line (lua_State *L, FILE *f, int chop) {
luaL_Buffer b;
int c;
luaL_buffinit(L, &b);
- l_lockfile(f);
- while ((c = l_getc(f)) != EOF && c != '\n')
- luaL_addchar(&b, c);
- l_unlockfile(f);
- if (!chop && c == '\n') luaL_addchar(&b, c);
+ for (;;) {
+ char *buff = luaL_prepbuffer(&b); /* pre-allocate buffer */
+ int i = 0;
+ l_lockfile(f); /* no memory errors can happen inside the lock */
+ while (i < LUAL_BUFFERSIZE && (c = l_getc(f)) != EOF && c != '\n')
+ buff[i++] = c;
+ l_unlockfile(f);
+ luaL_addsize(&b, i);
+ if (i < LUAL_BUFFERSIZE)
+ break;
+ }
+ if (!chop && c == '\n') /* want a newline and have one? */
+ luaL_addchar(&b, c); /* add ending newline to result */
luaL_pushresult(&b); /* close buffer */
+ /* return ok if read something (either a newline or something else) */
return (c == '\n' || lua_rawlen(L, -1) > 0);
}
@@ -516,7 +525,7 @@ static int g_read (lua_State *L, FILE *f, int first) {
success = 1;
for (n = first; nargs-- && success; n++) {
if (lua_type(L, n) == LUA_TNUMBER) {
- size_t l = (size_t)lua_tointeger(L, n);
+ size_t l = (size_t)luaL_checkinteger(L, n);
success = (l == 0) ? test_eof(L, f) : read_chars(L, f, l);
}
else {