@@ -827,32 +827,48 @@ static void verify_one_sparse(struct repository *r,
827
827
path -> buf );
828
828
}
829
829
830
- static void verify_one (struct repository * r ,
831
- struct index_state * istate ,
832
- struct cache_tree * it ,
833
- struct strbuf * path )
830
+ /*
831
+ * Returns:
832
+ * 0 - Verification completed.
833
+ * 1 - Restart verification - a call to ensure_full_index() freed the cache
834
+ * tree that is being verified and verification needs to be restarted from
835
+ * the new toplevel cache tree.
836
+ */
837
+ static int verify_one (struct repository * r ,
838
+ struct index_state * istate ,
839
+ struct cache_tree * it ,
840
+ struct strbuf * path )
834
841
{
835
842
int i , pos , len = path -> len ;
836
843
struct strbuf tree_buf = STRBUF_INIT ;
837
844
struct object_id new_oid ;
838
845
839
846
for (i = 0 ; i < it -> subtree_nr ; i ++ ) {
840
847
strbuf_addf (path , "%s/" , it -> down [i ]-> name );
841
- verify_one (r , istate , it -> down [i ]-> cache_tree , path );
848
+ if (verify_one (r , istate , it -> down [i ]-> cache_tree , path ))
849
+ return 1 ;
842
850
strbuf_setlen (path , len );
843
851
}
844
852
845
853
if (it -> entry_count < 0 ||
846
854
/* no verification on tests (t7003) that replace trees */
847
855
lookup_replace_object (r , & it -> oid ) != & it -> oid )
848
- return ;
856
+ return 0 ;
849
857
850
858
if (path -> len ) {
859
+ /*
860
+ * If the index is sparse and the cache tree is not
861
+ * index_name_pos() may trigger ensure_full_index() which will
862
+ * free the tree that is being verified.
863
+ */
864
+ int is_sparse = istate -> sparse_index ;
851
865
pos = index_name_pos (istate , path -> buf , path -> len );
866
+ if (is_sparse && !istate -> sparse_index )
867
+ return 1 ;
852
868
853
869
if (pos >= 0 ) {
854
870
verify_one_sparse (r , istate , it , path , pos );
855
- return ;
871
+ return 0 ;
856
872
}
857
873
858
874
pos = - pos - 1 ;
@@ -900,6 +916,7 @@ static void verify_one(struct repository *r,
900
916
oid_to_hex (& new_oid ), oid_to_hex (& it -> oid ));
901
917
strbuf_setlen (path , len );
902
918
strbuf_release (& tree_buf );
919
+ return 0 ;
903
920
}
904
921
905
922
void cache_tree_verify (struct repository * r , struct index_state * istate )
@@ -908,6 +925,10 @@ void cache_tree_verify(struct repository *r, struct index_state *istate)
908
925
909
926
if (!istate -> cache_tree )
910
927
return ;
911
- verify_one (r , istate , istate -> cache_tree , & path );
928
+ if (verify_one (r , istate , istate -> cache_tree , & path )) {
929
+ strbuf_reset (& path );
930
+ if (verify_one (r , istate , istate -> cache_tree , & path ))
931
+ BUG ("ensure_full_index() called twice while verifying cache tree" );
932
+ }
912
933
strbuf_release (& path );
913
934
}
0 commit comments