diff options
author | Jeff King <peff@peff.net> | 2016-02-22 17:44:28 -0500 |
---|---|---|
committer | Junio C Hamano <gitster@pobox.com> | 2016-02-22 14:51:09 -0800 |
commit | 3733e6946465d4a3a1d89026a5ec911d3af339ab (patch) | |
tree | 687aa6252267a70f503904d635f44600eb3bfae7 /combine-diff.c | |
parent | b32fa95fd8293ebfecb2b7b6c8d460579318f9fe (diff) | |
download | git-3733e6946465d4a3a1d89026a5ec911d3af339ab.tar.gz |
use xmallocz to avoid size arithmetic
We frequently allocate strings as xmalloc(len + 1), where
the extra 1 is for the NUL terminator. This can be done more
simply with xmallocz, which also checks for integer
overflow.
There's no case where switching xmalloc(n+1) to xmallocz(n)
is wrong; the result is the same length, and malloc made no
guarantees about what was in the buffer anyway. But in some
cases, we can stop manually placing NUL at the end of the
allocated buffer. But that's only safe if it's clear that
the contents will always fill the buffer.
In each case where this patch does so, I manually examined
the control flow, and I tried to err on the side of caution.
Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'combine-diff.c')
-rw-r--r-- | combine-diff.c | 4 |
1 files changed, 1 insertions, 3 deletions
diff --git a/combine-diff.c b/combine-diff.c index a698016db7..890c4157bd 100644 --- a/combine-diff.c +++ b/combine-diff.c @@ -1043,7 +1043,7 @@ static void show_patch_diff(struct combine_diff_path *elem, int num_parent, elem->mode = canon_mode(S_IFLNK); result_size = len; - result = xmalloc(len + 1); + result = xmallocz(len); done = read_in_full(fd, result, len); if (done < 0) @@ -1051,8 +1051,6 @@ static void show_patch_diff(struct combine_diff_path *elem, int num_parent, else if (done < len) die("early EOF '%s'", elem->path); - result[len] = 0; - /* If not a fake symlink, apply filters, e.g. autocrlf */ if (is_file) { struct strbuf buf = STRBUF_INIT; |