Skip to content
Snippets Groups Projects
Commit 90eef979 authored by Vladislav Shpilevoy's avatar Vladislav Shpilevoy Committed by Kirill Yukhin
Browse files

app: handle concatenated argv name-value correctly

The server used to crash when any option argument was passed with
a value concatenated to it, like this: '-lvalue', '-evalue'
instead of '-l value' and '-e value'.

However this is a valid way of writing values, and it should not
have crashed regardless of its validity.

The bug was in usage of 'optind' global variable from getopt()
function family. It is not supposed to be used for getting an
option's value. It points to a next argv to parse. Next argv !=
value of current argv, like it was with '-lvalue' and '-evalue'.

For getting a current value there is a variable 'optarg'.

Closes #4775

(cherry picked from commit 29cfd564)
parent dd41ebc2
No related branches found
No related tags found
No related merge requests found
...@@ -553,7 +553,7 @@ run_script_f(va_list ap) ...@@ -553,7 +553,7 @@ run_script_f(va_list ap)
const char *path = va_arg(ap, const char *); const char *path = va_arg(ap, const char *);
bool interactive = va_arg(ap, int); bool interactive = va_arg(ap, int);
int optc = va_arg(ap, int); int optc = va_arg(ap, int);
char **optv = va_arg(ap, char **); const char **optv = va_arg(ap, const char **);
int argc = va_arg(ap, int); int argc = va_arg(ap, int);
char **argv = va_arg(ap, char **); char **argv = va_arg(ap, char **);
/* /*
...@@ -656,7 +656,7 @@ run_script_f(va_list ap) ...@@ -656,7 +656,7 @@ run_script_f(va_list ap)
int int
tarantool_lua_run_script(char *path, bool interactive, tarantool_lua_run_script(char *path, bool interactive,
int optc, char **optv, int argc, char **argv) int optc, const char **optv, int argc, char **argv)
{ {
const char *title = path ? basename(path) : "interactive"; const char *title = path ? basename(path) : "interactive";
/* /*
......
...@@ -72,7 +72,7 @@ tarantool_lua_free(); ...@@ -72,7 +72,7 @@ tarantool_lua_free();
*/ */
int int
tarantool_lua_run_script(char *path, bool force_interactive, tarantool_lua_run_script(char *path, bool force_interactive,
int optc, char **optv, int optc, const char **optv,
int argc, char **argv); int argc, char **argv);
extern char *history; extern char *history;
......
...@@ -721,7 +721,7 @@ main(int argc, char **argv) ...@@ -721,7 +721,7 @@ main(int argc, char **argv)
bool interactive = false; bool interactive = false;
/* Lua interpeter options, e.g. -e and -l */ /* Lua interpeter options, e.g. -e and -l */
int optc = 0; int optc = 0;
char **optv = NULL; const char **optv = NULL;
auto guard = make_scoped_guard([=]{ if (optc) free(optv); }); auto guard = make_scoped_guard([=]{ if (optc) free(optv); });
static struct option longopts[] = { static struct option longopts[] = {
...@@ -749,16 +749,13 @@ main(int argc, char **argv) ...@@ -749,16 +749,13 @@ main(int argc, char **argv)
case 'e': case 'e':
/* Save Lua interepter options to optv as is */ /* Save Lua interepter options to optv as is */
if (optc == 0) { if (optc == 0) {
optv = (char **) calloc(argc, sizeof(char *)); optv = (const char **) calloc(argc,
sizeof(optv[0]));
if (optv == NULL) if (optv == NULL)
panic_syserror("No enough memory for arguments"); panic_syserror("No enough memory for arguments");
} }
/* optv[optc++] = ch == 'l' ? "-l" : "-e";
* The variable optind is the index of the next optv[optc++] = optarg;
* element to be processed in argv.
*/
optv[optc++] = argv[optind - 2];
optv[optc++] = argv[optind - 1];
break; break;
default: default:
/* "invalid option" is printed by getopt */ /* "invalid option" is printed by getopt */
......
-- test-run result file version 2
--
-- gh-4775: crash on option concatenated with value.
--
child = io.popen('tarantool -e"print(100) os.exit()"')
| ---
| ...
child:read()
| ---
| - '100'
| ...
-- :close() is omitted, because SIGCHILD may be handled by
-- libev instead of Lua. In that case :close() with fail with
-- ECHILD, but it does not matter for this test.
--
-- gh-4775: crash on option concatenated with value.
--
child = io.popen('tarantool -e"print(100) os.exit()"')
child:read()
-- :close() is omitted, because SIGCHILD may be handled by
-- libev instead of Lua. In that case :close() with fail with
-- ECHILD, but it does not matter for this test.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment