git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 2/4] archive-tar: fix pax extended header length calculation

From
René Scharfe <l.s.r@web.de>
Date
Aug 17, 2019, 16:24 UTC
Message-ID
<4b04cf3f-966a-3aa9-6a30-7ac3a234e1ed@web.de>
In-Reply-To
<c77a0081-93ab-7b97-e322-9616f6e7f86c@web.de>

A pax extended header records starts with a decimal number. Its value is the length of the whole record, including its own length.

The calculation of that number if 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 ae64bbc18c ("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>
---
 archive-tar.c                   | 2 +-
 t/t5004-archive-corner-cases.sh | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
diff --git a/archive-tar.c b/archive-tar.c
index 355c8142c6..4395a29ffb 100644
--- a/archive-tar.c
+++ b/archive-tar.c
@@ -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);
diff --git a/t/t5004-archive-corner-cases.sh b/t/t5004-archive-corner-cases.sh
index 2f15d1899d..4966a74b4d 100755
--- a/t/t5004-archive-corner-cases.sh
+++ b/t/t5004-archive-corner-cases.sh
@@ -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 &&
--
2.23.0
Previous: René ScharfeNext: Eric Sunshine
Message 3 of 7 in “fix pax extended header length calculation”
  1. 0/4 fix pax extended header length calculationRené Scharfe, Aug 17, 2019
  2. 1/4 archive-tar: report wrong pax extended header lengthRené Scharfe, Aug 17, 2019
  3. 2/4 archive-tar: fix pax extended header length calculationRené Scharfe, Aug 17, 2019
  4. Eric SunshineAug 17, 2019
  5. 3/4 archive-tar: use size_t in strbuf_append_ext_header()René Scharfe, Aug 17, 2019
  6. 4/4 archive-tar: turn length miscalculation warning into BUGRené Scharfe, Aug 17, 2019
  7. brian m. carlsonAug 17, 2019

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.