Fix nixio exece()
authorJo-Philipp Wich <jow@openwrt.org>
Mon, 29 Oct 2012 12:52:27 +0000 (12:52 +0000)
committerJo-Philipp Wich <jow@openwrt.org>
Mon, 29 Oct 2012 12:52:27 +0000 (12:52 +0000)
I had occasion to use nixio.exece() recently and I could not figure out what it
wanted for the third argument (the environment) as no matter what sort of table
I passed, even nixio.getenv(), would fail with bad argument #3 to 'exece'
(invalid environment).

What it should expect is a table full of string values for keys and something
that can be converted to a string as a value, however it inverts the value of
lua_type() and compares it against a type, which will never evaluate to true.

Getting past this, the code inserts the KEY=VALUE string before the arg userdata,
and then forgets to take the env userdata into account when collecting the
arguments back into a C char**. This patch addresses all these issues and
provides a working nixio.exece().

[Fixes #500]

libs/nixio/src/process.c

index 45c06a6..5ae9b82 100644 (file)
@@ -65,14 +65,14 @@ int nixio__exec(lua_State *L, int m) {
                                        return luaL_error(L, "stack overflow");
                                }
 
-                               if (!lua_type(L, -2) != LUA_TSTRING || !lua_isstring(L, -1)) {
+                               if (lua_type(L, -2) != LUA_TSTRING || !lua_isstring(L, -1)) {
                                        return luaL_argerror(L, 3, "invalid environment");
                                }
 
                                lua_pushfstring(L, "%s=%s",
                                                lua_tostring(L, -2), lua_tostring(L, -1));
 
-                               lua_insert(L, 4);
+                               lua_insert(L, 5);
                                lua_pop(L, 1);
                                argn++;
                        }
@@ -80,8 +80,8 @@ int nixio__exec(lua_State *L, int m) {
                        char **env = lua_newuserdata(L, sizeof(char*) * (argn + 1));
                        env[argn] = NULL;
 
-                       for (i = 1; i < argn; i++) {
-                               env[i-1] = (char *)lua_tostring(L, -i);
+                       for (i = 1; i <= argn; i++) {
+                               env[i-1] = (char *)lua_tostring(L, -(i+1));
                        }
 
                        execve(path, args, env);