]> git.ipfire.org Git - thirdparty/git.git/commitdiff
pager: fix crash when pager program doesn't exist
authorEnzo Matsumiya <ematsumiya@suse.de>
Thu, 25 Nov 2021 00:02:39 +0000 (21:02 -0300)
committerJunio C Hamano <gitster@pobox.com>
Thu, 25 Nov 2021 00:14:10 +0000 (16:14 -0800)
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 <ematsumiya@suse.de>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
pager.c
t/t7006-pager.sh

diff --git a/pager.c b/pager.c
index 52f27a6765c8def12d66066d5ef409bd7b1f9de5..27877f8ebbc1b5f6e93717270db0757ad787ba46 100644 (file)
--- a/pager.c
+++ b/pager.c
@@ -8,7 +8,7 @@
 #define DEFAULT_PAGER "less"
 #endif
 
-static struct child_process pager_process = CHILD_PROCESS_INIT;
+static struct child_process pager_process;
 static const char *pager_program;
 
 /* Is the value coming back from term_columns() just a guess? */
@@ -124,6 +124,8 @@ void setup_pager(void)
 
        setenv("GIT_PAGER_IN_USE", "true", 1);
 
+       child_process_init(&pager_process);
+
        /* spawn the pager */
        prepare_pager_args(&pager_process, pager);
        pager_process.in = -1;
index 0e7cf75435eca107fc17b836fa939d3378e983a4..eed680a2b1eefedcd55007ebab64e8bc9bcf7990 100755 (executable)
@@ -786,4 +786,9 @@ test_expect_success TTY 'git returns SIGPIPE on propagated signals from pager' '
        test_path_is_file pager-used
 '
 
+test_expect_success TTY 'non-existent pager doesnt cause crash' '
+       test_config pager.show invalid-pager &&
+       test_terminal git show
+'
+
 test_done