Fix a bug where argv will be replaced by the argparse library.

The argparse library takes the input, assigns it to `this->output`, and then writes a null pointer to it. This causes issues on OS X 10.10 because AppKit tries to read the arguments passed to the application, which the first argument is null.
This commit is contained in:
Kyle Kirbatski 2015-12-12 13:45:51 -06:00 committed by Kyle Kirby
parent 0a86b8c946
commit c3185de49d
1 changed files with 14 additions and 2 deletions

View File

@ -90,10 +90,19 @@ int cmdline_run(const char **argv, int argc)
argparse_t argparse; argparse_t argparse;
argparse_init(&argparse, options, usage, 0); argparse_init(&argparse, options, usage, 0);
argc = argparse_parse(&argparse, argc, argv); size_t argvsize = sizeof(char**) * argc;
/**
* argparse_parse ends up inadvertently mutating the argv variable, setting
* a null pointer in the array. Because of this, AppKit in OS X 10.10 will
* dereference it, causing a segmentation fault.
*/
const char** mutableArgv = malloc(argvsize);
memcpy(mutableArgv,argv,argvsize);
argc = argparse_parse(&argparse, argc, mutableArgv);
if (version) { if (version) {
print_version(); print_version();
free(mutableArgv);
return 0; return 0;
} }
@ -121,7 +130,8 @@ int cmdline_run(const char **argv, int argc)
if (argc != 0) { if (argc != 0) {
// see comment next to cmdline_call_action for expected return codes // see comment next to cmdline_call_action for expected return codes
gExitCode = cmdline_call_action(argv, argc); gExitCode = cmdline_call_action(mutableArgv, argc);
free(mutableArgv);
if (gExitCode < 0) { if (gExitCode < 0) {
// action failed, don't change exit code // action failed, don't change exit code
// and don't start the game // and don't start the game
@ -133,6 +143,8 @@ int cmdline_run(const char **argv, int argc)
return 0; return 0;
} }
// start the game, so far exit code means success. // start the game, so far exit code means success.
} else {
free(mutableArgv);
} }
// Headless mode requires a park to open // Headless mode requires a park to open