aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2024-06-05 11:50:42 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2024-06-05 11:50:42 -0300
commit7eb1ed21b7057ab5f1b921f8271eddcf13659737 (patch)
tree921d52f71dfa72461b0dc45ee3667f98f56521ba
parent2db966fcbf757775c842bc66449d7e697826aa1d (diff)
downloadlua-7eb1ed21b7057ab5f1b921f8271eddcf13659737.tar.gz
lua-7eb1ed21b7057ab5f1b921f8271eddcf13659737.tar.bz2
lua-7eb1ed21b7057ab5f1b921f8271eddcf13659737.zip
More permissive use of 'errno'
Assume 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.
-rw-r--r--lauxlib.c2
-rw-r--r--liolib.c10
2 files changed, 5 insertions, 7 deletions
diff --git a/lauxlib.c b/lauxlib.c
index baa67ce6..923105ed 100644
--- a/lauxlib.c
+++ b/lauxlib.c
@@ -811,9 +811,9 @@ LUALIB_API int luaL_loadfilex (lua_State *L, const char *filename,
811 } 811 }
812 if (c != EOF) 812 if (c != EOF)
813 lf.buff[lf.n++] = c; /* 'c' is the first character of the stream */ 813 lf.buff[lf.n++] = c; /* 'c' is the first character of the stream */
814 errno = 0;
814 status = lua_load(L, getF, &lf, lua_tostring(L, -1), mode); 815 status = lua_load(L, getF, &lf, lua_tostring(L, -1), mode);
815 readstatus = ferror(lf.f); 816 readstatus = ferror(lf.f);
816 errno = 0; /* no useful error number until here */
817 if (filename) fclose(lf.f); /* close file (even in case of errors) */ 817 if (filename) fclose(lf.f); /* close file (even in case of errors) */
818 if (readstatus) { 818 if (readstatus) {
819 lua_settop(L, fnameindex); /* ignore results from 'lua_load' */ 819 lua_settop(L, fnameindex); /* ignore results from 'lua_load' */
diff --git a/liolib.c b/liolib.c
index 82f444b0..c5075f3e 100644
--- a/liolib.c
+++ b/liolib.c
@@ -570,6 +570,7 @@ static int g_read (lua_State *L, FILE *f, int first) {
570 int nargs = lua_gettop(L) - 1; 570 int nargs = lua_gettop(L) - 1;
571 int n, success; 571 int n, success;
572 clearerr(f); 572 clearerr(f);
573 errno = 0;
573 if (nargs == 0) { /* no arguments? */ 574 if (nargs == 0) { /* no arguments? */
574 success = read_line(L, f, 1); 575 success = read_line(L, f, 1);
575 n = first + 1; /* to return 1 result */ 576 n = first + 1; /* to return 1 result */
@@ -606,10 +607,8 @@ static int g_read (lua_State *L, FILE *f, int first) {
606 } 607 }
607 } 608 }
608 } 609 }
609 if (ferror(f)) { 610 if (ferror(f))
610 errno = 0; /* no relevant errno here */
611 return luaL_fileresult(L, 0, NULL); 611 return luaL_fileresult(L, 0, NULL);
612 }
613 if (!success) { 612 if (!success) {
614 lua_pop(L, 1); /* remove last result */ 613 lua_pop(L, 1); /* remove last result */
615 luaL_pushfail(L); /* push nil instead */ 614 luaL_pushfail(L); /* push nil instead */
@@ -665,6 +664,7 @@ static int io_readline (lua_State *L) {
665static int g_write (lua_State *L, FILE *f, int arg) { 664static int g_write (lua_State *L, FILE *f, int arg) {
666 int nargs = lua_gettop(L) - arg; 665 int nargs = lua_gettop(L) - arg;
667 int status = 1; 666 int status = 1;
667 errno = 0;
668 for (; nargs--; arg++) { 668 for (; nargs--; arg++) {
669 if (lua_type(L, arg) == LUA_TNUMBER) { 669 if (lua_type(L, arg) == LUA_TNUMBER) {
670 /* optimization: could be done exactly as for strings */ 670 /* optimization: could be done exactly as for strings */
@@ -683,10 +683,8 @@ static int g_write (lua_State *L, FILE *f, int arg) {
683 } 683 }
684 if (l_likely(status)) 684 if (l_likely(status))
685 return 1; /* file handle already on stack top */ 685 return 1; /* file handle already on stack top */
686 else { 686 else
687 errno = 0; /* no relevant errno here */
688 return luaL_fileresult(L, status, NULL); 687 return luaL_fileresult(L, status, NULL);
689 }
690} 688}
691 689
692 690