From c315150aff4496eb50f4b9a4d9da9f7869710d36 Mon Sep 17 00:00:00 2001 From: Philipp Janda Date: Sun, 14 Jun 2015 22:44:46 +0200 Subject: Fix detection of closed files on Lua 5.2/5.3. Lua 5.2 changed the protocol on how to represent closed files: Lua 5.1 sets the file pointer to NULL, Lua 5.2 sets the close function pointer to NULL (a NULL file pointer now signals an incompletely constructed object). Also `luaL_checkudata` never returns NULL, it raises an error instead if you have an invalid value (no userdata/not correct userdata type). --- src/lfs.c | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/lfs.c b/src/lfs.c index 020267d..8b4cfd2 100644 --- a/src/lfs.c +++ b/src/lfs.c @@ -197,15 +197,23 @@ static int get_dir (lua_State *L) { ** Check if the given element on the stack is a file and returns it. */ static FILE *check_file (lua_State *L, int idx, const char *funcname) { +#if LUA_VERSION_NUM == 501 FILE **fh = (FILE **)luaL_checkudata (L, idx, "FILE*"); - if (fh == NULL) { - luaL_error (L, "%s: not a file", funcname); - return 0; - } else if (*fh == NULL) { + if (*fh == NULL) { luaL_error (L, "%s: closed file", funcname); return 0; } else return *fh; +#elif LUA_VERSION_NUM >= 502 && LUA_VERSION_NUM <= 503 + luaL_Stream *fh = (luaL_Stream *)luaL_checkudata (L, idx, "FILE*"); + if (fh->closef == 0 || fh->f == NULL) { + luaL_error (L, "%s: closed file", funcname); + return 0; + } else + return fh->f; +#else +#error unsupported Lua version +#endif } -- cgit v1.2.3-55-g6feb