From: Mike Pall Date: Mon, 19 Aug 2024 15:31:15 +0000 (+0200) Subject: Fix another potential file descriptor leak in luaL_loadfile*(). X-Git-Url: http://git.ipfire.org/gitweb/gitweb.cgi?a=commitdiff_plain;h=ab39082fddfca0de268a106a3b6d736eef032328;p=thirdparty%2FLuaJIT.git Fix another potential file descriptor leak in luaL_loadfile*(). Reported by Peter Cawley. #1249 --- diff --git a/src/lj_load.c b/src/lj_load.c index d92bd1b4..1524aeb6 100644 --- a/src/lj_load.c +++ b/src/lj_load.c @@ -87,6 +87,7 @@ LUALIB_API int luaL_loadfilex(lua_State *L, const char *filename, FileReaderCtx ctx; int status; const char *chunkname; + int err = 0; if (filename) { chunkname = lua_pushfstring(L, "@%s", filename); ctx.fp = fopen(filename, "rb"); @@ -100,17 +101,16 @@ LUALIB_API int luaL_loadfilex(lua_State *L, const char *filename, chunkname = "=stdin"; } status = lua_loadx(L, reader_file, &ctx, chunkname, mode); - if (ferror(ctx.fp)) { - L->top -= filename ? 2 : 1; - lua_pushfstring(L, "cannot read %s: %s", chunkname+1, strerror(errno)); - if (filename) - fclose(ctx.fp); - return LUA_ERRFILE; - } + if (ferror(ctx.fp)) err = errno; if (filename) { + fclose(ctx.fp); L->top--; copyTV(L, L->top-1, L->top); - fclose(ctx.fp); + } + if (err) { + L->top--; + lua_pushfstring(L, "cannot read %s: %s", chunkname+1, strerror(err)); + return LUA_ERRFILE; } return status; }