aboutsummaryrefslogtreecommitdiff
path: root/liolib.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 /liolib.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 'liolib.c')
-rw-r--r--liolib.c31
1 files changed, 23 insertions, 8 deletions
diff --git a/liolib.c b/liolib.c
index b08397da..82f444b0 100644
--- a/liolib.c
+++ b/liolib.c
@@ -245,8 +245,8 @@ static int f_gc (lua_State *L) {
245*/ 245*/
246static int io_fclose (lua_State *L) { 246static int io_fclose (lua_State *L) {
247 LStream *p = tolstream(L); 247 LStream *p = tolstream(L);
248 int res = fclose(p->f); 248 errno = 0;
249 return luaL_fileresult(L, (res == 0), NULL); 249 return luaL_fileresult(L, (fclose(p->f) == 0), NULL);
250} 250}
251 251
252 252
@@ -272,6 +272,7 @@ static int io_open (lua_State *L) {
272 LStream *p = newfile(L); 272 LStream *p = newfile(L);
273 const char *md = mode; /* to traverse/check mode */ 273 const char *md = mode; /* to traverse/check mode */
274 luaL_argcheck(L, l_checkmode(md), 2, "invalid mode"); 274 luaL_argcheck(L, l_checkmode(md), 2, "invalid mode");
275 errno = 0;
275 p->f = fopen(filename, mode); 276 p->f = fopen(filename, mode);
276 return (p->f == NULL) ? luaL_fileresult(L, 0, filename) : 1; 277 return (p->f == NULL) ? luaL_fileresult(L, 0, filename) : 1;
277} 278}
@@ -292,6 +293,7 @@ static int io_popen (lua_State *L) {
292 const char *mode = luaL_optstring(L, 2, "r"); 293 const char *mode = luaL_optstring(L, 2, "r");
293 LStream *p = newprefile(L); 294 LStream *p = newprefile(L);
294 luaL_argcheck(L, l_checkmodep(mode), 2, "invalid mode"); 295 luaL_argcheck(L, l_checkmodep(mode), 2, "invalid mode");
296 errno = 0;
295 p->f = l_popen(L, filename, mode); 297 p->f = l_popen(L, filename, mode);
296 p->closef = &io_pclose; 298 p->closef = &io_pclose;
297 return (p->f == NULL) ? luaL_fileresult(L, 0, filename) : 1; 299 return (p->f == NULL) ? luaL_fileresult(L, 0, filename) : 1;
@@ -300,6 +302,7 @@ static int io_popen (lua_State *L) {
300 302
301static int io_tmpfile (lua_State *L) { 303static int io_tmpfile (lua_State *L) {
302 LStream *p = newfile(L); 304 LStream *p = newfile(L);
305 errno = 0;
303 p->f = tmpfile(); 306 p->f = tmpfile();
304 return (p->f == NULL) ? luaL_fileresult(L, 0, NULL) : 1; 307 return (p->f == NULL) ? luaL_fileresult(L, 0, NULL) : 1;
305} 308}
@@ -603,8 +606,10 @@ static int g_read (lua_State *L, FILE *f, int first) {
603 } 606 }
604 } 607 }
605 } 608 }
606 if (ferror(f)) 609 if (ferror(f)) {
610 errno = 0; /* no relevant errno here */
607 return luaL_fileresult(L, 0, NULL); 611 return luaL_fileresult(L, 0, NULL);
612 }
608 if (!success) { 613 if (!success) {
609 lua_pop(L, 1); /* remove last result */ 614 lua_pop(L, 1); /* remove last result */
610 luaL_pushfail(L); /* push nil instead */ 615 luaL_pushfail(L); /* push nil instead */
@@ -678,7 +683,10 @@ static int g_write (lua_State *L, FILE *f, int arg) {
678 } 683 }
679 if (l_likely(status)) 684 if (l_likely(status))
680 return 1; /* file handle already on stack top */ 685 return 1; /* file handle already on stack top */
681 else return luaL_fileresult(L, status, NULL); 686 else {
687 errno = 0; /* no relevant errno here */
688 return luaL_fileresult(L, status, NULL);
689 }
682} 690}
683 691
684 692
@@ -703,6 +711,7 @@ static int f_seek (lua_State *L) {
703 l_seeknum offset = (l_seeknum)p3; 711 l_seeknum offset = (l_seeknum)p3;
704 luaL_argcheck(L, (lua_Integer)offset == p3, 3, 712 luaL_argcheck(L, (lua_Integer)offset == p3, 3,
705 "not an integer in proper range"); 713 "not an integer in proper range");
714 errno = 0;
706 op = l_fseek(f, offset, mode[op]); 715 op = l_fseek(f, offset, mode[op]);
707 if (l_unlikely(op)) 716 if (l_unlikely(op))
708 return luaL_fileresult(L, 0, NULL); /* error */ 717 return luaL_fileresult(L, 0, NULL); /* error */
@@ -719,19 +728,25 @@ static int f_setvbuf (lua_State *L) {
719 FILE *f = tofile(L); 728 FILE *f = tofile(L);
720 int op = luaL_checkoption(L, 2, NULL, modenames); 729 int op = luaL_checkoption(L, 2, NULL, modenames);
721 lua_Integer sz = luaL_optinteger(L, 3, LUAL_BUFFERSIZE); 730 lua_Integer sz = luaL_optinteger(L, 3, LUAL_BUFFERSIZE);
722 int res = setvbuf(f, NULL, mode[op], (size_t)sz); 731 int res;
732 errno = 0;
733 res = setvbuf(f, NULL, mode[op], (size_t)sz);
723 return luaL_fileresult(L, res == 0, NULL); 734 return luaL_fileresult(L, res == 0, NULL);
724} 735}
725 736
726 737
727 738
728static int io_flush (lua_State *L) { 739static int io_flush (lua_State *L) {
729 return luaL_fileresult(L, fflush(getiofile(L, IO_OUTPUT)) == 0, NULL); 740 FILE *f = getiofile(L, IO_OUTPUT);
741 errno = 0;
742 return luaL_fileresult(L, fflush(f) == 0, NULL);
730} 743}
731 744
732 745
733static int f_flush (lua_State *L) { 746static int f_flush (lua_State *L) {
734 return luaL_fileresult(L, fflush(tofile(L)) == 0, NULL); 747 FILE *f = tofile(L);
748 errno = 0;
749 return luaL_fileresult(L, fflush(f) == 0, NULL);
735} 750}
736 751
737 752
@@ -773,7 +788,7 @@ static const luaL_Reg meth[] = {
773** metamethods for file handles 788** metamethods for file handles
774*/ 789*/
775static const luaL_Reg metameth[] = { 790static const luaL_Reg metameth[] = {
776 {"__index", NULL}, /* place holder */ 791 {"__index", NULL}, /* placeholder */
777 {"__gc", f_gc}, 792 {"__gc", f_gc},
778 {"__close", f_gc}, 793 {"__close", f_gc},
779 {"__tostring", f_tostring}, 794 {"__tostring", f_tostring},