From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from [87.239.111.99] (localhost [127.0.0.1]) by dev.tarantool.org (Postfix) with ESMTP id 262106ECE3; Wed, 13 Jul 2022 14:59:10 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 262106ECE3 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1657713550; bh=+YpqLa2iWM3jogKFFyACa0lferWYO6bwUpw+nBVe4RY=; h=In-Reply-To:Date:References:To:Subject:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc: From; b=wFJ29/0G/5xGIdtXvfx+5MSc2fwuvqN8ocFywVjGbXTDLb+1LV/zqq4Ma1SHZ4ujU 6ntE+zIjk5N//CAjoOpUpYK786EMQ8BiBvP09YvY8enpWT4vd54yhrs7uSdGN5q+Il UqYfYtGzJ2ExS1OwVvaQAQ07vdRiSsgrPpI7ps8k= Received: from smtp56.i.mail.ru (smtp56.i.mail.ru [217.69.128.36]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dev.tarantool.org (Postfix) with ESMTPS id A68346D97F for ; Wed, 13 Jul 2022 14:58:41 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org A68346D97F Received: by smtp56.i.mail.ru with esmtpa (envelope-from ) id 1oBb0r-000327-0U; Wed, 13 Jul 2022 14:58:41 +0300 Content-Type: text/plain; charset=us-ascii Mime-Version: 1.0 (Mac OS X Mail 16.0 \(3696.100.31\)) In-Reply-To: <52271fcc9139dbf02d98b384a50cc13d1a02abb6.1652863494.git.skaplun@tarantool.org> Date: Wed, 13 Jul 2022 14:58:40 +0300 Content-Transfer-Encoding: quoted-printable Message-Id: References: <52271fcc9139dbf02d98b384a50cc13d1a02abb6.1652863494.git.skaplun@tarantool.org> To: Sergey Kaplun X-Mailer: Apple Mail (2.3696.100.31) X-Mailru-Src: smtp X-7564579A: 646B95376F6C166E X-77F55803: 4F1203BC0FB41BD9BCA415BD41350256CA927C7DF12EC75E9AC4F7747704ED77182A05F538085040618DE37ED8CA9D7BBE92BCD176770B9EE4BA564BC5858EE4C2D95583D0506CCB X-8FC586DF: 6EFBBC1D9D64D975 X-C1DE0DAB: 9604B64F49C60606AD91A466A1DEF99B296C473AB1E142185AC9E3593CE4B31AB1881A6453793CE9274300E5CE05BD4401A9E91200F654B0BC42BABAB7E101F7EEDD2DF4914E7A12BE4EA4B6CE4DA036E6C37550E7DD1CA59C2B6934AE262D3EE7EAB7254005DCED8DA55E71E02F9FC08E8E86DC7131B365E7726E8460B7C23C X-C8649E89: 4E36BF7865823D7055A7F0CF078B5EC49A30900B95165D34197948450CB5442AF76032B73F0B31D137937D16C6EB8EBF6591F06A2B9011195D94D21DEE6BF9F91D7E09C32AA3244CD1AA1320465F6870317C123D06DD6187408A6A02710B7304FACE5A9C96DEB163 X-D57D3AED: 3ZO7eAau8CL7WIMRKs4sN3D3tLDjz0dLbV79QFUyzQ2Ujvy7cMT6pYYqY16iZVKkSc3dCLJ7zSJH7+u4VD18S7Vl4ZUrpaVfd2+vE6kuoey4m4VkSEu530nj6fImhcD4MUrOEAnl0W826KZ9Q+tr5ycPtXkTV4k65bRjmOUUP8cvGozZ33TWg5HZplvhhXbhDGzqmQDTd6OAevLeAnq3Ra9uf7zvY2zzsIhlcp/Y7m53TZgf2aB4JOg4gkr2biojpV6+aF9js3/TSWsOUYsLjg== X-Mailru-Sender: 11C2EC085EDE56FA38FD4C59F7EFE407A9D92B8691196768CE9BD56F4E55053C1D978292A6D09B1019381EE24192DF5555834048F03EF5D4C9A814A92B2E3B1BA4250FC3964EA4964198E0F3ECE9B5443453F38A29522196 X-Mras: OK Subject: Re: [Tarantool-patches] [PATCH luajit 1/2] Fix io.close(). X-BeenThere: tarantool-patches@dev.tarantool.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: Tarantool development patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , From: sergos via Tarantool-patches Reply-To: sergos Cc: tarantool-patches@dev.tarantool.org Errors-To: tarantool-patches-bounces@dev.tarantool.org Sender: "Tarantool-patches" Hi! Thanks for the patch! Just some minor nits. LGTM Sergos > On 18 May 2022, at 11:58, Sergey Kaplun wrote: >=20 > From: Mike Pall >=20 > Reported by farmboy0. >=20 > When `io.close()` is called without arguments on already closed = default ^ ^ an an > output `iof->fp` is already NULL. So, the forward call to `fclose()` ^ ^ ^ the underlying following(?) the > leads to SEGFAULT. ^ a >=20 > This patch adds the corresponding check by using `io_stdfile()` = instead ^ of the > `IOSTDF_IOF()`. ^ of the >=20 > Also, this patch refactors several internal functions by changing > the argument type from `FILE *` to `IOFileUD *`. ^ ^ the the >=20 > 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 >=20 > 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; > } >=20 > -static FILE *io_stdfile(lua_State *L, ptrdiff_t id) > +static IOFileUD *io_stdfile(lua_State *L, ptrdiff_t id) > { > IOFileUD *iof =3D IOSTDF_IOF(L, id); > if (iof->fp =3D=3D NULL) > lj_err_caller(L, LJ_ERR_IOSTDCL); > - return iof->fp; > + return iof; > } >=20 > static IOFileUD *io_file_new(lua_State *L) > @@ -189,8 +189,9 @@ static int io_file_readlen(lua_State *L, FILE *fp, = MSize m) > } > } >=20 > -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 =3D iof->fp; > int ok, n, nargs =3D (int)(L->top - L->base) - start; > clearerr(fp); > if (nargs =3D=3D 0) { > @@ -226,8 +227,9 @@ static int io_file_read(lua_State *L, FILE *fp, = int start) > return n - start; > } >=20 > -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 =3D iof->fp; > cTValue *tv; > int status =3D 1; > for (tv =3D 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 +=3D n; > } > - n =3D io_file_read(L, iof->fp, 0); > + n =3D 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 =3D 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); > } >=20 > LJLIB_CF(io_method_read) > { > - return io_file_read(L, io_tofile(L)->fp, 1); > + return io_file_read(L, io_tofile(L), 1); > } >=20 > 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); > } >=20 > 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) >=20 > LJLIB_CF(io_flush) LJLIB_REC(io_flush GCROOT_IO_OUTPUT) > { > - return luaL_fileresult(L, fflush(io_stdfile(L, GCROOT_IO_OUTPUT)) = =3D=3D 0, NULL); > + return luaL_fileresult(L, fflush(io_stdfile(L, = GCROOT_IO_OUTPUT)->fp) =3D=3D 0, NULL); > } >=20 > 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 =3D require('tap') > + > +local test =3D tap.test('lj-735-io-close-on-closed-file') > +test:plan(1) > + > +local TEST_FILE =3D 'lj-735-io-close-on-closed-file.tmp' > + > +local oldstdout =3D io.output() > +io.output(TEST_FILE) > + > +local status, err =3D io.close() > +assert(status, err) > + > +status =3D pcall(io.close) > + > +io.output(oldstdout) Was not clear that you keep an old one for the test output, comment? > + > +test:ok(not status, 'close already closed file') > + > +assert(os.remove(TEST_FILE)) > + > +os.exit(test:check() and 0 or 1) > --=20 > 2.34.1 >=20