Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 44c0d06

Browse files
committed
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
2 parents bf781d9 + 5e2feb5 commit 44c0d06

File tree

3 files changed

+20
-20
lines changed

3 files changed

+20
-20
lines changed

alloc.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,19 +36,25 @@ struct alloc_state {
3636
int slab_nr, slab_alloc;
3737
};
3838

39-
struct alloc_state *allocate_alloc_state(void)
39+
struct alloc_state *alloc_state_alloc(void)
4040
{
4141
return xcalloc(1, sizeof(struct alloc_state));
4242
}
4343

44-
void clear_alloc_state(struct alloc_state *s)
44+
void alloc_state_free_and_null(struct alloc_state **s_)
4545
{
46+
struct alloc_state *s = *s_;
47+
48+
if (!s)
49+
return;
50+
4651
while (s->slab_nr > 0) {
4752
s->slab_nr--;
4853
free(s->slabs[s->slab_nr]);
4954
}
5055

5156
FREE_AND_NULL(s->slabs);
57+
FREE_AND_NULL(*s_);
5258
}
5359

5460
static inline void *alloc_node(struct alloc_state *s, size_t node_size)

alloc.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ void *alloc_commit_node(struct repository *r);
1414
void *alloc_tag_node(struct repository *r);
1515
void *alloc_object_node(struct repository *r);
1616

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_);
1919

2020
#endif

object.c

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -517,12 +517,11 @@ struct parsed_object_pool *parsed_object_pool_new(struct repository *repo)
517517
memset(o, 0, sizeof(*o));
518518

519519
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();
526525
o->is_shallow = -1;
527526
CALLOC_ARRAY(o->shallow_stat, 1);
528527

@@ -573,16 +572,11 @@ void parsed_object_pool_clear(struct parsed_object_pool *o)
573572
o->buffer_slab = NULL;
574573

575574
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);
581580
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);
587581
FREE_AND_NULL(o->shallow_stat);
588582
}

0 commit comments

Comments
 (0)