diff options
| -rw-r--r-- | liolib.c | 14 |
1 files changed, 10 insertions, 4 deletions
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: liolib.c,v 2.91 2010/07/28 15:51:59 roberto Exp roberto $ | 2 | ** $Id: liolib.c,v 2.92 2010/10/25 19:01:37 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 | */ |
| @@ -19,6 +19,9 @@ | |||
| 19 | #include "lualib.h" | 19 | #include "lualib.h" |
| 20 | 20 | ||
| 21 | 21 | ||
| 22 | #define MAX_SIZE_T (~(size_t)0) | ||
| 23 | |||
| 24 | |||
| 22 | /* | 25 | /* |
| 23 | ** lua_popen spawns a new process connected to the current one through | 26 | ** lua_popen spawns a new process connected to the current one through |
| 24 | ** the file streams. | 27 | ** the file streams. |
| @@ -377,10 +380,13 @@ static int read_chars (lua_State *L, FILE *f, size_t n) { | |||
| 377 | size_t nr; /* number of chars actually read in each cycle */ | 380 | size_t nr; /* number of chars actually read in each cycle */ |
| 378 | luaL_Buffer b; | 381 | luaL_Buffer b; |
| 379 | luaL_buffinit(L, &b); | 382 | luaL_buffinit(L, &b); |
| 380 | rlen = LUAL_BUFFERSIZE; /* try to read that much each time */ | 383 | rlen = LUAL_BUFFERSIZE / 2; /* to be doubled at 1st iteration */ |
| 381 | do { | 384 | do { |
| 382 | char *p = luaL_prepbuffer(&b); | 385 | char *p; |
| 386 | if (rlen < (MAX_SIZE_T / 2)) | ||
| 387 | rlen *= 2; /* double buffer size at each iteration */ | ||
| 383 | if (rlen > tbr) rlen = tbr; /* cannot read more than asked */ | 388 | if (rlen > tbr) rlen = tbr; /* cannot read more than asked */ |
| 389 | p = luaL_prepbuffsize(&b, rlen); | ||
| 384 | nr = fread(p, sizeof(char), rlen, f); | 390 | nr = fread(p, sizeof(char), rlen, f); |
| 385 | luaL_addsize(&b, nr); | 391 | luaL_addsize(&b, nr); |
| 386 | tbr -= nr; /* still have to read 'tbr' chars */ | 392 | tbr -= nr; /* still have to read 'tbr' chars */ |
| @@ -421,7 +427,7 @@ static int g_read (lua_State *L, FILE *f, int first) { | |||
| 421 | success = read_line(L, f, 0); | 427 | success = read_line(L, f, 0); |
| 422 | break; | 428 | break; |
| 423 | case 'a': /* file */ | 429 | case 'a': /* file */ |
| 424 | read_chars(L, f, ~((size_t)0)); /* read MAX_SIZE_T chars */ | 430 | read_chars(L, f, MAX_SIZE_T); /* read MAX_SIZE_T chars */ |
| 425 | success = 1; /* always success */ | 431 | success = 1; /* always success */ |
| 426 | break; | 432 | break; |
| 427 | default: | 433 | default: |
