Skip to content

Commit 7ff5d29

Browse files
Mike Pallmkokryashkin
Mike Pall
authored andcommitted
Fix command-line argv handling.
1 parent 3c8dbe2 commit 7ff5d29

File tree

2 files changed

+51
-12
lines changed

2 files changed

+51
-12
lines changed

src/luajit.c

+12-12
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939

4040
static lua_State *globalL = NULL;
4141
static const char *progname = LUA_PROGNAME;
42+
static char *empty_argv[2] = { NULL, NULL };
4243

4344
#if !LJ_TARGET_CONSOLE
4445
static void lstop(lua_State *L, lua_Debug *ar)
@@ -90,9 +91,9 @@ static void print_tools_usage(void)
9091
fflush(stderr);
9192
}
9293

93-
static void l_message(const char *pname, const char *msg)
94+
static void l_message(const char *msg)
9495
{
95-
if (pname) { fputs(pname, stderr); fputc(':', stderr); fputc(' ', stderr); }
96+
if (progname) { fputs(progname, stderr); fputc(':', stderr); fputc(' ', stderr); }
9697
fputs(msg, stderr); fputc('\n', stderr);
9798
fflush(stderr);
9899
}
@@ -102,7 +103,7 @@ static int report(lua_State *L, int status)
102103
if (status && !lua_isnil(L, -1)) {
103104
const char *msg = lua_tostring(L, -1);
104105
if (msg == NULL) msg = "(error object is not a string)";
105-
l_message(progname, msg);
106+
l_message(msg);
106107
lua_pop(L, 1);
107108
}
108109
return status;
@@ -267,9 +268,8 @@ static void dotty(lua_State *L)
267268
lua_getglobal(L, "print");
268269
lua_insert(L, 1);
269270
if (lua_pcall(L, lua_gettop(L)-1, 0, 0) != 0)
270-
l_message(progname,
271-
lua_pushfstring(L, "error calling " LUA_QL("print") " (%s)",
272-
lua_tostring(L, -1)));
271+
l_message(lua_pushfstring(L, "error calling " LUA_QL("print") " (%s)",
272+
lua_tostring(L, -1)));
273273
}
274274
}
275275
lua_settop(L, 0); /* clear stack */
@@ -321,8 +321,7 @@ static int loadjitmodule(lua_State *L)
321321
lua_getfield(L, -1, "start");
322322
if (lua_isnil(L, -1)) {
323323
nomodule:
324-
l_message(progname,
325-
"unknown luaJIT command or jit.* modules not installed");
324+
l_message("unknown luaJIT command or jit.* modules not installed");
326325
return 1;
327326
}
328327
lua_remove(L, -2); /* Drop module table. */
@@ -382,7 +381,7 @@ static int runtoolcmd(lua_State *L, const char *tool_name)
382381
if (msg) {
383382
if (!strncmp(msg, "module ", 7))
384383
msg = "unknown luaJIT command or tools not installed";
385-
l_message(progname, msg);
384+
l_message(msg);
386385
}
387386
return 1;
388387
}
@@ -566,7 +565,6 @@ static int pmain(lua_State *L)
566565
int argn;
567566
int flags = 0;
568567
globalL = L;
569-
if (argv[0] && argv[0][0]) progname = argv[0];
570568

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

@@ -622,9 +620,11 @@ static int pmain(lua_State *L)
622620
int main(int argc, char **argv)
623621
{
624622
int status;
625-
lua_State *L = lua_open();
623+
lua_State *L;
624+
if (!argv[0]) argv = empty_argv; else if (argv[0][0]) progname = argv[0];
625+
L = lua_open(); /* create state */
626626
if (L == NULL) {
627-
l_message(argv[0], "cannot create state: not enough memory");
627+
l_message("cannot create state: not enough memory");
628628
return EXIT_FAILURE;
629629
}
630630
smain.argc = argc;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#include <unistd.h>
2+
#include <sys/resource.h>
3+
#include <stdio.h>
4+
5+
#include "test.h"
6+
7+
static int null_argv(void *test_state)
8+
{
9+
struct rlimit memory_limit = {};
10+
getrlimit(RLIMIT_AS, &memory_limit);
11+
memory_limit.rlim_cur = memory_limit.rlim_cur >> 25;
12+
printf("One: %lld Two: %lld\n", memory_limit.rlim_cur, memory_limit.rlim_max);
13+
if(-1 == setrlimit(RLIMIT_AS, &memory_limit))
14+
perror("Fuck!");
15+
else
16+
printf("Stonks");
17+
pid_t pid = fork();
18+
if (pid == 0) {
19+
execlp("/Users/m.kokryashkin/dev/luajit/build/src/luajit", NULL);
20+
}
21+
else {
22+
int status = 0;
23+
waitpid(pid, &status, 0);
24+
int segfault_occurred = WIFSIGNALED(status);
25+
int exited_with_error = WEXITSTATUS(status);
26+
assert_true(segfault_occurred == 0);
27+
assert_true(exited_with_error != 0);
28+
}
29+
return TEST_EXIT_SUCCESS;
30+
}
31+
32+
int main(void)
33+
{
34+
const struct test_unit tgroup[] = {
35+
test_unit_def(null_argv),
36+
};
37+
const int test_result = test_run_group(tgroup, NULL);
38+
return test_result;
39+
}

0 commit comments

Comments
 (0)