Skip to content

Commit f917f57

Browse files
ematsumiyagitster
authored andcommitted
pager: fix crash when pager program doesn't exist
When prepare_cmd() fails for, e.g., pager process setup, child_process_clear() frees the memory in pager_process.args, but .argv was pointed to pager_process.args.v earlier in start_command(), so it's now a dangling pointer. setup_pager() is then called a second time, from cmd_log_init_finish() in this case, and any further operations using its .argv, e.g. strvec_*, will use the dangling pointer and eventually crash. According to trivial tests, setup_pager() is not called twice if the first call is successful. This patch makes sure that pager_process is properly initialized on setup_pager(). Drop CHILD_PROCESS_INIT from its declaration since it's no longer really necessary. Add a test to catch possible regressions. Reproducer: $ git config pager.show INVALID_PAGER $ git show $VALID_COMMIT error: cannot run INVALID_PAGER: No such file or directory [1] 3619 segmentation fault (core dumped) git show $VALID_COMMIT Signed-off-by: Enzo Matsumiya <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent e9d7761 commit f917f57

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

pager.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
#define DEFAULT_PAGER "less"
99
#endif
1010

11-
static struct child_process pager_process = CHILD_PROCESS_INIT;
11+
static struct child_process pager_process;
1212
static const char *pager_program;
1313

1414
/* Is the value coming back from term_columns() just a guess? */
@@ -124,6 +124,8 @@ void setup_pager(void)
124124

125125
setenv("GIT_PAGER_IN_USE", "true", 1);
126126

127+
child_process_init(&pager_process);
128+
127129
/* spawn the pager */
128130
prepare_pager_args(&pager_process, pager);
129131
pager_process.in = -1;

t/t7006-pager.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -786,4 +786,9 @@ test_expect_success TTY 'git returns SIGPIPE on propagated signals from pager' '
786786
test_path_is_file pager-used
787787
'
788788

789+
test_expect_success TTY 'non-existent pager doesnt cause crash' '
790+
test_config pager.show invalid-pager &&
791+
test_terminal git show
792+
'
793+
789794
test_done

0 commit comments

Comments
 (0)