Skip to content

Commit 7add441

Browse files
peffgitster
authored andcommitted
fsck: properly bound "invalid tag name" error message
When we detect an invalid tag-name header in a tag object, like, "tag foo bar\n", we feed the pointer starting at "foo bar" to a printf "%s" formatter. This shows the name, as we want, but then it keeps printing the rest of the tag buffer, rather than stopping at the end of the line. Our tests did not notice because they look only for the matching line, but the bug is that we print much more than we wanted to. So we also adjust the test to be more exact. Note that when fscking tags with "index-pack --strict", this is even worse. index-pack does not add a trailing NUL-terminator after the object, so we may actually read past the buffer and print uninitialized memory. Running t5302 with valgrind does notice the bug for that reason. Signed-off-by: Jeff King <[email protected]> Acked-by: Johannes Schindelin <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent f99b7af commit 7add441

File tree

2 files changed

+8
-3
lines changed

2 files changed

+8
-3
lines changed

fsck.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,8 @@ static int fsck_tag_buffer(struct tag *tag, const char *data,
423423
}
424424
strbuf_addf(&sb, "refs/tags/%.*s", (int)(eol - buffer), buffer);
425425
if (check_refname_format(sb.buf, 0))
426-
error_func(&tag->object, FSCK_WARN, "invalid 'tag' name: %s", buffer);
426+
error_func(&tag->object, FSCK_WARN, "invalid 'tag' name: %.*s",
427+
(int)(eol - buffer), buffer);
427428
buffer = eol + 1;
428429

429430
if (!skip_prefix(buffer, "tagger ", &buffer))

t/t1450-fsck.sh

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -209,8 +209,12 @@ test_expect_success 'tag with incorrect tag name & missing tagger' '
209209
echo $tag >.git/refs/tags/wrong &&
210210
test_when_finished "git update-ref -d refs/tags/wrong" &&
211211
git fsck --tags 2>out &&
212-
grep "invalid .tag. name" out &&
213-
grep "expected .tagger. line" out
212+
213+
cat >expect <<-EOF &&
214+
warning in tag $tag: invalid '\''tag'\'' name: wrong name format
215+
warning in tag $tag: invalid format - expected '\''tagger'\'' line
216+
EOF
217+
test_cmp expect out
214218
'
215219

216220
test_expect_success 'cleaned up' '

0 commit comments

Comments
 (0)