Skip to content

Commit 8cf5156

Browse files
vasildgitster
authored andcommitted
range-diff: fix a crash in parsing git-log output
`git range-diff` calls `git log` internally and tries to parse its output. But `git log` output can be customized by the user in their git config and for certain configurations either an error will be returned by `git range-diff` or it will crash. To fix this explicitly set the output format of the internally executed `git log` with `--pretty=medium`. Because that cancels `--notes`, add explicitly `--notes` at the end. Also, make sure we never crash in the same way - trying to dereference `util` which was never created and has remained NULL. It would happen if the first line of `git log` output does not begin with 'commit '. Alternative considered but discarded - somehow disable all git configs and behave as if no config is present in the internally executed `git log`, but that does not seem to be possible. GIT_CONFIG_NOSYSTEM is the closest to it, but even with that we would still read `.git/config`. Signed-off-by: Vasil Dimov <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent de49261 commit 8cf5156

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

range-diff.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ static int read_patches(const char *range, struct string_list *list,
6363
"--output-indicator-old=<",
6464
"--output-indicator-context=#",
6565
"--no-abbrev-commit",
66+
"--pretty=medium",
67+
"--notes",
6668
NULL);
6769
if (other_arg)
6870
argv_array_pushv(&cp.args, other_arg->argv);
@@ -106,6 +108,17 @@ static int read_patches(const char *range, struct string_list *list,
106108
continue;
107109
}
108110

111+
if (!util) {
112+
error(_("could not parse first line of `log` output: "
113+
"did not start with 'commit ': '%s'"),
114+
line);
115+
string_list_clear(list, 1);
116+
strbuf_release(&buf);
117+
strbuf_release(&contents);
118+
finish_command(&cp);
119+
return -1;
120+
}
121+
109122
if (starts_with(line, "diff --git")) {
110123
struct patch patch = { 0 };
111124
struct strbuf root = STRBUF_INIT;

t/t3206-range-diff.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -513,6 +513,16 @@ test_expect_success 'range-diff overrides diff.noprefix internally' '
513513
git -c diff.noprefix=true range-diff HEAD^...
514514
'
515515

516+
test_expect_success 'basic with modified format.pretty with suffix' '
517+
git -c format.pretty="format:commit %H%d%n" range-diff \
518+
master..topic master..unmodified
519+
'
520+
521+
test_expect_success 'basic with modified format.pretty without "commit "' '
522+
git -c format.pretty="format:%H%n" range-diff \
523+
master..topic master..unmodified
524+
'
525+
516526
test_expect_success 'range-diff compares notes by default' '
517527
git notes add -m "topic note" topic &&
518528
git notes add -m "unmodified note" unmodified &&

0 commit comments

Comments
 (0)