瀏覽代碼

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.
Roberto Ierusalimschy 1 年之前
父節點
當前提交
7eb1ed21b7
共有 2 個文件被更改,包括 5 次插入7 次删除
  1. 1 1
      lauxlib.c
  2. 4 6
      liolib.c

+ 1 - 1
lauxlib.c

@@ -811,9 +811,9 @@ LUALIB_API int luaL_loadfilex (lua_State *L, const char *filename,
   }
   if (c != EOF)
     lf.buff[lf.n++] = c;  /* 'c' is the first character of the stream */
+  errno = 0;
   status = lua_load(L, getF, &lf, lua_tostring(L, -1), mode);
   readstatus = ferror(lf.f);
-  errno = 0;  /* no useful error number until here */
   if (filename) fclose(lf.f);  /* close file (even in case of errors) */
   if (readstatus) {
     lua_settop(L, fnameindex);  /* ignore results from 'lua_load' */

+ 4 - 6
liolib.c

@@ -570,6 +570,7 @@ static int g_read (lua_State *L, FILE *f, int first) {
   int nargs = lua_gettop(L) - 1;
   int n, success;
   clearerr(f);
+  errno = 0;
   if (nargs == 0) {  /* no arguments? */
     success = read_line(L, f, 1);
     n = first + 1;  /* to return 1 result */
@@ -606,10 +607,8 @@ static int g_read (lua_State *L, FILE *f, int first) {
       }
     }
   }
-  if (ferror(f)) {
-    errno = 0;  /* no relevant errno here */
+  if (ferror(f))
     return luaL_fileresult(L, 0, NULL);
-  }
   if (!success) {
     lua_pop(L, 1);  /* remove last result */
     luaL_pushfail(L);  /* push nil instead */
@@ -665,6 +664,7 @@ static int io_readline (lua_State *L) {
 static int g_write (lua_State *L, FILE *f, int arg) {
   int nargs = lua_gettop(L) - arg;
   int status = 1;
+  errno = 0;
   for (; nargs--; arg++) {
     if (lua_type(L, arg) == LUA_TNUMBER) {
       /* optimization: could be done exactly as for strings */
@@ -683,10 +683,8 @@ static int g_write (lua_State *L, FILE *f, int arg) {
   }
   if (l_likely(status))
     return 1;  /* file handle already on stack top */
-  else {
-    errno = 0;  /* no relevant errno here */
+  else
     return luaL_fileresult(L, status, NULL);
-  }
 }