Skip to content

Commit 31e2a73

Browse files
committed
archive-tar: guard get_path_prefix against zero-length input
get_path_prefix() uses a do-while loop that unconditionally decrements the size_t index variable i before checking the loop condition. When i 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 at line 281) only invokes this function when pathlen > sizeof(header.name) (100 bytes), so i is always at least 100 on entry and 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. Add an early return for i == 0 before the do-while loop. Pointed out by Coverity. Assisted-by: Claude Opus 4.6 Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
1 parent c472c05 commit 31e2a73

1 file changed

Lines changed: 2 additions & 0 deletions

File tree

archive-tar.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,8 @@ static size_t get_path_prefix(const char *path, size_t pathlen, size_t maxlen)
210210
i--;
211211
if (i > maxlen)
212212
i = maxlen;
213+
if (!i)
214+
return 0;
213215
do {
214216
i--;
215217
} while (i > 0 && path[i] != '/');

0 commit comments

Comments
 (0)