{"thread":{"id":"56438","subject":"[BUG?] git range-diff -Ix @{1}... segfaults","startedAt":"2021-09-03T18:49:52Z","lastAt":"2021-09-07T20:05:18Z","messageCount":3,"participants":["Junio C Hamano","René Scharfe"],"isPatch":false,"patchVersion":null,"patchTotal":null},"messages":[{"id":"434668","messageId":"xmqqbl59fq9i.fsf@gitster.g","threadId":"56438","inReplyTo":null,"subject":"[BUG?] git range-diff -Ix @{1}... segfaults","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2021-09-03T18:49:45Z","receivedAt":"2021-09-03T18:49:52Z","isPatch":false,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"It does not seem to trigger with an empty range, e.g.\n\n    $ git range-diff -Ix HEAD...\n    $ git range-diff -Ix HEAD~...\n    -:  ---------- > 1:  0a0bc7d03a scalar: accept -C and -c opt...\n\nbut when it needs real comparison, the -I seems to kill the command\nquite easily.\n\nTHanks.\n\n    \n"},{"id":"434680","messageId":"7ac58fa9-f0f3-53bc-e51b-de9cd4efdb29@web.de","threadId":"56438","inReplyTo":"xmqqbl59fq9i.fsf@gitster.g","subject":"Re: [BUG?] git range-diff -Ix @{1}... segfaults","fromName":"René Scharfe","fromEmail":"l.s.r@web.de","sentAt":"2021-09-04T07:50:58Z","receivedAt":"2021-09-04T07:56:13Z","isPatch":false,"sender":{"key":"l.s.r@web.de","avatar":"https://avatars.githubusercontent.com/u/26122331?v=4"},"body":"Am 03.09.21 um 20:49 schrieb Junio C Hamano:\n> It does not seem to trigger with an empty range, e.g.\n>\n>     $ git range-diff -Ix HEAD...\n>     $ git range-diff -Ix HEAD~...\n>     -:  ---------- > 1:  0a0bc7d03a scalar: accept -C and -c opt...\n>\n> but when it needs real comparison, the -I seems to kill the command\n> quite easily.\n\nReverting c45dc9cf30 (diff: plug memory leak from regcomp() on\n{log,diff} -I, 2021-02-11) fixes the segfault.\n\ndiff.c::diff_free() frees the ignore_regex member of a struct\ndiff_options, but doesn't reset the ignore_regex_nr nor clears the\npointer.  It is called from diff.c::diff_flush().\n\nrange-diff.c::output() calls diff.c::diff_flush() in a loop with the\nsame struct diff_options (via range-diff.c::patch_diff()).\n\nSo the second iteration of that loop tries to use the already freed\nignore regexes.  Here's a patch for that:\n\n--- >8 ---\nSubject: [PATCH] range-diff: avoid segfault with -I\n\noutput() reuses the same struct diff_options for multiple calls of\ndiff_flush().  Set the option no_free to instruct it to keep the\nignore regexes between calls and release them explicitly at the end.\n\nSigned-off-by: René Scharfe <l.s.r@web.de>\n---\nTest missing because I couldn't see any effect of -I on range-diff.\n\n range-diff.c | 3 +++\n 1 file changed, 3 insertions(+)\n\ndiff --git a/range-diff.c b/range-diff.c\nindex e731525e66..cac89a2f4f 100644\n--- a/range-diff.c\n+++ b/range-diff.c\n@@ -482,6 +482,7 @@ static void output(struct string_list *a, struct string_list *b,\n \telse\n \t\tdiff_setup(&opts);\n\n+\topts.no_free = 1;\n \tif (!opts.output_format)\n \t\topts.output_format = DIFF_FORMAT_PATCH;\n \topts.flags.suppress_diff_headers = 1;\n@@ -542,6 +543,8 @@ static void output(struct string_list *a, struct string_list *b,\n \tstrbuf_release(&buf);\n \tstrbuf_release(&dashes);\n \tstrbuf_release(&indent);\n+\topts.no_free = 0;\n+\tdiff_free(&opts);\n }\n\n int show_range_diff(const char *range1, const char *range2,\n--\n2.33.0\n\n"},{"id":"434925","messageId":"xmqq7dfsb18n.fsf@gitster.g","threadId":"56438","inReplyTo":"7ac58fa9-f0f3-53bc-e51b-de9cd4efdb29@web.de","subject":"Re: [BUG?] git range-diff -Ix @{1}... segfaults","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2021-09-07T20:05:12Z","receivedAt":"2021-09-07T20:05:18Z","isPatch":false,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"René Scharfe <l.s.r@web.de> writes:\n\n> range-diff.c::output() calls diff.c::diff_flush() in a loop with the\n> same struct diff_options (via range-diff.c::patch_diff()).\n\nWoooo, that's, eh, unexpected.\n\n> So the second iteration of that loop tries to use the already freed\n> ignore regexes.  Here's a patch for that:\n\nThanks.\n\n>\n> --- >8 ---\n> Subject: [PATCH] range-diff: avoid segfault with -I\n>\n> output() reuses the same struct diff_options for multiple calls of\n> diff_flush().  Set the option no_free to instruct it to keep the\n> ignore regexes between calls and release them explicitly at the end.\n>\n> Signed-off-by: René Scharfe <l.s.r@web.de>\n> ---\n> Test missing because I couldn't see any effect of -I on range-diff.\n\nI wanted to omit my sign-off when comparing a previous series and\nthe new series (iow, I expected -I\"^Signed-off-by: me\" to apply to\nthe outer diff, not the inner \"generation of patches to be compared\").\n\n\n>  range-diff.c | 3 +++\n>  1 file changed, 3 insertions(+)\n>\n> diff --git a/range-diff.c b/range-diff.c\n> index e731525e66..cac89a2f4f 100644\n> --- a/range-diff.c\n> +++ b/range-diff.c\n> @@ -482,6 +482,7 @@ static void output(struct string_list *a, struct string_list *b,\n>  \telse\n>  \t\tdiff_setup(&opts);\n>\n> +\topts.no_free = 1;\n>  \tif (!opts.output_format)\n>  \t\topts.output_format = DIFF_FORMAT_PATCH;\n>  \topts.flags.suppress_diff_headers = 1;\n> @@ -542,6 +543,8 @@ static void output(struct string_list *a, struct string_list *b,\n>  \tstrbuf_release(&buf);\n>  \tstrbuf_release(&dashes);\n>  \tstrbuf_release(&indent);\n> +\topts.no_free = 0;\n> +\tdiff_free(&opts);\n>  }\n>\n>  int show_range_diff(const char *range1, const char *range2,\n> --\n> 2.33.0\n"}]}