Hi Luigi,

On Sat, 2025-02-08 at 04:21 -0700, Max Chernoff wrote:
> # Support setting the environment with "os.spawn".
> [...]

I found a couple small bugs with my implementation:

1. Passing the command-line as a table doesn't work if an environment
   table is added.

2. Segfaults if a zero-length environment table is passed.

3. Skips the last passed environment variable.

The attached patch should fix all of these.

Thanks, and sorry for the bugs,
-- Max
From 9bb9ec8d86e7979593b44904119c68fdd3082234 Mon Sep 17 00:00:00 2001
From: Max Chernoff <g...@maxchernoff.ca>
Date: Tue, 11 Feb 2025 00:10:00 -0700
Subject: [PATCH] Fix bugs in "os.spawn" second argument

---
 source/texk/web2c/luatexdir/lua/loslibext.c | 16 +++++++++++-----
 1 file changed, 11 insertions(+), 5 deletions(-)

diff --git a/source/texk/web2c/luatexdir/lua/loslibext.c b/source/texk/web2c/luatexdir/lua/loslibext.c
index e184a4624..b013c42a6 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
-- 
2.48.1

_______________________________________________
dev-luatex mailing list -- dev-luatex@ntg.nl
To unsubscribe send an email to dev-luatex-le...@ntg.nl

Reply via email to