Skip to content

Commit

Permalink
archive-tar: fix pax extended header length calculation
Browse files Browse the repository at this point in the history
A pax extended header record starts with a decimal number.  Its value
is the length of the whole record, including its own length.

The calculation of that number in strbuf_append_ext_header() is off by
one in case the length of the rest is close to a higher order of
magnitude.  This affects paths and link targets a bit shorter than 1000,
10000, 100000 etc. characters -- paths with a length of up to 100 fit
into the tar header and don't need a pax extended header.

The mistake has been present since the function was added by ae64bbc
("tar-tree: Introduce write_entry()", 2006-03-25).

Account for digits added to len during the loop and keep incrementing
until we have enough space for len and the rest.  The crucial change is
to check against the current value of len before each iteration, instead
of against its value before the loop.

Signed-off-by: René Scharfe <l.s.r@web.de>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
  • Loading branch information
rscharfe authored and gitster committed Aug 19, 2019
1 parent 4060c19 commit 82a46af
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 2 deletions.
2 changes: 1 addition & 1 deletion archive-tar.c
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ static void strbuf_append_ext_header(struct strbuf *sb, const char *keyword,

/* "%u %s=%s\n" */
len = 1 + 1 + strlen(keyword) + 1 + valuelen + 1;
for (tmp = len; tmp > 9; tmp /= 10)
for (tmp = 1; len / 10 >= tmp; tmp *= 10)
len++;

strbuf_grow(sb, len);
Expand Down
2 changes: 1 addition & 1 deletion t/t5004-archive-corner-cases.sh
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ build_tree() {
' "$1"
}

test_expect_failure 'tar archive with long paths' '
test_expect_success 'tar archive with long paths' '
blob=$(echo foo | git hash-object -w --stdin) &&
tree=$(build_tree $blob | git mktree) &&
git archive -o long_paths.tar $tree 2>stderr &&
Expand Down

0 comments on commit 82a46af

Please sign in to comment.