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 <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Jeff King committed
Aug 19, 2015 at 14:12 UTC
5015f01c12a45a1042c1aa6b6f7f6b62bfa00ade
1 file changed
+3
-6
sha1_file.c
+3
-6
@@ -377,15 +377,12 @@ void read_info_alternates(const char * relative_base, int depth)
377
char *map;
378
size_t mapsz;
379
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;
381
int fd;
382
387
- sprintf(path, "%s/%s", relative_base, alt_file_name);
383
+ path = xstrfmt("%s/info/alternates", relative_base);
384
fd = git_open_noatime(path);
385
+ free(path);
386
if (fd < 0)
387
return;
388
if (fstat(fd, &st) || (st.st_size == 0)) {