aboutsummaryrefslogtreecommitdiff
path: root/lauxlib.c
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2024-06-04 12:48:29 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2024-06-04 12:48:29 -0300
commitd5212c13b081ed62d8e1ae436779e79c79edf564 (patch)
tree936ac196173318f51bec43077a2006ef48a88813 /lauxlib.c
parente0efebdbe4e4053c6fb78588c546f1dc23aa964a (diff)
downloadlua-d5212c13b081ed62d8e1ae436779e79c79edf564.tar.gz
lua-d5212c13b081ed62d8e1ae436779e79c79edf564.tar.bz2
lua-d5212c13b081ed62d8e1ae436779e79c79edf564.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).
Diffstat (limited to 'lauxlib.c')
-rw-r--r--lauxlib.c18
1 files changed, 13 insertions, 5 deletions
diff --git a/lauxlib.c b/lauxlib.c
index 1c9082e6..28bff274 100644
--- a/lauxlib.c
+++ b/lauxlib.c
@@ -249,11 +249,13 @@ LUALIB_API int luaL_fileresult (lua_State *L, int stat, const char *fname) {
249 return 1; 249 return 1;
250 } 250 }
251 else { 251 else {
252 const char *msg;
252 luaL_pushfail(L); 253 luaL_pushfail(L);
254 msg = (en != 0) ? strerror(en) : "(no extra info)";
253 if (fname) 255 if (fname)
254 lua_pushfstring(L, "%s: %s", fname, strerror(en)); 256 lua_pushfstring(L, "%s: %s", fname, msg);
255 else 257 else
256 lua_pushstring(L, strerror(en)); 258 lua_pushstring(L, msg);
257 lua_pushinteger(L, en); 259 lua_pushinteger(L, en);
258 return 3; 260 return 3;
259 } 261 }
@@ -732,9 +734,12 @@ static const char *getF (lua_State *L, void *ud, size_t *size) {
732 734
733 735
734static int errfile (lua_State *L, const char *what, int fnameindex) { 736static int errfile (lua_State *L, const char *what, int fnameindex) {
735 const char *serr = strerror(errno); 737 int err = errno;
736 const char *filename = lua_tostring(L, fnameindex) + 1; 738 const char *filename = lua_tostring(L, fnameindex) + 1;
737 lua_pushfstring(L, "cannot %s %s: %s", what, filename, serr); 739 if (err != 0)
740 lua_pushfstring(L, "cannot %s %s: %s", what, filename, strerror(err));
741 else
742 lua_pushfstring(L, "cannot %s %s", what, filename);
738 lua_remove(L, fnameindex); 743 lua_remove(L, fnameindex);
739 return LUA_ERRFILE; 744 return LUA_ERRFILE;
740} 745}
@@ -787,6 +792,7 @@ LUALIB_API int luaL_loadfilex (lua_State *L, const char *filename,
787 } 792 }
788 else { 793 else {
789 lua_pushfstring(L, "@%s", filename); 794 lua_pushfstring(L, "@%s", filename);
795 errno = 0;
790 lf.f = fopen(filename, "r"); 796 lf.f = fopen(filename, "r");
791 if (lf.f == NULL) return errfile(L, "open", fnameindex); 797 if (lf.f == NULL) return errfile(L, "open", fnameindex);
792 } 798 }
@@ -796,6 +802,7 @@ LUALIB_API int luaL_loadfilex (lua_State *L, const char *filename,
796 if (c == LUA_SIGNATURE[0]) { /* binary file? */ 802 if (c == LUA_SIGNATURE[0]) { /* binary file? */
797 lf.n = 0; /* remove possible newline */ 803 lf.n = 0; /* remove possible newline */
798 if (filename) { /* "real" file? */ 804 if (filename) { /* "real" file? */
805 errno = 0;
799 lf.f = freopen(filename, "rb", lf.f); /* reopen in binary mode */ 806 lf.f = freopen(filename, "rb", lf.f); /* reopen in binary mode */
800 if (lf.f == NULL) return errfile(L, "reopen", fnameindex); 807 if (lf.f == NULL) return errfile(L, "reopen", fnameindex);
801 skipcomment(lf.f, &c); /* re-read initial portion */ 808 skipcomment(lf.f, &c); /* re-read initial portion */
@@ -805,6 +812,7 @@ LUALIB_API int luaL_loadfilex (lua_State *L, const char *filename,
805 lf.buff[lf.n++] = c; /* 'c' is the first character of the stream */ 812 lf.buff[lf.n++] = c; /* 'c' is the first character of the stream */
806 status = lua_load(L, getF, &lf, lua_tostring(L, -1), mode); 813 status = lua_load(L, getF, &lf, lua_tostring(L, -1), mode);
807 readstatus = ferror(lf.f); 814 readstatus = ferror(lf.f);
815 errno = 0; /* no useful error number until here */
808 if (filename) fclose(lf.f); /* close file (even in case of errors) */ 816 if (filename) fclose(lf.f); /* close file (even in case of errors) */
809 if (readstatus) { 817 if (readstatus) {
810 lua_settop(L, fnameindex); /* ignore results from 'lua_load' */ 818 lua_settop(L, fnameindex); /* ignore results from 'lua_load' */
@@ -933,7 +941,7 @@ LUALIB_API const char *luaL_tolstring (lua_State *L, int idx, size_t *len) {
933LUALIB_API void luaL_setfuncs (lua_State *L, const luaL_Reg *l, int nup) { 941LUALIB_API void luaL_setfuncs (lua_State *L, const luaL_Reg *l, int nup) {
934 luaL_checkstack(L, nup, "too many upvalues"); 942 luaL_checkstack(L, nup, "too many upvalues");
935 for (; l->name != NULL; l++) { /* fill the table with given functions */ 943 for (; l->name != NULL; l++) { /* fill the table with given functions */
936 if (l->func == NULL) /* place holder? */ 944 if (l->func == NULL) /* placeholder? */
937 lua_pushboolean(L, 0); 945 lua_pushboolean(L, 0);
938 else { 946 else {
939 int i; 947 int i;