Tarantool development patches archive
 help / color / mirror / Atom feed
From: Sergey Kaplun via Tarantool-patches <tarantool-patches@dev.tarantool.org>
To: Sergey Ostanevich <sergos@tarantool.org>,
	Igor Munkin <imun@tarantool.org>
Cc: tarantool-patches@dev.tarantool.org
Subject: [Tarantool-patches] [PATCH luajit 1/2] Fix io.close().
Date: Wed, 18 May 2022 11:58:16 +0300	[thread overview]
Message-ID: <52271fcc9139dbf02d98b384a50cc13d1a02abb6.1652863494.git.skaplun@tarantool.org> (raw)
In-Reply-To: <cover.1652863494.git.skaplun@tarantool.org>

From: Mike Pall <mike>

Reported by farmboy0.

When `io.close()` is called without arguments on already closed default
output `iof->fp` is already NULL. So, the forward call to `fclose()`
leads to SEGFAULT.

This patch adds the corresponding check by using `io_stdfile()` instead
`IOSTDF_IOF()`.

Also, this patch refactors several internal functions by changing
the argument type from `FILE *` to `IOFileUD *`.

Sergey Kaplun:
* added the description and the test for the problem
---
 src/lib_io.c                                  | 20 +++++++++--------
 .../lj-735-io-close-on-closed-file.test.lua   | 22 +++++++++++++++++++
 2 files changed, 33 insertions(+), 9 deletions(-)
 create mode 100644 test/tarantool-tests/lj-735-io-close-on-closed-file.test.lua

diff --git a/src/lib_io.c b/src/lib_io.c
index 9763ed46..d9028938 100644
--- a/src/lib_io.c
+++ b/src/lib_io.c
@@ -60,12 +60,12 @@ static IOFileUD *io_tofile(lua_State *L)
   return iof;
 }
 
-static FILE *io_stdfile(lua_State *L, ptrdiff_t id)
+static IOFileUD *io_stdfile(lua_State *L, ptrdiff_t id)
 {
   IOFileUD *iof = IOSTDF_IOF(L, id);
   if (iof->fp == NULL)
     lj_err_caller(L, LJ_ERR_IOSTDCL);
-  return iof->fp;
+  return iof;
 }
 
 static IOFileUD *io_file_new(lua_State *L)
@@ -189,8 +189,9 @@ static int io_file_readlen(lua_State *L, FILE *fp, MSize m)
   }
 }
 
-static int io_file_read(lua_State *L, FILE *fp, int start)
+static int io_file_read(lua_State *L, IOFileUD *iof, int start)
 {
+  FILE *fp = iof->fp;
   int ok, n, nargs = (int)(L->top - L->base) - start;
   clearerr(fp);
   if (nargs == 0) {
@@ -226,8 +227,9 @@ static int io_file_read(lua_State *L, FILE *fp, int start)
   return n - start;
 }
 
-static int io_file_write(lua_State *L, FILE *fp, int start)
+static int io_file_write(lua_State *L, IOFileUD *iof, int start)
 {
+  FILE *fp = iof->fp;
   cTValue *tv;
   int status = 1;
   for (tv = L->base+start; tv < L->top; tv++) {
@@ -261,7 +263,7 @@ static int io_file_iter(lua_State *L)
     memcpy(L->top, &fn->c.upvalue[1], n*sizeof(TValue));
     L->top += n;
   }
-  n = io_file_read(L, iof->fp, 0);
+  n = io_file_read(L, iof, 0);
   if (ferror(iof->fp))
     lj_err_callermsg(L, strVdata(L->top-2));
   if (tvisnil(L->base) && (iof->type & IOFILE_FLAG_CLOSE)) {
@@ -287,18 +289,18 @@ static int io_file_lines(lua_State *L)
 LJLIB_CF(io_method_close)
 {
   IOFileUD *iof = L->base < L->top ? io_tofile(L) :
-		  IOSTDF_IOF(L, GCROOT_IO_OUTPUT);
+		  io_stdfile(L, GCROOT_IO_OUTPUT);
   return io_file_close(L, iof);
 }
 
 LJLIB_CF(io_method_read)
 {
-  return io_file_read(L, io_tofile(L)->fp, 1);
+  return io_file_read(L, io_tofile(L), 1);
 }
 
 LJLIB_CF(io_method_write)		LJLIB_REC(io_write 0)
 {
-  return io_file_write(L, io_tofile(L)->fp, 1);
+  return io_file_write(L, io_tofile(L), 1);
 }
 
 LJLIB_CF(io_method_flush)		LJLIB_REC(io_flush 0)
@@ -452,7 +454,7 @@ LJLIB_CF(io_write)		LJLIB_REC(io_write GCROOT_IO_OUTPUT)
 
 LJLIB_CF(io_flush)		LJLIB_REC(io_flush GCROOT_IO_OUTPUT)
 {
-  return luaL_fileresult(L, fflush(io_stdfile(L, GCROOT_IO_OUTPUT)) == 0, NULL);
+  return luaL_fileresult(L, fflush(io_stdfile(L, GCROOT_IO_OUTPUT)->fp) == 0, NULL);
 }
 
 static int io_std_getset(lua_State *L, ptrdiff_t id, const char *mode)
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
new file mode 100644
index 00000000..795dad6c
--- /dev/null
+++ b/test/tarantool-tests/lj-735-io-close-on-closed-file.test.lua
@@ -0,0 +1,22 @@
+local tap = require('tap')
+
+local test = tap.test('lj-735-io-close-on-closed-file')
+test:plan(1)
+
+local TEST_FILE = 'lj-735-io-close-on-closed-file.tmp'
+
+local oldstdout = io.output()
+io.output(TEST_FILE)
+
+local status, err = io.close()
+assert(status, err)
+
+status = pcall(io.close)
+
+io.output(oldstdout)
+
+test:ok(not status, 'close already closed file')
+
+assert(os.remove(TEST_FILE))
+
+os.exit(test:check() and 0 or 1)
-- 
2.34.1


  reply	other threads:[~2022-05-18  9:01 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-05-18  8:58 [Tarantool-patches] [PATCH luajit 0/2] " Sergey Kaplun via Tarantool-patches
2022-05-18  8:58 ` Sergey Kaplun via Tarantool-patches [this message]
2022-07-13 11:58   ` [Tarantool-patches] [PATCH luajit 1/2] " sergos via Tarantool-patches
2022-07-14  8:04     ` Sergey Kaplun via Tarantool-patches
2022-05-18  8:58 ` [Tarantool-patches] [PATCH luajit 2/2] Fix io.close() error message Sergey Kaplun via Tarantool-patches
2022-07-13 11:58   ` sergos via Tarantool-patches
2022-07-14  7:54     ` Sergey Kaplun via Tarantool-patches
2022-07-13 11:58 ` [Tarantool-patches] [PATCH luajit 0/2] Fix io.close() sergos via Tarantool-patches
2022-07-13 17:45   ` Sergey Kaplun via Tarantool-patches
2022-11-23  7:50 ` Igor Munkin via Tarantool-patches

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=52271fcc9139dbf02d98b384a50cc13d1a02abb6.1652863494.git.skaplun@tarantool.org \
    --to=tarantool-patches@dev.tarantool.org \
    --cc=imun@tarantool.org \
    --cc=sergos@tarantool.org \
    --cc=skaplun@tarantool.org \
    --subject='Re: [Tarantool-patches] [PATCH luajit 1/2] Fix io.close().' \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox