diff options
| author | Mike Pall <mike> | 2021-07-22 02:33:04 +0200 |
|---|---|---|
| committer | Mike Pall <mike> | 2021-07-22 02:33:04 +0200 |
| commit | 8430f774e6478b587664ac4b7d4eb0b46678eff5 (patch) | |
| tree | 02b860df140dd6f00ccbe92911bcd959f6251704 | |
| parent | 81a797373fa5364f7640b7f1668abada56a45d6b (diff) | |
| download | luajit-8430f774e6478b587664ac4b7d4eb0b46678eff5.tar.gz luajit-8430f774e6478b587664ac4b7d4eb0b46678eff5.tar.bz2 luajit-8430f774e6478b587664ac4b7d4eb0b46678eff5.zip | |
Fix io.close().
Reported by farmboy0.
| -rw-r--r-- | src/lib_io.c | 20 |
1 files changed, 11 insertions, 9 deletions
diff --git a/src/lib_io.c b/src/lib_io.c index 389152c7..76d3ace1 100644 --- a/src/lib_io.c +++ b/src/lib_io.c | |||
| @@ -58,12 +58,12 @@ static IOFileUD *io_tofile(lua_State *L) | |||
| 58 | return iof; | 58 | return iof; |
| 59 | } | 59 | } |
| 60 | 60 | ||
| 61 | static FILE *io_stdfile(lua_State *L, ptrdiff_t id) | 61 | static IOFileUD *io_stdfile(lua_State *L, ptrdiff_t id) |
| 62 | { | 62 | { |
| 63 | IOFileUD *iof = IOSTDF_IOF(L, id); | 63 | IOFileUD *iof = IOSTDF_IOF(L, id); |
| 64 | if (iof->fp == NULL) | 64 | if (iof->fp == NULL) |
| 65 | lj_err_caller(L, LJ_ERR_IOSTDCL); | 65 | lj_err_caller(L, LJ_ERR_IOSTDCL); |
| 66 | return iof->fp; | 66 | return iof; |
| 67 | } | 67 | } |
| 68 | 68 | ||
| 69 | static IOFileUD *io_file_new(lua_State *L) | 69 | static IOFileUD *io_file_new(lua_State *L) |
| @@ -187,8 +187,9 @@ static int io_file_readlen(lua_State *L, FILE *fp, MSize m) | |||
| 187 | } | 187 | } |
| 188 | } | 188 | } |
| 189 | 189 | ||
| 190 | static int io_file_read(lua_State *L, FILE *fp, int start) | 190 | static int io_file_read(lua_State *L, IOFileUD *iof, int start) |
| 191 | { | 191 | { |
| 192 | FILE *fp = iof->fp; | ||
| 192 | int ok, n, nargs = (int)(L->top - L->base) - start; | 193 | int ok, n, nargs = (int)(L->top - L->base) - start; |
| 193 | clearerr(fp); | 194 | clearerr(fp); |
| 194 | if (nargs == 0) { | 195 | if (nargs == 0) { |
| @@ -225,8 +226,9 @@ static int io_file_read(lua_State *L, FILE *fp, int start) | |||
| 225 | return n - start; | 226 | return n - start; |
| 226 | } | 227 | } |
| 227 | 228 | ||
| 228 | static int io_file_write(lua_State *L, FILE *fp, int start) | 229 | static int io_file_write(lua_State *L, IOFileUD *iof, int start) |
| 229 | { | 230 | { |
| 231 | FILE *fp = iof->fp; | ||
| 230 | cTValue *tv; | 232 | cTValue *tv; |
| 231 | int status = 1; | 233 | int status = 1; |
| 232 | for (tv = L->base+start; tv < L->top; tv++) { | 234 | for (tv = L->base+start; tv < L->top; tv++) { |
| @@ -266,7 +268,7 @@ static int io_file_iter(lua_State *L) | |||
| 266 | memcpy(L->top, &fn->c.upvalue[1], n*sizeof(TValue)); | 268 | memcpy(L->top, &fn->c.upvalue[1], n*sizeof(TValue)); |
| 267 | L->top += n; | 269 | L->top += n; |
| 268 | } | 270 | } |
| 269 | n = io_file_read(L, iof->fp, 0); | 271 | n = io_file_read(L, iof, 0); |
| 270 | if (ferror(iof->fp)) | 272 | if (ferror(iof->fp)) |
| 271 | lj_err_callermsg(L, strVdata(L->top-2)); | 273 | lj_err_callermsg(L, strVdata(L->top-2)); |
| 272 | if (tvisnil(L->base) && (iof->type & IOFILE_FLAG_CLOSE)) { | 274 | if (tvisnil(L->base) && (iof->type & IOFILE_FLAG_CLOSE)) { |
| @@ -292,18 +294,18 @@ static int io_file_lines(lua_State *L) | |||
| 292 | LJLIB_CF(io_method_close) | 294 | LJLIB_CF(io_method_close) |
| 293 | { | 295 | { |
| 294 | IOFileUD *iof = L->base < L->top ? io_tofile(L) : | 296 | IOFileUD *iof = L->base < L->top ? io_tofile(L) : |
| 295 | IOSTDF_IOF(L, GCROOT_IO_OUTPUT); | 297 | io_stdfile(L, GCROOT_IO_OUTPUT); |
| 296 | return io_file_close(L, iof); | 298 | return io_file_close(L, iof); |
| 297 | } | 299 | } |
| 298 | 300 | ||
| 299 | LJLIB_CF(io_method_read) | 301 | LJLIB_CF(io_method_read) |
| 300 | { | 302 | { |
| 301 | return io_file_read(L, io_tofile(L)->fp, 1); | 303 | return io_file_read(L, io_tofile(L), 1); |
| 302 | } | 304 | } |
| 303 | 305 | ||
| 304 | LJLIB_CF(io_method_write) LJLIB_REC(io_write 0) | 306 | LJLIB_CF(io_method_write) LJLIB_REC(io_write 0) |
| 305 | { | 307 | { |
| 306 | return io_file_write(L, io_tofile(L)->fp, 1); | 308 | return io_file_write(L, io_tofile(L), 1); |
| 307 | } | 309 | } |
| 308 | 310 | ||
| 309 | LJLIB_CF(io_method_flush) LJLIB_REC(io_flush 0) | 311 | LJLIB_CF(io_method_flush) LJLIB_REC(io_flush 0) |
| @@ -457,7 +459,7 @@ LJLIB_CF(io_write) LJLIB_REC(io_write GCROOT_IO_OUTPUT) | |||
| 457 | 459 | ||
| 458 | LJLIB_CF(io_flush) LJLIB_REC(io_flush GCROOT_IO_OUTPUT) | 460 | LJLIB_CF(io_flush) LJLIB_REC(io_flush GCROOT_IO_OUTPUT) |
| 459 | { | 461 | { |
| 460 | return luaL_fileresult(L, fflush(io_stdfile(L, GCROOT_IO_OUTPUT)) == 0, NULL); | 462 | return luaL_fileresult(L, fflush(io_stdfile(L, GCROOT_IO_OUTPUT)->fp) == 0, NULL); |
| 461 | } | 463 | } |
| 462 | 464 | ||
| 463 | static int io_std_getset(lua_State *L, ptrdiff_t id, const char *mode) | 465 | static int io_std_getset(lua_State *L, ptrdiff_t id, const char *mode) |
