Move clean-up after table rebuilding

Suppress a false positive alert by CodeQL.
This commit is contained in:
Nobuyoshi Nakada 2024-02-08 18:15:08 +09:00
parent 76f0eec20f
commit 0923a98868
No known key found for this signature in database
GPG Key ID: 3582D74E1FEE4465

38
st.c
View File

@ -718,7 +718,9 @@ count_collision(const struct st_hash_type *type)
#error "REBUILD_THRESHOLD should be >= 2" #error "REBUILD_THRESHOLD should be >= 2"
#endif #endif
static void rebuild_table_with(st_table *new_tab, st_table *tab); static void rebuild_table_with(st_table *const new_tab, st_table *const tab);
static void rebuild_move_table(st_table *const new_tab, st_table *const tab);
static void rebuild_cleanup(st_table *const tab);
/* Rebuild table TAB. Rebuilding removes all deleted bins and entries /* Rebuild table TAB. Rebuilding removes all deleted bins and entries
and can change size of the table entries and bins arrays. and can change size of the table entries and bins arrays.
@ -744,11 +746,13 @@ rebuild_table(st_table *tab)
new_tab = st_init_table_with_size(tab->type, new_tab = st_init_table_with_size(tab->type,
2 * tab->num_entries - 1); 2 * tab->num_entries - 1);
rebuild_table_with(new_tab, tab); rebuild_table_with(new_tab, tab);
rebuild_move_table(new_tab, tab);
} }
rebuild_cleanup(tab);
} }
static void static void
rebuild_table_with(st_table *new_tab, st_table *tab) rebuild_table_with(st_table *const new_tab, st_table *const tab)
{ {
st_index_t i, ni; st_index_t i, ni;
unsigned int size_ind; unsigned int size_ind;
@ -780,16 +784,24 @@ rebuild_table_with(st_table *new_tab, st_table *tab)
new_tab->num_entries++; new_tab->num_entries++;
ni++; ni++;
} }
if (new_tab != tab) { }
tab->entry_power = new_tab->entry_power;
tab->bin_power = new_tab->bin_power; static void
tab->size_ind = new_tab->size_ind; rebuild_move_table(st_table *const new_tab, st_table *const tab)
free(tab->bins); {
tab->bins = new_tab->bins; tab->entry_power = new_tab->entry_power;
free(tab->entries); tab->bin_power = new_tab->bin_power;
tab->entries = new_tab->entries; tab->size_ind = new_tab->size_ind;
free(new_tab); free(tab->bins);
} tab->bins = new_tab->bins;
free(tab->entries);
tab->entries = new_tab->entries;
free(new_tab);
}
static void
rebuild_cleanup(st_table *const tab)
{
tab->entries_start = 0; tab->entries_start = 0;
tab->entries_bound = tab->num_entries; tab->entries_bound = tab->num_entries;
tab->rebuilds_num++; tab->rebuilds_num++;
@ -2319,6 +2331,8 @@ rb_st_compact_table(st_table *tab)
/* Compaction: */ /* Compaction: */
st_table *new_tab = st_init_table_with_size(tab->type, 2 * num); st_table *new_tab = st_init_table_with_size(tab->type, 2 * num);
rebuild_table_with(new_tab, tab); rebuild_table_with(new_tab, tab);
rebuild_move_table(new_tab, tab);
rebuild_cleanup(tab);
} }
} }