fix bugs with args and env array

This commit is contained in:
takase1121 2021-07-08 11:21:11 +08:00
parent b38add8de5
commit 453df84c82
1 changed files with 16 additions and 14 deletions

View File

@ -57,8 +57,6 @@ static int poll_process(process_t* proc, int timeout)
static int process_start(lua_State* L) static int process_start(lua_State* L)
{ {
int cmd_len = lua_rawlen(L, 1) + 1;
const char** cmd = malloc(sizeof(char *) * cmd_len);
luaL_checktype(L, 1, LUA_TTABLE); luaL_checktype(L, 1, LUA_TTABLE);
if (lua_isnoneornil(L, 2)) { if (lua_isnoneornil(L, 2)) {
lua_settop(L, 1); // remove the nil if it's there lua_settop(L, 1); // remove the nil if it's there
@ -66,6 +64,8 @@ static int process_start(lua_State* L)
} }
luaL_checktype(L, 2, LUA_TTABLE); luaL_checktype(L, 2, LUA_TTABLE);
int cmd_len = lua_rawlen(L, 1);
const char** cmd = malloc(sizeof(char *) * (cmd_len + 1));
ASSERT_MALLOC(cmd); ASSERT_MALLOC(cmd);
cmd[cmd_len] = NULL; cmd[cmd_len] = NULL;
@ -95,16 +95,17 @@ static int process_start(lua_State* L)
// env // env
luaL_getsubtable(L, 2, "env"); luaL_getsubtable(L, 2, "env");
const char **env = NULL;
int env_len = 0;
lua_pushnil(L); lua_pushnil(L);
int env_len = 1;
while (lua_next(L, -2) != 0) { while (lua_next(L, -2) != 0) {
env_len++; env_len++;
lua_pop(L, 1); lua_pop(L, 1);
} }
const char** env = malloc(sizeof(char *) * env_len); if (env_len > 0) {
ASSERT_MALLOC(env); env = malloc(sizeof(char*) * (env_len + 1));
env[env_len] = NULL; env[env_len] = NULL;
int i = 0; int i = 0;
@ -117,6 +118,7 @@ static int process_start(lua_State* L)
env[i++] = luaL_checkstring(L, -1); env[i++] = luaL_checkstring(L, -1);
lua_pop(L, 1); lua_pop(L, 1);
} }
}
reproc_t* proc = reproc_new(); reproc_t* proc = reproc_new();
int out = reproc_start( int out = reproc_start(