Skip to content

Commit e159b81

Browse files
peffgitster
authored andcommitted
pack-objects: zero-initialize tree_depth/layer arrays
Commit 108f530 (pack-objects: move tree_depth into 'struct packing_data', 2018-08-16) started maintaining a tree_depth array that matches the "objects" array. We extend the array when: 1. The objects array is extended, in which case we use realloc to extend the tree_depth array. 2. A caller asks to store a tree_depth for object N, and this is the first such request; we create the array from scratch and store the value for N. In the latter case, though, we use regular xmalloc(), and the depth values for any objects besides N is undefined. This happens to not trigger a bug with the current code, but the reasons are quite subtle: - we never ask about the depth for any object with index i < N. This is because we store the depth immediately for all trees and blobs. So any such "i" must be a non-tree, and therefore we will never need to care about its depth (in fact, we really only care about the depth of trees). - there are no objects at this point with index i > N, because we always fill in the depth for a tree immediately after its object entry is created (we may still allocate uninitialized depth entries, but they'll be initialized by packlist_alloc() when it initializes the entry in the "objects" array). So it works, but only by chance. To be defensive, let's zero the array, which matches the "unset" values which would be handed out by oe_tree_depth() already. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent bc35ac1 commit e159b81

File tree

2 files changed

+3
-2
lines changed

2 files changed

+3
-2
lines changed

git-compat-util.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -844,6 +844,7 @@ extern FILE *fopen_or_warn(const char *path, const char *mode);
844844
#define FREE_AND_NULL(p) do { free(p); (p) = NULL; } while (0)
845845

846846
#define ALLOC_ARRAY(x, alloc) (x) = xmalloc(st_mult(sizeof(*(x)), (alloc)))
847+
#define CALLOC_ARRAY(x, alloc) (x) = xcalloc((alloc), sizeof(*(x)));
847848
#define REALLOC_ARRAY(x, alloc) (x) = xrealloc((x), st_mult(sizeof(*(x)), (alloc)))
848849

849850
#define COPY_ARRAY(dst, src, n) copy_array((dst), (src), (n), sizeof(*(dst)) + \

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_alloc);
367+
CALLOC_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_alloc);
384+
CALLOC_ARRAY(pack->layer, pack->nr_alloc);
385385
pack->layer[e - pack->objects] = layer;
386386
}
387387

0 commit comments

Comments
 (0)