diff options
| author | nikita <nikita@NetBSD.org> | 2023-04-17 19:16:38 +0000 |
|---|---|---|
| committer | nikita <nikita@NetBSD.org> | 2023-04-17 19:16:38 +0000 |
| commit | cc273feb5cd5dd4e25e5586ed72417d7aed13044 (patch) | |
| tree | 191cd8b4f19c898a4d095668ce41f418d046c7d8 /external/mit | |
| parent | 6314f572eadf8f4b0d31a801340c23e37adc42db (diff) | |
lua: Apply upstream bugfix for "lua.c assumes that argv has at least one element."
Diffstat (limited to 'external/mit')
| -rw-r--r-- | external/mit/lua/dist/src/lua.c | 36 |
1 files changed, 24 insertions, 12 deletions
diff --git a/external/mit/lua/dist/src/lua.c b/external/mit/lua/dist/src/lua.c index 723393a33e6..36299020f1e 100644 --- a/external/mit/lua/dist/src/lua.c +++ b/external/mit/lua/dist/src/lua.c @@ -1,4 +1,4 @@ -/* $NetBSD: lua.c,v 1.10 2023/04/16 20:46:17 nikita Exp $ */ +/* $NetBSD: lua.c,v 1.11 2023/04/17 19:16:38 nikita Exp $ */ /* ** Id: lua.c @@ -179,10 +179,11 @@ static void print_version (void) { ** to the script (everything after 'script') go to positive indices; ** other arguments (before the script name) go to negative indices. ** If there is no script name, assume interpreter's name as base. +** (If there is no interpreter's name either, 'script' is -1, so +** table sizes are zero.) */ static void createargtable (lua_State *L, char **argv, int argc, int script) { int i, narg; - if (script == argc) script = 0; /* no script name? */ narg = argc - (script + 1); /* number of positive indices */ lua_createtable(L, narg, script + 1); for (i = 0; i < argc; i++) { @@ -270,14 +271,23 @@ static int handle_script (lua_State *L, char **argv) { /* ** Traverses all arguments from 'argv', returning a mask with those -** needed before running any Lua code (or an error code if it finds -** any invalid argument). 'first' returns the first not-handled argument -** (either the script name or a bad argument in case of error). +** needed before running any Lua code or an error code if it finds any +** invalid argument. In case of error, 'first' is the index of the bad +** argument. Otherwise, 'first' is -1 if there is no program name, +** 0 if there is no script name, or the index of the script name. */ static int collectargs (char **argv, int *first) { int args = 0; int i; - for (i = 1; argv[i] != NULL; i++) { + if (argv[0] != NULL) { /* is there a program name? */ + if (argv[0][0]) /* not empty? */ + progname = argv[0]; /* save it */ + } + else { /* no program name */ + *first = -1; + return 0; + } + for (i = 1; argv[i] != NULL; i++) { /* handle arguments */ *first = i; if (argv[i][0] != '-') /* not an option? */ return args; /* stop handling options */ @@ -318,7 +328,7 @@ static int collectargs (char **argv, int *first) { return has_error; } } - *first = i; /* no script name */ + *first = 0; /* no script name */ return args; } @@ -611,6 +621,7 @@ static int pmain (lua_State *L) { char **argv = (char **)lua_touserdata(L, 2); int script; int args = collectargs(argv, &script); + int optlim = (script > 0) ? script : argc; /* first argv not an option */ luaL_checkversion(L); /* check that interpreter has correct version */ if (argv[0] && argv[0][0]) progname = argv[0]; if (args == has_error) { /* bad arg? */ @@ -630,14 +641,15 @@ static int pmain (lua_State *L) { if (handle_luainit(L) != LUA_OK) /* run LUA_INIT */ return 0; /* error running LUA_INIT */ } - if (!runargs(L, argv, script)) /* execute arguments -e and -l */ + if (!runargs(L, argv, optlim)) /* execute arguments -e and -l */ return 0; /* something failed */ - if (script < argc && /* execute main script (if there is one) */ - handle_script(L, argv + script) != LUA_OK) - return 0; + if (script > 0) { /* execute main script (if there is one) */ + if (handle_script(L, argv + script) != LUA_OK) + return 0; + } if (args & has_i) /* -i option? */ doREPL(L); /* do read-eval-print loop */ - else if (script == argc && !(args & (has_e | has_v))) { /* no arguments? */ + else if (script < 1 && !(args & (has_e | has_v))) { /* no active option? */ if (lua_stdin_is_tty()) { /* running in interactive mode? */ print_version(); doREPL(L); /* do read-eval-print loop */ |
