@@ -74,7 +74,13 @@ static int mark_object(struct object *obj, int type, void *data)
7474{
7575 struct object * parent = data ;
7676
77+ /*
78+ * The only case data is NULL or type is OBJ_ANY is when
79+ * mark_object_reachable() calls us. All the callers of
80+ * that function has non-NULL obj hence ...
81+ */
7782 if (!obj ) {
83+ /* ... these references to parent->fld are safe here */
7884 printf ("broken link from %7s %s\n" ,
7985 typename (parent -> type ), sha1_to_hex (parent -> sha1 ));
8086 printf ("broken link from %7s %s\n" ,
@@ -84,6 +90,7 @@ static int mark_object(struct object *obj, int type, void *data)
8490 }
8591
8692 if (type != OBJ_ANY && obj -> type != type )
93+ /* ... and the reference to parent is safe here */
8794 objerror (parent , "wrong object type in link" );
8895
8996 if (obj -> flags & REACHABLE )
@@ -109,7 +116,7 @@ static void mark_object_reachable(struct object *obj)
109116 mark_object (obj , OBJ_ANY , NULL );
110117}
111118
112- static int traverse_one_object (struct object * obj , struct object * parent )
119+ static int traverse_one_object (struct object * obj )
113120{
114121 int result ;
115122 struct tree * tree = NULL ;
@@ -138,7 +145,7 @@ static int traverse_reachable(void)
138145 entry = pending .objects + -- pending .nr ;
139146 obj = entry -> item ;
140147 parent = (struct object * ) entry -> name ;
141- result |= traverse_one_object (obj , parent );
148+ result |= traverse_one_object (obj );
142149 }
143150 return !!result ;
144151}
@@ -385,28 +392,34 @@ static void add_sha1_list(unsigned char *sha1, unsigned long ino)
385392 sha1_list .nr = ++ nr ;
386393}
387394
395+ static inline int is_loose_object_file (struct dirent * de ,
396+ char * name , unsigned char * sha1 )
397+ {
398+ if (strlen (de -> d_name ) != 38 )
399+ return 0 ;
400+ memcpy (name + 2 , de -> d_name , 39 );
401+ return !get_sha1_hex (name , sha1 );
402+ }
403+
388404static void fsck_dir (int i , char * path )
389405{
390406 DIR * dir = opendir (path );
391407 struct dirent * de ;
408+ char name [100 ];
392409
393410 if (!dir )
394411 return ;
395412
396413 if (verbose )
397414 fprintf (stderr , "Checking directory %s\n" , path );
398415
416+ sprintf (name , "%02x" , i );
399417 while ((de = readdir (dir )) != NULL ) {
400- char name [100 ];
401418 unsigned char sha1 [20 ];
402419
403420 if (is_dot_or_dotdot (de -> d_name ))
404421 continue ;
405- if (strlen (de -> d_name ) == 38 ) {
406- sprintf (name , "%02x" , i );
407- memcpy (name + 2 , de -> d_name , 39 );
408- if (get_sha1_hex (name , sha1 ) < 0 )
409- break ;
422+ if (is_loose_object_file (de , name , sha1 )) {
410423 add_sha1_list (sha1 , DIRENT_SORT_HINT (de ));
411424 continue ;
412425 }
@@ -556,8 +569,8 @@ static int fsck_cache_tree(struct cache_tree *it)
556569 sha1_to_hex (it -> sha1 ));
557570 return 1 ;
558571 }
559- mark_object_reachable (obj );
560572 obj -> used = 1 ;
573+ mark_object_reachable (obj );
561574 if (obj -> type != OBJ_TREE )
562575 err |= objerror (obj , "non-tree in cache-tree" );
563576 }
0 commit comments