Module Name: src Committed By: nikita Date: Mon Apr 17 19:16:38 UTC 2023
Modified Files: src/external/mit/lua/dist/src: lua.c Log Message: lua: Apply upstream bugfix for "lua.c assumes that argv has at least one element." To generate a diff of this commit: cvs rdiff -u -r1.10 -r1.11 src/external/mit/lua/dist/src/lua.c Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files.
Modified files: Index: src/external/mit/lua/dist/src/lua.c diff -u src/external/mit/lua/dist/src/lua.c:1.10 src/external/mit/lua/dist/src/lua.c:1.11 --- src/external/mit/lua/dist/src/lua.c:1.10 Sun Apr 16 20:46:17 2023 +++ src/external/mit/lua/dist/src/lua.c Mon Apr 17 19:16:38 2023 @@ -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, /* ** 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 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 */