path: root/builtin/apply.c
diff options
authorJeff King <>2016-02-22 22:44:35 (GMT)
committerJunio C Hamano <>2016-02-22 22:51:09 (GMT)
commit50a6c8efa2bbeddf46ca34c7765024108202e04b (patch)
tree0c189695ed6ad8349527cb03d326ef3fb39707cf /builtin/apply.c
parent96ffc06f72f693d80f05059a1f0e5ca9007d5f1b (diff)
use st_add and st_mult for allocation size computation
If our size computation overflows size_t, we may allocate a much smaller buffer than we expected and overflow it. It's probably impossible to trigger an overflow in most of these sites in practice, but it is easy enough convert their additions and multiplications into overflow-checking variants. This may be fixing real bugs, and it makes auditing the code easier. Signed-off-by: Jeff King <> Signed-off-by: Junio C Hamano <>
Diffstat (limited to 'builtin/apply.c')
1 files changed, 1 insertions, 1 deletions
diff --git a/builtin/apply.c b/builtin/apply.c
index deb1364..0db6d14 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -2632,7 +2632,7 @@ static void update_image(struct image *img,
insert_count = postimage->len;
/* Adjust the contents */
- result = xmalloc(img->len + insert_count - remove_count + 1);
+ result = xmalloc(st_add3(st_sub(img->len, remove_count), insert_count, 1));
memcpy(result, img->buf, applied_at);
memcpy(result + applied_at, postimage->buf, postimage->len);
memcpy(result + applied_at + postimage->len,