Skip to content

Commit 667b76e

Browse files
peffgitster
authored andcommitted
walker_fetch(): avoid raw array length computation
We compute the length of an array of object_id's with a raw multiplication. In theory this could trigger an integer overflow which would cause an under-allocation (and eventually an out of bounds write). I doubt this can be triggered in practice, since you'd need to feed it an enormous number of target objects, which would typically come from the ref advertisement and be using proportional memory. And even on 64-bit systems, where "int" is much smaller than "size_t", that should hold: even though "targets" is an int, the multiplication will be done as a size_t because of the use of sizeof(). But we can easily fix it by using ALLOC_ARRAY(), which uses st_mult() under the hood. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 9734b74 commit 667b76e

File tree

1 file changed

+3
-1
lines changed

1 file changed

+3
-1
lines changed

walker.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,12 +261,14 @@ int walker_fetch(struct walker *walker, int targets, char **target,
261261
struct strbuf refname = STRBUF_INIT;
262262
struct strbuf err = STRBUF_INIT;
263263
struct ref_transaction *transaction = NULL;
264-
struct object_id *oids = xmalloc(targets * sizeof(struct object_id));
264+
struct object_id *oids;
265265
char *msg = NULL;
266266
int i, ret = -1;
267267

268268
save_commit_buffer = 0;
269269

270+
ALLOC_ARRAY(oids, targets);
271+
270272
if (write_ref) {
271273
transaction = ref_transaction_begin(&err);
272274
if (!transaction) {

0 commit comments

Comments
 (0)