diff --git a/source/texk/web2c/luatexdir/lua/loslibext.c b/source/texk/web2c/luatexdir/lua/loslibext.c
index e184a4624a6b133e25efb5f37e6bff2632b05614..b013c42a637c016e4d0c120af8fa6c691aa6bbd8 100644
--- a/source/texk/web2c/luatexdir/lua/loslibext.c
+++ b/source/texk/web2c/luatexdir/lua/loslibext.c
@@ -382,7 +382,7 @@ static char **do_flatten_command(lua_State * L, char **runcmd)
     *runcmd = NULL;
 
     for (j = 1;; j++) {
-        lua_rawgeti(L, -1, j);
+        lua_rawgeti(L, 1, j);
         if (lua_isnil(L, -1)) {
             lua_pop(L, 1);
             break;
@@ -394,7 +394,7 @@ static char **do_flatten_command(lua_State * L, char **runcmd)
     cmdline = malloc(sizeof(char *) * (unsigned) (j + 1));
     for (i = 1; i <= (unsigned) j; i++) {
         cmdline[i] = NULL;
-        lua_rawgeti(L, -1, (int) i);
+        lua_rawgeti(L, 1, (int) i);
         if (lua_isnil(L, -1) || (s = lua_tostring(L, -1)) == NULL) {
             lua_pop(L, 1);
             if (i == 1) {
@@ -410,7 +410,7 @@ static char **do_flatten_command(lua_State * L, char **runcmd)
     }
     cmdline[i] = NULL;
 
-    lua_rawgeti(L, -1, 0);
+    lua_rawgeti(L, 1, 0);
     if (lua_isnil(L, -1) || (s = lua_tostring(L, -1)) == NULL) {
 #ifdef _WIN32
         *runcmd = get_command_name(cmdline[0]);
@@ -546,12 +546,18 @@ static int os_spawn(lua_State * L)
                 val = lua_tostring(L, -1);
                 value = xmalloc((unsigned) (strlen(key) + strlen(val) + 2));
                 sprintf(value, "%s=%s", key, val);
-                envblock = xreallocarray(envblock, char*, size++ + 1);
+                envblock = xreallocarray(envblock, char*, size + 1);
                 envblock[size] = value;
+                size++;
             }
             lua_pop(L, 1);
         }
-        envblock[size++] = NULL;
+        if (envblock) {
+            envblock[size++] = NULL;
+        } else {
+            envblock = xmalloc(sizeof(char *));
+            envblock[0] = NULL;
+        }
     }
     /* If restrictedshell == 0, any command is allowed. */
     /* this is a little different from \write18/ os.execute processing
diff --git a/source/texk/web2c/luatexdir/luatex_svnversion.h b/source/texk/web2c/luatexdir/luatex_svnversion.h
index aad2746ab8a7f438bd179233c2efcdf8dc8898ed..9132d0eccdadc0795694f9a7b0e70658b45eacc7 100644
--- a/source/texk/web2c/luatexdir/luatex_svnversion.h
+++ b/source/texk/web2c/luatexdir/luatex_svnversion.h
@@ -1,4 +1,4 @@
 #ifndef luatex_svn_revision_h
 #define luatex_svn_revision_h
-#define luatex_svn_revision 7664
+#define luatex_svn_revision 7665
 #endif