Skip to content

Commit bc35ac1

Browse files
peffgitster
authored andcommitted
pack-objects: fix tree_depth and layer invariants
Commit 108f530 (pack-objects: move tree_depth into 'struct packing_data', 2018-08-16) dynamically manages a tree_depth array in packing_data that maintains one of these invariants: 1. tree_depth is NULL (i.e., the requested options don't require us to track tree depths) 2. tree_depth is non-NULL and has as many entries as the "objects" array We maintain (2) by: a. When the objects array grows, grow tree_depth to the same size (unless it's NULL, in which case we can leave it). b. When a caller asks to set a depth via oe_set_tree_depth(), if tree_depth is NULL we allocate it. But in (b), we use the number of stored objects, _not_ the allocated size of the objects array. So we can run into a situation like this: 1. packlist_alloc() needs to store the Nth object, so it grows the objects array to M, where M > N. 2. oe_set_tree_depth() wants to store a depth, so it allocates an array of length N. Now we've violated our invariant. 3. packlist_alloc() needs to store the N+1th object. But it _doesn't_ grow the objects array, since N <= M still holds. We try to assign to tree_depth[N+1], which is out of bounds. That doesn't happen in our test scripts, because the repositories they use are so small, but it's easy to trigger by running: echo HEAD | git pack-objects --revs --delta-islands --stdout >/dev/null in any reasonably-sized repo (like git.git). We can fix it by always growing the array to match pack->nr_alloc, not pack->nr_objects. Likewise for the "layer" array from fe0ac2f (pack-objects: move 'layer' into 'struct packing_data', 2018-08-16), which has the same bug. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent fe0ac2f commit bc35ac1

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

pack-objects.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,7 @@ static inline void oe_set_tree_depth(struct packing_data *pack,
364364
unsigned int tree_depth)
365365
{
366366
if (!pack->tree_depth)
367-
ALLOC_ARRAY(pack->tree_depth, pack->nr_objects);
367+
ALLOC_ARRAY(pack->tree_depth, pack->nr_alloc);
368368
pack->tree_depth[e - pack->objects] = tree_depth;
369369
}
370370

@@ -381,7 +381,7 @@ static inline void oe_set_layer(struct packing_data *pack,
381381
unsigned char layer)
382382
{
383383
if (!pack->layer)
384-
ALLOC_ARRAY(pack->layer, pack->nr_objects);
384+
ALLOC_ARRAY(pack->layer, pack->nr_alloc);
385385
pack->layer[e - pack->objects] = layer;
386386
}
387387

0 commit comments

Comments
 (0)