summaryrefslogtreecommitdiff
path: root/grep.c
diff options
context:
space:
mode:
authorJeff King <peff@peff.net>2016-02-22 22:44:28 (GMT)
committerJunio C Hamano <gitster@pobox.com>2016-02-22 22:51:09 (GMT)
commit3733e6946465d4a3a1d89026a5ec911d3af339ab (patch)
tree687aa6252267a70f503904d635f44600eb3bfae7 /grep.c
parentb32fa95fd8293ebfecb2b7b6c8d460579318f9fe (diff)
downloadgit-3733e6946465d4a3a1d89026a5ec911d3af339ab.zip
git-3733e6946465d4a3a1d89026a5ec911d3af339ab.tar.gz
git-3733e6946465d4a3a1d89026a5ec911d3af339ab.tar.bz2
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 'grep.c')
-rw-r--r--grep.c3
1 files changed, 1 insertions, 2 deletions
diff --git a/grep.c b/grep.c
index 7b2b96a..528b652 100644
--- a/grep.c
+++ b/grep.c
@@ -1741,7 +1741,7 @@ static int grep_source_load_file(struct grep_source *gs)
i = open(filename, O_RDONLY);
if (i < 0)
goto err_ret;
- data = xmalloc(size + 1);
+ data = xmallocz(size);
if (st.st_size != read_in_full(i, data, size)) {
error(_("'%s': short read %s"), filename, strerror(errno));
close(i);
@@ -1749,7 +1749,6 @@ static int grep_source_load_file(struct grep_source *gs)
return -1;
}
close(i);
- data[size] = 0;
gs->buf = data;
gs->size = size;