aboutsummaryrefslogtreecommitdiff
path: root/loslib.c
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2024-06-12 15:50:31 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2024-06-12 15:50:31 -0300
commitbb7bb5944c9b3c868c6ab9cbe7d11b611251066b (patch)
treefe7f5e8def79d5a3c253a2f39c4557fc20d204e6 /loslib.c
parent94b503d95ef00f1e38b58b024ef45bf8973a8746 (diff)
downloadlua-bb7bb5944c9b3c868c6ab9cbe7d11b611251066b.tar.gz
lua-bb7bb5944c9b3c868c6ab9cbe7d11b611251066b.tar.bz2
lua-bb7bb5944c9b3c868c6ab9cbe7d11b611251066b.zip
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.
Diffstat (limited to 'loslib.c')
-rw-r--r--loslib.c2
1 files changed, 2 insertions, 0 deletions
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) {
155 155
156static int os_remove (lua_State *L) { 156static int os_remove (lua_State *L) {
157 const char *filename = luaL_checkstring(L, 1); 157 const char *filename = luaL_checkstring(L, 1);
158 errno = 0;
158 return luaL_fileresult(L, remove(filename) == 0, filename); 159 return luaL_fileresult(L, remove(filename) == 0, filename);
159} 160}
160 161
@@ -162,6 +163,7 @@ static int os_remove (lua_State *L) {
162static int os_rename (lua_State *L) { 163static int os_rename (lua_State *L) {
163 const char *fromname = luaL_checkstring(L, 1); 164 const char *fromname = luaL_checkstring(L, 1);
164 const char *toname = luaL_checkstring(L, 2); 165 const char *toname = luaL_checkstring(L, 2);
166 errno = 0;
165 return luaL_fileresult(L, rename(fromname, toname) == 0, NULL); 167 return luaL_fileresult(L, rename(fromname, toname) == 0, NULL);
166} 168}
167 169