Skip to content

Commit

Permalink
Fix command-line argv handling.
Browse files Browse the repository at this point in the history
  • Loading branch information
Mike Pall authored and mkokryashkin committed Nov 21, 2023
1 parent 3c8dbe2 commit d9982fe
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 12 deletions.
24 changes: 12 additions & 12 deletions src/luajit.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@

static lua_State *globalL = NULL;
static const char *progname = LUA_PROGNAME;
static char *empty_argv[2] = { NULL, NULL };

#if !LJ_TARGET_CONSOLE
static void lstop(lua_State *L, lua_Debug *ar)
Expand Down Expand Up @@ -90,9 +91,9 @@ static void print_tools_usage(void)
fflush(stderr);
}

static void l_message(const char *pname, const char *msg)
static void l_message(const char *msg)
{
if (pname) { fputs(pname, stderr); fputc(':', stderr); fputc(' ', stderr); }
if (progname) { fputs(progname, stderr); fputc(':', stderr); fputc(' ', stderr); }
fputs(msg, stderr); fputc('\n', stderr);
fflush(stderr);
}
Expand All @@ -102,7 +103,7 @@ static int report(lua_State *L, int status)
if (status && !lua_isnil(L, -1)) {
const char *msg = lua_tostring(L, -1);
if (msg == NULL) msg = "(error object is not a string)";
l_message(progname, msg);
l_message(msg);
lua_pop(L, 1);
}
return status;
Expand Down Expand Up @@ -267,9 +268,8 @@ static void dotty(lua_State *L)
lua_getglobal(L, "print");
lua_insert(L, 1);
if (lua_pcall(L, lua_gettop(L)-1, 0, 0) != 0)
l_message(progname,
lua_pushfstring(L, "error calling " LUA_QL("print") " (%s)",
lua_tostring(L, -1)));
l_message(lua_pushfstring(L, "error calling " LUA_QL("print") " (%s)",
lua_tostring(L, -1)));
}
}
lua_settop(L, 0); /* clear stack */
Expand Down Expand Up @@ -321,8 +321,7 @@ static int loadjitmodule(lua_State *L)
lua_getfield(L, -1, "start");
if (lua_isnil(L, -1)) {
nomodule:
l_message(progname,
"unknown luaJIT command or jit.* modules not installed");
l_message("unknown luaJIT command or jit.* modules not installed");
return 1;
}
lua_remove(L, -2); /* Drop module table. */
Expand Down Expand Up @@ -382,7 +381,7 @@ static int runtoolcmd(lua_State *L, const char *tool_name)
if (msg) {
if (!strncmp(msg, "module ", 7))
msg = "unknown luaJIT command or tools not installed";
l_message(progname, msg);
l_message(msg);
}
return 1;
}
Expand Down Expand Up @@ -566,7 +565,6 @@ static int pmain(lua_State *L)
int argn;
int flags = 0;
globalL = L;
if (argv[0] && argv[0][0]) progname = argv[0];

LUAJIT_VERSION_SYM(); /* Linker-enforced version check. */

Expand Down Expand Up @@ -622,9 +620,11 @@ static int pmain(lua_State *L)
int main(int argc, char **argv)
{
int status;
lua_State *L = lua_open();
lua_State *L;
if (!argv[0]) argv = empty_argv; else if (argv[0][0]) progname = argv[0];
L = lua_open(); /* create state */
if (L == NULL) {
l_message(argv[0], "cannot create state: not enough memory");
l_message("cannot create state: not enough memory");
return EXIT_FAILURE;
}
smain.argc = argc;
Expand Down
34 changes: 34 additions & 0 deletions test/tarantool-c-tests/fix-argv-handling.test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include <unistd.h>
#include <sys/resource.h>
#include <stdio.h>

#include "test.h"

static int null_argv(void *test_state)
{
struct rlimit memory_limit = {3 * 1024 * 1024 + 512 * 1024, RLIM_INFINITY};
if(-1 == setrlimit(RLIMIT_AS, &memory_limit))
bail_out("Failed to set memory limit.");
pid_t pid = fork();
if (pid == 0) {
execlp("/home/ubuntu/fckxorg/luajit/build/src/luajit", NULL);
}
else {
int status = 0;
waitpid(pid, &status, 0);
int segfault_occurred = WIFSIGNALED(status);
int exited_with_error = WEXITSTATUS(status);
assert_true(segfault_occurred == 0);
assert_true(exited_with_error != 0);
}
return TEST_EXIT_SUCCESS;
}

int main(void)
{
const struct test_unit tgroup[] = {
test_unit_def(null_argv),
};
const int test_result = test_run_group(tgroup, NULL);
return test_result;
}

0 comments on commit d9982fe

Please sign in to comment.