diff options
author | Jeff King <peff@peff.net> | 2017-03-28 15:45:43 -0400 |
---|---|---|
committer | Junio C Hamano <gitster@pobox.com> | 2017-03-28 15:28:04 -0700 |
commit | 594fa9998c41277c579a94657100fa303160aa7e (patch) | |
tree | c3cf4c5d51f96a603b4244e14316ca59c9300075 /fast-import.c | |
parent | 892e723afd2b5696e4d75280e730bf9f1ea92329 (diff) | |
download | git-594fa9998c41277c579a94657100fa303160aa7e.tar.gz |
odb_mkstemp: write filename into strbuf
The odb_mkstemp() function expects the caller to provide a
fixed buffer to write the resulting tempfile name into. But
it creates the template using snprintf without checking the
return value. This means we could silently truncate the
filename.
In practice, it's unlikely that the truncation would end in
the template-pattern that mkstemp needs to open the file. So
we'd probably end up failing either way, unless the path was
specially crafted.
The simplest fix would be to notice the truncation and die.
However, we can observe that most callers immediately
xstrdup() the result anyway. So instead, let's switch to
using a strbuf, which is easier for them (and isn't a big
deal for the other 2 callers, who can just strbuf_release
when they're done with it).
Note that many of the callers used static buffers, but this
was purely to avoid putting a large buffer on the stack. We
never passed the static buffers out of the function, so
there's no complicated memory handling we need to change.
Signed-off-by: Jeff King <peff@peff.net>
Diffstat (limited to 'fast-import.c')
-rw-r--r-- | fast-import.c | 9 |
1 files changed, 5 insertions, 4 deletions
diff --git a/fast-import.c b/fast-import.c index f6f416f20a..1ea3a08609 100644 --- a/fast-import.c +++ b/fast-import.c @@ -890,14 +890,15 @@ static struct tree_content *dup_tree_content(struct tree_content *s) static void start_packfile(void) { - static char tmp_file[PATH_MAX]; + struct strbuf tmp_file = STRBUF_INIT; struct packed_git *p; struct pack_header hdr; int pack_fd; - pack_fd = odb_mkstemp(tmp_file, sizeof(tmp_file), - "pack/tmp_pack_XXXXXX"); - FLEX_ALLOC_STR(p, pack_name, tmp_file); + pack_fd = odb_mkstemp(&tmp_file, "pack/tmp_pack_XXXXXX"); + FLEX_ALLOC_STR(p, pack_name, tmp_file.buf); + strbuf_release(&tmp_file); + p->pack_fd = pack_fd; p->do_not_close = 1; pack_file = sha1fd(pack_fd, p->pack_name); |