Skip to content

Commit 5015f01

Browse files
peffgitster
authored andcommitted
read_info_alternates: handle paths larger than PATH_MAX
This function assumes that the relative_base path passed into it is no larger than PATH_MAX, and writes into a fixed-size buffer. However, this path may not have actually come from the filesystem; for example, add_submodule_odb generates a path using a strbuf and passes it in. This is hard to trigger in practice, though, because the long submodule directory would have to exist on disk before we would try to open its info/alternates file. We can easily avoid the bug, though, by simply creating the filename on the heap. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent c29edfe commit 5015f01

File tree

1 file changed

+3
-6
lines changed

1 file changed

+3
-6
lines changed

sha1_file.c

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -377,15 +377,12 @@ void read_info_alternates(const char * relative_base, int depth)
377377
char *map;
378378
size_t mapsz;
379379
struct stat st;
380-
const char alt_file_name[] = "info/alternates";
381-
/* Given that relative_base is no longer than PATH_MAX,
382-
ensure that "path" has enough space to append "/", the
383-
file name, "info/alternates", and a trailing NUL. */
384-
char path[PATH_MAX + 1 + sizeof alt_file_name];
380+
char *path;
385381
int fd;
386382

387-
sprintf(path, "%s/%s", relative_base, alt_file_name);
383+
path = xstrfmt("%s/info/alternates", relative_base);
388384
fd = git_open_noatime(path);
385+
free(path);
389386
if (fd < 0)
390387
return;
391388
if (fstat(fd, &st) || (st.st_size == 0)) {

0 commit comments

Comments
 (0)