• Jeff King's avatar
    pack-objects: zero-initialize tree_depth/layer arrays · e159b810
    Jeff King authored
    Commit 108f5303 (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: 's avatarJeff King <peff@peff.net>
    Signed-off-by: 's avatarJunio C Hamano <gitster@pobox.com>
    e159b810
pack-objects.h 10.6 KB