Git fork

Merge branch 'ne/alloc-free-and-null'

The clear_alloc_state() API function was not fully clearing the
structure for reuse, but since nobody reuses it, replace it with a
variant that frees the structure as well, making the callers simpler.

* ne/alloc-free-and-null:
alloc: fix dangling pointer in alloc_state cleanup

+20 -20
+8 -2
alloc.c
··· 36 36 int slab_nr, slab_alloc; 37 37 }; 38 38 39 - struct alloc_state *allocate_alloc_state(void) 39 + struct alloc_state *alloc_state_alloc(void) 40 40 { 41 41 return xcalloc(1, sizeof(struct alloc_state)); 42 42 } 43 43 44 - void clear_alloc_state(struct alloc_state *s) 44 + void alloc_state_free_and_null(struct alloc_state **s_) 45 45 { 46 + struct alloc_state *s = *s_; 47 + 48 + if (!s) 49 + return; 50 + 46 51 while (s->slab_nr > 0) { 47 52 s->slab_nr--; 48 53 free(s->slabs[s->slab_nr]); 49 54 } 50 55 51 56 FREE_AND_NULL(s->slabs); 57 + FREE_AND_NULL(*s_); 52 58 } 53 59 54 60 static inline void *alloc_node(struct alloc_state *s, size_t node_size)
+2 -2
alloc.h
··· 14 14 void *alloc_tag_node(struct repository *r); 15 15 void *alloc_object_node(struct repository *r); 16 16 17 - struct alloc_state *allocate_alloc_state(void); 18 - void clear_alloc_state(struct alloc_state *s); 17 + struct alloc_state *alloc_state_alloc(void); 18 + void alloc_state_free_and_null(struct alloc_state **s_); 19 19 20 20 #endif
+10 -16
object.c
··· 517 517 memset(o, 0, sizeof(*o)); 518 518 519 519 o->repo = repo; 520 - o->blob_state = allocate_alloc_state(); 521 - o->tree_state = allocate_alloc_state(); 522 - o->commit_state = allocate_alloc_state(); 523 - o->tag_state = allocate_alloc_state(); 524 - o->object_state = allocate_alloc_state(); 525 - 520 + o->blob_state = alloc_state_alloc(); 521 + o->tree_state = alloc_state_alloc(); 522 + o->commit_state = alloc_state_alloc(); 523 + o->tag_state = alloc_state_alloc(); 524 + o->object_state = alloc_state_alloc(); 526 525 o->is_shallow = -1; 527 526 CALLOC_ARRAY(o->shallow_stat, 1); 528 527 ··· 573 572 o->buffer_slab = NULL; 574 573 575 574 parsed_object_pool_reset_commit_grafts(o); 576 - clear_alloc_state(o->blob_state); 577 - clear_alloc_state(o->tree_state); 578 - clear_alloc_state(o->commit_state); 579 - clear_alloc_state(o->tag_state); 580 - clear_alloc_state(o->object_state); 575 + alloc_state_free_and_null(&o->blob_state); 576 + alloc_state_free_and_null(&o->tree_state); 577 + alloc_state_free_and_null(&o->commit_state); 578 + alloc_state_free_and_null(&o->tag_state); 579 + alloc_state_free_and_null(&o->object_state); 581 580 stat_validity_clear(o->shallow_stat); 582 - FREE_AND_NULL(o->blob_state); 583 - FREE_AND_NULL(o->tree_state); 584 - FREE_AND_NULL(o->commit_state); 585 - FREE_AND_NULL(o->tag_state); 586 - FREE_AND_NULL(o->object_state); 587 581 FREE_AND_NULL(o->shallow_stat); 588 582 }