Better Patch for Lua 5.1.5 #2

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

Better Patch for Lua 5.1.5 #2

me the user-2

When loading a file, Lua may call the reader function again after it returned end of input . 
reported by Chris Howie on 05 Jun 2013. existed since 5.1. fixed in 5.2.0.
Example:

load(function () print("called"); return nil end)
--> called
--> called             (should be called only once!)

The patch given adds a new variable 'eoz'
I think it should just set z->reader to NULL 
and check for that condition instead

lzio.c:
@@ -22,10 +22,15 @@
   size_t size;
   lua_State *L = z->L;
   const char *buff;
+  if (!z->reader)
+    return EOZ;
   lua_unlock(L);
   buff = z->reader(L, z->data, &size);
   lua_lock(L);
-  if (buff == NULL || size == 0) return EOZ;
+  if (buff == NULL || size == 0) {
+    z->reader = NULL;  /* avoid calling reader function next time */
+    return EOZ;
+  }
   z->n = size - 1;
   z->p = buff;
   return char2int(*(z->p++));