aboutsummaryrefslogtreecommitdiff
path: root/liolib.c
diff options
context:
space:
mode:
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 @@
1/* 1/*
2** $Id: liolib.c,v 2.139 2014/11/02 19:19:04 roberto Exp roberto $ 2** $Id: liolib.c,v 2.140 2014/11/02 19:33:33 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*/
@@ -466,12 +466,21 @@ static int read_line (lua_State *L, FILE *f, int chop) {
466 luaL_Buffer b; 466 luaL_Buffer b;
467 int c; 467 int c;
468 luaL_buffinit(L, &b); 468 luaL_buffinit(L, &b);
469 l_lockfile(f); 469 for (;;) {
470 while ((c = l_getc(f)) != EOF && c != '\n') 470 char *buff = luaL_prepbuffer(&b); /* pre-allocate buffer */
471 luaL_addchar(&b, c); 471 int i = 0;
472 l_unlockfile(f); 472 l_lockfile(f); /* no memory errors can happen inside the lock */
473 if (!chop && c == '\n') luaL_addchar(&b, c); 473 while (i < LUAL_BUFFERSIZE && (c = l_getc(f)) != EOF && c != '\n')
474 buff[i++] = c;
475 l_unlockfile(f);
476 luaL_addsize(&b, i);
477 if (i < LUAL_BUFFERSIZE)
478 break;
479 }
480 if (!chop && c == '\n') /* want a newline and have one? */
481 luaL_addchar(&b, c); /* add ending newline to result */
474 luaL_pushresult(&b); /* close buffer */ 482 luaL_pushresult(&b); /* close buffer */
483 /* return ok if read something (either a newline or something else) */
475 return (c == '\n' || lua_rawlen(L, -1) > 0); 484 return (c == '\n' || lua_rawlen(L, -1) > 0);
476} 485}
477 486
@@ -516,7 +525,7 @@ static int g_read (lua_State *L, FILE *f, int first) {
516 success = 1; 525 success = 1;
517 for (n = first; nargs-- && success; n++) { 526 for (n = first; nargs-- && success; n++) {
518 if (lua_type(L, n) == LUA_TNUMBER) { 527 if (lua_type(L, n) == LUA_TNUMBER) {
519 size_t l = (size_t)lua_tointeger(L, n); 528 size_t l = (size_t)luaL_checkinteger(L, n);
520 success = (l == 0) ? test_eof(L, f) : read_chars(L, f, l); 529 success = (l == 0) ? test_eof(L, f) : read_chars(L, f, l);
521 } 530 }
522 else { 531 else {