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 /pack-bitmap-write.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 'pack-bitmap-write.c')
-rw-r--r-- | pack-bitmap-write.c | 12 |
1 files changed, 7 insertions, 5 deletions
diff --git a/pack-bitmap-write.c b/pack-bitmap-write.c index 44492c346c..e313f4f2bc 100644 --- a/pack-bitmap-write.c +++ b/pack-bitmap-write.c @@ -508,16 +508,16 @@ void bitmap_writer_finish(struct pack_idx_entry **index, const char *filename, uint16_t options) { - static char tmp_file[PATH_MAX]; static uint16_t default_version = 1; static uint16_t flags = BITMAP_OPT_FULL_DAG; + struct strbuf tmp_file = STRBUF_INIT; struct sha1file *f; struct bitmap_disk_header header; - int fd = odb_mkstemp(tmp_file, sizeof(tmp_file), "pack/tmp_bitmap_XXXXXX"); + int fd = odb_mkstemp(&tmp_file, "pack/tmp_bitmap_XXXXXX"); - f = sha1fd(fd, tmp_file); + f = sha1fd(fd, tmp_file.buf); memcpy(header.magic, BITMAP_IDX_SIGNATURE, sizeof(BITMAP_IDX_SIGNATURE)); header.version = htons(default_version); @@ -537,9 +537,11 @@ void bitmap_writer_finish(struct pack_idx_entry **index, sha1close(f, NULL, CSUM_FSYNC); - if (adjust_shared_perm(tmp_file)) + if (adjust_shared_perm(tmp_file.buf)) die_errno("unable to make temporary bitmap file readable"); - if (rename(tmp_file, filename)) + if (rename(tmp_file.buf, filename)) die_errno("unable to rename temporary bitmap file to '%s'", filename); + + strbuf_release(&tmp_file); } |