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

[PATCH 1/5] archive-tar: guard get_path_prefix against zero-length input

From
Johannes Schindelin via GitGitGadget <gitgitgadget@gmail.com>
Date
Oct 14, 2026, 06:09 UTC
Message-ID
<dcd319476add64e892e517faa71966f09010029a.1791958171.git.gitgitgadget@gmail.com>
In-Reply-To
<pull.2253.git.1791958171.gitgitgadget@gmail.com>
From: Johannes Schindelin <johannes.schindelin@gmx.de>

The `get_path_prefix()` function uses a `do ... while()` loop that unconditionally decrements the `size_t` index variable before checking the loop condition. When that variable is 0 on entry, the decrement wraps to `SIZE_MAX`, and `path[SIZE_MAX]` is an out-of-bounds read.

The current caller (`write_tar_entry()`) only invokes this function when `pathlen > sizeof(header.name)` i.e. when `pathlen` is at least 100. Therefore, the bug is unreachable in practice.

However, the function's interface accepts arbitrary pathlen and maxlen values, and a future caller or a maxlen of 0 could trigger the wrap, so let's add a check for that.

Pointed out by Coverity.
Assisted-by: Claude Opus 4.6
Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
---
 archive-tar.c | 2 ++
 1 file changed, 2 insertions(+)
diff --git a/archive-tar.c b/archive-tar.c
index a1c66024d4..2da6e55484 100644
--- a/archive-tar.c
+++ b/archive-tar.c
@@ -210,6 +210,8 @@ static size_t get_path_prefix(const char *path, size_t pathlen, size_t maxlen)
 		i--;
 	if (i > maxlen)
 		i = maxlen;
+	if (!i)
+		return 0;
 	do {
 		i--;
 	} while (i > 0 && path[i] != '/');
-- 
gitgitgadget
Previous: Johannes Schindelin via GitGitGadgetNext: Johannes Schindelin via GitGitGadget
Message 2 of 16 in “Address overflows pointed out by Coverity”
  1. 0/5 Address overflows pointed out by CoverityJohannes Schindelin via GitGitGadget, Oct 14, 2026
  2. 1/5 archive-tar: guard get_path_prefix against zero-length inputJohannes Schindelin via GitGitGadget, Oct 14, 2026
  3. 2/5 http: use `size_t` for byte position in fwrite_sha1_fileJohannes Schindelin via GitGitGadget, Oct 14, 2026
  4. Patrick SteinhardtOct 16, 2026
  5. Junio C HamanoOct 16, 2026
  6. Simon RichterOct 17, 2026
  7. Junio C HamanoOct 17, 2026
  8. Junio C HamanoOct 17, 2026
  9. 3/5 wrapper: reject insanely large byte counts when reading/writingJohannes Schindelin via GitGitGadget, Oct 14, 2026
  10. Phillip WoodOct 14, 2026
  11. 4/5 git-compat-util: add `signed_mult_overflows` and `mult_overflows` macrosJohannes Schindelin via GitGitGadget, Oct 14, 2026
  12. Phillip WoodOct 15, 2026
  13. 5/5 date: avoid overflow in approxidate relative-time multiplicationJohannes Schindelin via GitGitGadget, Oct 14, 2026
  14. Junio C HamanoOct 14, 2026
  15. Phillip WoodOct 15, 2026
  16. Harald NordgrenOct 16, 2026

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.