diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2022-02-07 10:16:35 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2022-02-07 10:16:35 -0300 |
commit | 25b143dd34fb587d1e35290c4b25bc08954800e2 (patch) | |
tree | 9c677071fbecb61696bdbd4692855ef3e833149e /lua.c | |
parent | 5d708c3f9cae12820e415d4f89c9eacbe2ab964b (diff) | |
download | lua-25b143dd34fb587d1e35290c4b25bc08954800e2.tar.gz lua-25b143dd34fb587d1e35290c4b25bc08954800e2.tar.bz2 lua-25b143dd34fb587d1e35290c4b25bc08954800e2.zip |
Bug: lua.c assumes that argv has at least one element
Diffstat (limited to 'lua.c')
-rw-r--r-- | lua.c | 35 |
1 files changed, 23 insertions, 12 deletions
@@ -177,10 +177,11 @@ static void print_version (void) { | |||
177 | ** to the script (everything after 'script') go to positive indices; | 177 | ** to the script (everything after 'script') go to positive indices; |
178 | ** other arguments (before the script name) go to negative indices. | 178 | ** other arguments (before the script name) go to negative indices. |
179 | ** If there is no script name, assume interpreter's name as base. | 179 | ** If there is no script name, assume interpreter's name as base. |
180 | ** (If there is no interpreter's name either, 'script' is -1, so | ||
181 | ** table sizes are zero.) | ||
180 | */ | 182 | */ |
181 | static void createargtable (lua_State *L, char **argv, int argc, int script) { | 183 | static void createargtable (lua_State *L, char **argv, int argc, int script) { |
182 | int i, narg; | 184 | int i, narg; |
183 | if (script == argc) script = 0; /* no script name? */ | ||
184 | narg = argc - (script + 1); /* number of positive indices */ | 185 | narg = argc - (script + 1); /* number of positive indices */ |
185 | lua_createtable(L, narg, script + 1); | 186 | lua_createtable(L, narg, script + 1); |
186 | for (i = 0; i < argc; i++) { | 187 | for (i = 0; i < argc; i++) { |
@@ -268,14 +269,23 @@ static int handle_script (lua_State *L, char **argv) { | |||
268 | 269 | ||
269 | /* | 270 | /* |
270 | ** Traverses all arguments from 'argv', returning a mask with those | 271 | ** Traverses all arguments from 'argv', returning a mask with those |
271 | ** needed before running any Lua code (or an error code if it finds | 272 | ** needed before running any Lua code or an error code if it finds any |
272 | ** any invalid argument). 'first' returns the first not-handled argument | 273 | ** invalid argument. In case of error, 'first' is the index of the bad |
273 | ** (either the script name or a bad argument in case of error). | 274 | ** argument. Otherwise, 'first' is -1 if there is no program name, |
275 | ** 0 if there is no script name, or the index of the script name. | ||
274 | */ | 276 | */ |
275 | static int collectargs (char **argv, int *first) { | 277 | static int collectargs (char **argv, int *first) { |
276 | int args = 0; | 278 | int args = 0; |
277 | int i; | 279 | int i; |
278 | for (i = 1; argv[i] != NULL; i++) { | 280 | if (argv[0] != NULL) { /* is there a program name? */ |
281 | if (argv[0][0]) /* not empty? */ | ||
282 | progname = argv[0]; /* save it */ | ||
283 | } | ||
284 | else { /* no program name */ | ||
285 | *first = -1; | ||
286 | return 0; | ||
287 | } | ||
288 | for (i = 1; argv[i] != NULL; i++) { /* handle arguments */ | ||
279 | *first = i; | 289 | *first = i; |
280 | if (argv[i][0] != '-') /* not an option? */ | 290 | if (argv[i][0] != '-') /* not an option? */ |
281 | return args; /* stop handling options */ | 291 | return args; /* stop handling options */ |
@@ -316,7 +326,7 @@ static int collectargs (char **argv, int *first) { | |||
316 | return has_error; | 326 | return has_error; |
317 | } | 327 | } |
318 | } | 328 | } |
319 | *first = i; /* no script name */ | 329 | *first = 0; /* no script name */ |
320 | return args; | 330 | return args; |
321 | } | 331 | } |
322 | 332 | ||
@@ -609,8 +619,8 @@ static int pmain (lua_State *L) { | |||
609 | char **argv = (char **)lua_touserdata(L, 2); | 619 | char **argv = (char **)lua_touserdata(L, 2); |
610 | int script; | 620 | int script; |
611 | int args = collectargs(argv, &script); | 621 | int args = collectargs(argv, &script); |
622 | int optlim = (script > 0) ? script : argc; /* first argv not an option */ | ||
612 | luaL_checkversion(L); /* check that interpreter has correct version */ | 623 | luaL_checkversion(L); /* check that interpreter has correct version */ |
613 | if (argv[0] && argv[0][0]) progname = argv[0]; | ||
614 | if (args == has_error) { /* bad arg? */ | 624 | if (args == has_error) { /* bad arg? */ |
615 | print_usage(argv[script]); /* 'script' has index of bad arg. */ | 625 | print_usage(argv[script]); /* 'script' has index of bad arg. */ |
616 | return 0; | 626 | return 0; |
@@ -628,14 +638,15 @@ static int pmain (lua_State *L) { | |||
628 | if (handle_luainit(L) != LUA_OK) /* run LUA_INIT */ | 638 | if (handle_luainit(L) != LUA_OK) /* run LUA_INIT */ |
629 | return 0; /* error running LUA_INIT */ | 639 | return 0; /* error running LUA_INIT */ |
630 | } | 640 | } |
631 | if (!runargs(L, argv, script)) /* execute arguments -e and -l */ | 641 | if (!runargs(L, argv, optlim)) /* execute arguments -e and -l */ |
632 | return 0; /* something failed */ | 642 | return 0; /* something failed */ |
633 | if (script < argc && /* execute main script (if there is one) */ | 643 | if (script > 0) { /* execute main script (if there is one) */ |
634 | handle_script(L, argv + script) != LUA_OK) | 644 | if (handle_script(L, argv + script) != LUA_OK) |
635 | return 0; | 645 | return 0; /* interrupt in case of error */ |
646 | } | ||
636 | if (args & has_i) /* -i option? */ | 647 | if (args & has_i) /* -i option? */ |
637 | doREPL(L); /* do read-eval-print loop */ | 648 | doREPL(L); /* do read-eval-print loop */ |
638 | else if (script == argc && !(args & (has_e | has_v))) { /* no arguments? */ | 649 | else if (script < 1 && !(args & (has_e | has_v))) { /* no active option? */ |
639 | if (lua_stdin_is_tty()) { /* running in interactive mode? */ | 650 | if (lua_stdin_is_tty()) { /* running in interactive mode? */ |
640 | print_version(); | 651 | print_version(); |
641 | doREPL(L); /* do read-eval-print loop */ | 652 | doREPL(L); /* do read-eval-print loop */ |