From 1738e41b2a35e5f99b9a1300a5f687478458226a Mon Sep 17 00:00:00 2001 From: Haz Date: Tue, 14 Jul 2020 00:05:52 +0200 Subject: [PATCH] Fix segfault when calling `mrsh -o` * at main.c we call mrsh_process_args from builtins/set.c * that function calls set function impl. in the same file. * set function returns early in its switch block commandline arg `-o` when called without a long option. * this might be the desired behaviour, but it skips populating state->frame * the code responsible for that is after the switch statement * add a jump to handle this case --- builtin/set.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/builtin/set.c b/builtin/set.c index 06a0427..297b829 100644 --- a/builtin/set.c +++ b/builtin/set.c @@ -135,7 +135,7 @@ static int set(struct mrsh_state *state, int argc, char *argv[], case 'o': if (i + 1 == argc) { print_options(state); - return 0; + goto out; // we must populate state->argv } option = find_long_option(argv[i + 1]); if (!option) { @@ -199,7 +199,9 @@ static int set(struct mrsh_state *state, int argc, char *argv[], argv_free(state->frame->argc, state->frame->argv); state->frame->argc = argc - i + 1; state->frame->argv = argv_dup(argv_0, state->frame->argc, &argv[i]); - } else if (init_args != NULL) { + } else +out: + if (init_args != NULL) { // No args given, but we need to initialize state->argv state->frame->argc = 1; state->frame->argv = argv_dup(strdup(argv[0]), 1, argv); -- 2.45.2