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 <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
committed by
Junio C Hamano
parent
96ffc06f72
commit
50a6c8efa2
@ -189,11 +189,11 @@ static struct lline *coalesce_lines(struct lline *base, int *lenbase,
|
||||
* - Else if we have NEW, insert newend lline into base and
|
||||
* consume newend
|
||||
*/
|
||||
lcs = xcalloc(origbaselen + 1, sizeof(int*));
|
||||
directions = xcalloc(origbaselen + 1, sizeof(enum coalesce_direction*));
|
||||
lcs = xcalloc(st_add(origbaselen, 1), sizeof(int*));
|
||||
directions = xcalloc(st_add(origbaselen, 1), sizeof(enum coalesce_direction*));
|
||||
for (i = 0; i < origbaselen + 1; i++) {
|
||||
lcs[i] = xcalloc(lennew + 1, sizeof(int));
|
||||
directions[i] = xcalloc(lennew + 1, sizeof(enum coalesce_direction));
|
||||
lcs[i] = xcalloc(st_add(lennew, 1), sizeof(int));
|
||||
directions[i] = xcalloc(st_add(lennew, 1), sizeof(enum coalesce_direction));
|
||||
directions[i][0] = BASE;
|
||||
}
|
||||
for (j = 1; j < lennew + 1; j++)
|
||||
@ -1111,7 +1111,7 @@ static void show_patch_diff(struct combine_diff_path *elem, int num_parent,
|
||||
if (result_size && result[result_size-1] != '\n')
|
||||
cnt++; /* incomplete line */
|
||||
|
||||
sline = xcalloc(cnt+2, sizeof(*sline));
|
||||
sline = xcalloc(st_add(cnt, 2), sizeof(*sline));
|
||||
sline[0].bol = result;
|
||||
for (lno = 0, cp = result; cp < result + result_size; cp++) {
|
||||
if (*cp == '\n') {
|
||||
@ -1130,7 +1130,7 @@ static void show_patch_diff(struct combine_diff_path *elem, int num_parent,
|
||||
/* Even p_lno[cnt+1] is valid -- that is for the end line number
|
||||
* for deletion hunk at the end.
|
||||
*/
|
||||
sline[0].p_lno = xcalloc((cnt+2) * num_parent, sizeof(unsigned long));
|
||||
sline[0].p_lno = xcalloc(st_mult(st_add(cnt, 2), num_parent), sizeof(unsigned long));
|
||||
for (lno = 0; lno <= cnt; lno++)
|
||||
sline[lno+1].p_lno = sline[lno].p_lno + num_parent;
|
||||
|
||||
@ -1262,7 +1262,7 @@ static struct diff_filepair *combined_pair(struct combine_diff_path *p,
|
||||
struct diff_filespec *pool;
|
||||
|
||||
pair = xmalloc(sizeof(*pair));
|
||||
pool = xcalloc(num_parent + 1, sizeof(struct diff_filespec));
|
||||
pool = xcalloc(st_add(num_parent, 1), sizeof(struct diff_filespec));
|
||||
pair->one = pool + 1;
|
||||
pair->two = pool;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user