hook API: don't segfault on strbuf_addf() to NULL "out"
A message that is not in the archive (sent off-list or not yet received): [email protected]
PATCH 1/1 hook API: don't segfault on strbuf_addf() to NULL "out"
Fix a logic error in a082345372e (hook API: fix v2.36.0 regression:
hooks should be connected to a TTY, 2022-06-07). When it started using the "ungroup" API added in fd3aaf53f71 (run-command: add an "ungroup" option to run_process_parallel(), 2022-06-07) it should have made the same sort of change that fd3aaf53f71 itself made in "t/helper/test-run-command.c".
The correct way to emit this "Couldn't start" output with "ungroup" would be:
fprintf(stderr, _("Couldn't start hook '%s'\n"), hook_path);But we should instead remove the emitting of this output. As the added test shows we already emit output when we can't run the child. The "cannot run" output here is emitted by run-command.c's child_err_spew().
So the addition of the "Couldn't start hook" output here in 96e7225b310 (hook: add 'run' subcommand, 2021-12-22) was always redundant. For the pre-commit hook we'll now emit exactly the same output as we did before f443246b9f2 (commit: convert {pre-commit,prepare-commit-msg} hook to hook.h, 2021-12-22) (and likewise for others).
We could at this point add this to the pick_next_hook() callbacks in hook.c:
assert(!out); assert(!*pp_task_cb);
And this to notify_start_failure() and notify_hook_finished() (in the latter case the parameter is called "pp_task_cp"):
assert(!out); assert(!pp_task_cb);
But let's leave any such instrumentation for some eventual cleanup of the "ungroup" API.
---
As suspected I needed to bounce this off the CI once to due to the OS-specific messaging around failed hooks, it passes now:
https://github.com/avar/git/actions/runs/2802782493
hook.c | 7 ------- t/t1800-hook.sh | 26 ++++++++++++++++++++++++++ 2 files changed, 26 insertions(+), 7 deletions(-)
hook.c +0 -7
@@ -62,9 +62,6 @@ static int pick_next_hook(struct child_process *cp,strvec_push (&cp->args, hook_path);strvec_pushv (&cp->args, hook_cb->options->args.v); -/* Provide context for errors if necessary */ - *pp_task_cb = (char *)hook_path; -/* * This pick_next_hook() will be called again, we're only * running one hook, so indicate that no more work will be
@@ -80,13 +77,9 @@ static int notify_start_failure(struct strbuf *out, void *pp_task_cp) {struct hook_cb_data *hook_cb = pp_cb; -const char *hook_path = pp_task_cp; hook_cb->rc |=1 ; -strbuf_addf (out,_ ("Couldn't start hook '%s'\n" ), - hook_path); -return 1 ; }
t/t1800-hook.sh +26 -0
@@ -151,4 +151,30 @@ test_expect_success TTY 'git commit: stdout and stderr are connected to a TTY' ' test_hook_tty commit -m"B.new" ' +test_expect_success 'git hook run a hook with a bad shebang' ' + test_when_finished "rm -rf bad-hooks" && + mkdir bad-hooks && + write_script bad-hooks/test-hook "/bad/path/no/spaces" </dev/null && + + # TODO: We should emit the same (or at least a more similar) + # error on Windows and !Windows. See the OS-specific code in + # start_command() + if test_have_prereq !WINDOWS + then + cat >expect <<-\EOF + fatal: cannot run bad-hooks/test-hook: ... + EOF + else + cat >expect <<-\EOF + error: cannot spawn bad-hooks/test-hook: ... + EOF + fi && + test_expect_code 1 git \ + -c core.hooksPath=bad-hooks \ + hook run test-hook >out 2>err && + test_must_be_empty out && + sed -e "s/test-hook: .*/test-hook: .../" <err >actual && + test_cmp expect actual +' + test_done
Signature · 1 line
2.37.1.1283.g2c56c4202c5
Quoted 100 lines · Ævar Arnfjörð Bjarmason
Fix a logic error in
a082345372e(hook API: fix v2.36.0 regression:
hooks should be connected to a TTY, 2022-06-07). When it started using the "ungroup" API added infd3aaf53f71(run-command: add an "ungroup" option torun_process_parallel(), 2022-06-07) it should have made the same sort of change thatfd3aaf53f71itself made in "t/helper/test-run-command.c".The correct way to emit this "Couldn't start" output with "ungroup" would be:
fprintf(stderr, _("Couldn't start hook '%s'\n"), hook_path);But we should instead remove the emitting of this output. As the added test shows we already emit output when we can't run the child. The "cannot run" output here is emitted by run-command.c's
child_err_spew().So the addition of the "Couldn't start hook" output here in
96e7225b310(hook: add 'run' subcommand, 2021-12-22) was always redundant. For the pre-commit hook we'll now emit exactly the same output as we did beforef443246b9f2(commit: convert {pre-commit,prepare-commit-msg} hook tohook.h, 2021-12-22) (and likewise for others).We could at this point add this to the
pick_next_hook()callbacks inhook.c:assert(!out); assert(!*pp_task_cb);And this to
notify_start_failure()andnotify_hook_finished()(in the latter case the parameter is called "pp_task_cp"):assert(!out); assert(!pp_task_cb);But let's leave any such instrumentation for some eventual cleanup of the "ungroup" API.
Reported-by Ilya K <[email protected]>Signed-off-by Ævar Arnfjörð Bjarmason <[email protected]>---
As suspected I needed to bounce this off the CI once to due to the OS-specific messaging around failed hooks, it passes now:
https://github.com/avar/git/actions/runs/2802782493hook.c | 7 ------- t/t1800-hook.sh | 26 ++++++++++++++++++++++++++ 2 files changed, 26 insertions(+), 7 deletions(-)hook.c +0
-7@@ -62,9 +62,6 @@ static int pick_next_hook(struct child_process *cp,strvec_push (&cp->args, hook_path);strvec_pushv (&cp->args, hook_cb->options->args.v); -/* Provide context for errors if necessary */ - *pp_task_cb = (char *)hook_path; -/* * This pick_next_hook() will be called again, we're only * running one hook, so indicate that no more work will be @@ -80,13 +77,9 @@ static int notify_start_failure(struct strbuf *out, void *pp_task_cp) {struct hook_cb_data *hook_cb = pp_cb; -const char *hook_path = pp_task_cp; hook_cb->rc |=1 ; -strbuf_addf (out,_ ("Couldn't start hook '%s'\n" ), - hook_path); -return 1 ; }t/t1800-hook.sh +13
-0@@ -151,4 +151,30 @@ test_expect_success TTY 'git commit: stdout and stderr are connected to a TTY' ' test_hook_tty commit -m"B.new" ' +test_expect_success 'git hook run a hook with a bad shebang' ' + test_when_finished "rm -rf bad-hooks" && + mkdir bad-hooks && + write_script bad-hooks/test-hook "/bad/path/no/spaces" </dev/null && + + # TODO: We should emit the same (or at least a more similar) + # error on Windows and !Windows. See the OS-specific code in + # start_command() + if test_have_prereq !WINDOWS + then + cat >expect <<-\EOF + fatal: cannot run bad-hooks/test-hook: ... + EOF
t/t1800-hook.sh test_expect_success TTY 'git commit: stdout and stderr are connected to a TTY' ' · line 176
Quoted 10 lines
t/t1800-hook.sh +10
-0+ else + cat >expect <<-\EOF + error: cannot spawn bad-hooks/test-hook: ... + EOF + fi && + test_expect_code 1 git \ + -c core.hooksPath=bad-hooks \ + hook run test-hook >out 2>err && + test_must_be_empty out && + sed -e "s/test-hook: .*/test-hook: .../" <err >actual &&
If we're using "sed" here, can we also s/cannot run/cannot spawn/ in order to have the same expectation?
Otherwise, the fix looks sane to me (obviously, since I also suggest removing the line entirely).
Quoted 7 lines
t/t1800-hook.sh +3
-0+ test_cmp expect actual +' + test_doneSignature · 1 line
2.37.1.1283.g2c56c4202c5
Signature · 1 line
Danh
Quoted 29 lines · Đoàn Trần Công Danh
On 2022-08-05 16:15:33+0200, Ævar Arnfjörð Bjarmason <[email protected]> wrote:
[...]Quoted 13 lines
t/t1800-hook.sh +13
-0+test_expect_success 'git hook run a hook with a bad shebang' ' + test_when_finished "rm -rf bad-hooks" && + mkdir bad-hooks && + write_script bad-hooks/test-hook "/bad/path/no/spaces" </dev/null && + + # TODO: We should emit the same (or at least a more similar) + # error on Windows and !Windows. See the OS-specific code in + # start_command() + if test_have_prereq !WINDOWS + then + cat >expect <<-\EOF + fatal: cannot run bad-hooks/test-hook: ... + EOFt/t1800-hook.sh test_expect_success TTY 'git commit: stdout and stderr are connected to a TTY' ' · line 176
Quoted 10 lines
t/t1800-hook.sh +10
-0+ else + cat >expect <<-\EOF + error: cannot spawn bad-hooks/test-hook: ... + EOF + fi && + test_expect_code 1 git \ + -c core.hooksPath=bad-hooks \ + hook run test-hook >out 2>err && + test_must_be_empty out && + sed -e "s/test-hook: .*/test-hook: .../" <err >actual &&If we're using "sed" here, can we also s/cannot run/cannot spawn/ in order to have the same expectation?
Quoted 2 lines
Otherwise, the fix looks sane to me (obviously, since I also suggest removing the line entirely).
We could, but then we'd miss some weird regression where we changed the non-Windows message to "fatal: cannot spawn", which neither emit now.
We could of course do the "sed"-ing in an identical "test_have_prereq" block, but I think we'd just be back to square one then, and it would be more readable to just "cat" what we expect to happen there.
So I think I'd prefer to keep it as-is, it also makes a subsequent change where we unify these error messages more obvious, i.e. we'd keep the !WINDOWS branch of that if/else in thath case.
Quoted 1 line
Reviewed-by Đoàn Trần Công Danh <[email protected]>
Thanks!
Quoted 39 lines · Ævar Arnfjörð Bjarmason
Fix a logic error in
a082345372e(hook API: fix v2.36.0 regression:
hooks should be connected to a TTY, 2022-06-07). When it started using the "ungroup" API added infd3aaf53f71(run-command: add an "ungroup" option torun_process_parallel(), 2022-06-07) it should have made the same sort of change thatfd3aaf53f71itself made in "t/helper/test-run-command.c".The correct way to emit this "Couldn't start" output with "ungroup" would be:
fprintf(stderr, _("Couldn't start hook '%s'\n"), hook_path);But we should instead remove the emitting of this output. As the added test shows we already emit output when we can't run the child. The "cannot run" output here is emitted by run-command.c's
child_err_spew().So the addition of the "Couldn't start hook" output here in
96e7225b310(hook: add 'run' subcommand, 2021-12-22) was always redundant. For the pre-commit hook we'll now emit exactly the same output as we did beforef443246b9f2(commit: convert {pre-commit,prepare-commit-msg} hook tohook.h, 2021-12-22) (and likewise for others).We could at this point add this to the
pick_next_hook()callbacks inhook.c:assert(!out); assert(!*pp_task_cb);And this to
notify_start_failure()andnotify_hook_finished()(in the latter case the parameter is called "pp_task_cp"):assert(!out); assert(!pp_task_cb);But let's leave any such instrumentation for some eventual cleanup of the "ungroup" API.
Quoted 7 lines
Reported-by Ilya K <[email protected]>Signed-off-by Ævar Arnfjörð Bjarmason <[email protected]>---
As suspected I needed to bounce this off the CI once to due to the OS-specific messaging around failed hooks, it passes now:
https://github.com/avar/git/actions/runs/2802782493
Emily, other than the added code to deal with the difference in expected error message between Windows and other platforms, and the name of the temporary file used by the test to capture the raw error output, both in the t1800, everything is the same as the one you gave your Reviewed-by earlier, which I'll transplant while queuing this round.
Ævar, thanks for a quick fix. This will have to go on top of ab/hooks-regresion-fix topic and downmerged to 2.36 maintenance track.
Thanks, all.
Quoted 67 lines
hook.c | 7 ------- t/t1800-hook.sh | 26 ++++++++++++++++++++++++++ 2 files changed, 26 insertions(+), 7 deletions(-)hook.c +0
-7@@ -62,9 +62,6 @@ static int pick_next_hook(struct child_process *cp,strvec_push (&cp->args, hook_path);strvec_pushv (&cp->args, hook_cb->options->args.v); -/* Provide context for errors if necessary */ - *pp_task_cb = (char *)hook_path; -/* * This pick_next_hook() will be called again, we're only * running one hook, so indicate that no more work will be @@ -80,13 +77,9 @@ static int notify_start_failure(struct strbuf *out, void *pp_task_cp) {struct hook_cb_data *hook_cb = pp_cb; -const char *hook_path = pp_task_cp; hook_cb->rc |=1 ; -strbuf_addf (out,_ ("Couldn't start hook '%s'\n" ), - hook_path); -return 1 ; }t/t1800-hook.sh +26
-0@@ -151,4 +151,30 @@ test_expect_success TTY 'git commit: stdout and stderr are connected to a TTY' ' test_hook_tty commit -m"B.new" ' +test_expect_success 'git hook run a hook with a bad shebang' ' + test_when_finished "rm -rf bad-hooks" && + mkdir bad-hooks && + write_script bad-hooks/test-hook "/bad/path/no/spaces" </dev/null && + + # TODO: We should emit the same (or at least a more similar) + # error on Windows and !Windows. See the OS-specific code in + # start_command() + if test_have_prereq !WINDOWS + then + cat >expect <<-\EOF + fatal: cannot run bad-hooks/test-hook: ... + EOF + else + cat >expect <<-\EOF + error: cannot spawn bad-hooks/test-hook: ... + EOF + fi && + test_expect_code 1 git \ + -c core.hooksPath=bad-hooks \ + hook run test-hook >out 2>err && + test_must_be_empty out && + sed -e "s/test-hook: .*/test-hook: .../" <err >actual && + test_cmp expect actual +' + test_done