Skip to content

Commit b28fb93

Browse files
committed
Merge branch 'ps/build-sign-compare'
Last-minute fix for a regression in "git blame --abbrev=<length>" when insane <length> is specified; we used to correctly cap it to the hash output length but broke it during the cycle. * ps/build-sign-compare: builtin/blame: fix out-of-bounds write with blank boundary commits builtin/blame: fix out-of-bounds read with excessive `--abbrev`
2 parents 3ae3564 + e7fb2ca commit b28fb93

File tree

2 files changed

+31
-4
lines changed

2 files changed

+31
-4
lines changed

builtin/blame.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -489,9 +489,9 @@ static void emit_other(struct blame_scoreboard *sb, struct blame_entry *ent, int
489489
fputs(color, stdout);
490490

491491
if (suspect->commit->object.flags & UNINTERESTING) {
492-
if (blank_boundary)
493-
memset(hex, ' ', length);
494-
else if (!(opt & OUTPUT_ANNOTATE_COMPAT)) {
492+
if (blank_boundary) {
493+
memset(hex, ' ', strlen(hex));
494+
} else if (!(opt & OUTPUT_ANNOTATE_COMPAT)) {
495495
length--;
496496
putchar('^');
497497
}
@@ -505,7 +505,8 @@ static void emit_other(struct blame_scoreboard *sb, struct blame_entry *ent, int
505505
length--;
506506
putchar('?');
507507
}
508-
fwrite(hex, 1, length, stdout);
508+
509+
printf("%.*s", (int)(length < GIT_MAX_HEXSZ ? length : GIT_MAX_HEXSZ), hex);
509510
if (opt & OUTPUT_ANNOTATE_COMPAT) {
510511
const char *name;
511512
if (opt & OUTPUT_SHOW_EMAIL)

t/t8002-blame.sh

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,32 @@ test_expect_success '--no-abbrev works like --abbrev with full length' '
126126
check_abbrev $hexsz --no-abbrev
127127
'
128128

129+
test_expect_success 'blame --abbrev gets truncated' '
130+
check_abbrev $hexsz --abbrev=9000 HEAD
131+
'
132+
133+
test_expect_success 'blame --abbrev gets truncated with boundary commit' '
134+
check_abbrev $hexsz --abbrev=9000 ^HEAD
135+
'
136+
137+
test_expect_success 'blame --abbrev -b truncates the blank boundary' '
138+
# Note that `--abbrev=` always gets incremented by 1, which is why we
139+
# expect 11 leading spaces and not 10.
140+
cat >expect <<-EOF &&
141+
$(printf "%0.s " $(test_seq 11)) (<[email protected]> 2005-04-07 15:45:13 -0700 1) abbrev
142+
EOF
143+
git blame -b --abbrev=10 ^HEAD -- abbrev.t >actual &&
144+
test_cmp expect actual
145+
'
146+
147+
test_expect_success 'blame with excessive --abbrev and -b culls to hash length' '
148+
cat >expect <<-EOF &&
149+
$(printf "%0.s " $(test_seq $hexsz)) (<[email protected]> 2005-04-07 15:45:13 -0700 1) abbrev
150+
EOF
151+
git blame -b --abbrev=9000 ^HEAD -- abbrev.t >actual &&
152+
test_cmp expect actual
153+
'
154+
129155
test_expect_success '--exclude-promisor-objects does not BUG-crash' '
130156
test_must_fail git blame --exclude-promisor-objects one
131157
'

0 commit comments

Comments
 (0)