Skip to content

Commit d770187

Browse files
peffgitster
authored andcommitted
tree-diff: catch integer overflow in combine_diff_path allocation
A combine_diff_path struct has two "flex" members allocated alongside the struct: a string to hold the pathname, and an array of parent pointers. We use an "int" to compute this, meaning we may easily overflow it if the pathname is extremely long. We can fix this by using size_t, and checking for overflow with the st_add helper. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 935de81 commit d770187

File tree

2 files changed

+4
-4
lines changed

2 files changed

+4
-4
lines changed

diff.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -215,8 +215,8 @@ struct combine_diff_path {
215215
} parent[FLEX_ARRAY];
216216
};
217217
#define combine_diff_path_size(n, l) \
218-
(sizeof(struct combine_diff_path) + \
219-
sizeof(struct combine_diff_parent) * (n) + (l) + 1)
218+
st_add4(sizeof(struct combine_diff_path), (l), 1, \
219+
st_mult(sizeof(struct combine_diff_parent), (n)))
220220

221221
extern void show_combined_diff(struct combine_diff_path *elem, int num_parent,
222222
int dense, struct rev_info *);

tree-diff.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,8 @@ static struct combine_diff_path *path_appendnew(struct combine_diff_path *last,
124124
unsigned mode, const unsigned char *sha1)
125125
{
126126
struct combine_diff_path *p;
127-
int len = base->len + pathlen;
128-
int alloclen = combine_diff_path_size(nparent, len);
127+
size_t len = st_add(base->len, pathlen);
128+
size_t alloclen = combine_diff_path_size(nparent, len);
129129

130130
/* if last->next is !NULL - it is a pre-allocated memory, we can reuse */
131131
p = last->next;

0 commit comments

Comments
 (0)