[Tarantool-patches] [PATCH luajit 2/2] Fix io.close() error message.

Sergey Kaplun skaplun at tarantool.org
Wed May 18 11:58:17 MSK 2022


From: Mike Pall <mike>

Reported by François Perrad.

When `io.close()` is called without arguments on already closed default
output the error message is `LJ_ERR_IOSTDCL` ("standard file is closed")
instead of `LJ_ERR_IOCLFL` ("attempt to use a closed file").  It is
never a "real" standard file, because trying to close a standard throws
an error ("cannot close standard file"). Also, this is inconsistent with
PUC Lua.

This patch adds the corresponding check and code branch for this corner
case.

Sergey Kaplun:
* added the description and the test for the problem
---
 src/lib_io.c                                           | 10 ++++++++--
 .../lj-735-io-close-on-closed-file.test.lua            |  5 +++--
 2 files changed, 11 insertions(+), 4 deletions(-)

diff --git a/src/lib_io.c b/src/lib_io.c
index d9028938..f0108227 100644
--- a/src/lib_io.c
+++ b/src/lib_io.c
@@ -288,8 +288,14 @@ static int io_file_lines(lua_State *L)
 
 LJLIB_CF(io_method_close)
 {
-  IOFileUD *iof = L->base < L->top ? io_tofile(L) :
-		  io_stdfile(L, GCROOT_IO_OUTPUT);
+  IOFileUD *iof;
+  if (L->base < L->top) {
+    iof = io_tofile(L);
+  } else {
+    iof = IOSTDF_IOF(L, GCROOT_IO_OUTPUT);
+    if (iof->fp == NULL)
+      lj_err_caller(L, LJ_ERR_IOCLFL);
+  }
   return io_file_close(L, iof);
 }
 
diff --git a/test/tarantool-tests/lj-735-io-close-on-closed-file.test.lua b/test/tarantool-tests/lj-735-io-close-on-closed-file.test.lua
index 795dad6c..5e031c48 100644
--- a/test/tarantool-tests/lj-735-io-close-on-closed-file.test.lua
+++ b/test/tarantool-tests/lj-735-io-close-on-closed-file.test.lua
@@ -1,7 +1,7 @@
 local tap = require('tap')
 
 local test = tap.test('lj-735-io-close-on-closed-file')
-test:plan(1)
+test:plan(2)
 
 local TEST_FILE = 'lj-735-io-close-on-closed-file.tmp'
 
@@ -11,11 +11,12 @@ io.output(TEST_FILE)
 local status, err = io.close()
 assert(status, err)
 
-status = pcall(io.close)
+status, err = pcall(io.close)
 
 io.output(oldstdout)
 
 test:ok(not status, 'close already closed file')
+test:ok(err:match('attempt to use a closed file'), 'correct error message')
 
 assert(os.remove(TEST_FILE))
 
-- 
2.34.1



More information about the Tarantool-patches mailing list