From bb7bb5944c9b3c868c6ab9cbe7d11b611251066b Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 12 Jun 2024 15:50:31 -0300 Subject: More disciplined use of 'errno' Set errno to zero before calling any function where we may use its errno, and check errno for zero before using it (as functions may not set it even in error). The code assumes that no function will put garbage on errno (although ISO C allows that): If any function during an operation set errno, and the operation result in an error, assume that errno has something to say. --- loslib.c | 2 ++ 1 file changed, 2 insertions(+) (limited to 'loslib.c') diff --git a/loslib.c b/loslib.c index ad5a9276..ba80d72c 100644 --- a/loslib.c +++ b/loslib.c @@ -155,6 +155,7 @@ static int os_execute (lua_State *L) { static int os_remove (lua_State *L) { const char *filename = luaL_checkstring(L, 1); + errno = 0; return luaL_fileresult(L, remove(filename) == 0, filename); } @@ -162,6 +163,7 @@ static int os_remove (lua_State *L) { static int os_rename (lua_State *L) { const char *fromname = luaL_checkstring(L, 1); const char *toname = luaL_checkstring(L, 2); + errno = 0; return luaL_fileresult(L, rename(fromname, toname) == 0, NULL); } -- cgit v1.2.3-55-g6feb