Skip to content

Commit

Permalink
Fix segfault in memory cleanup (#40)
Browse files Browse the repository at this point in the history
Out of bounds memory access by indexing null pointer
`avg_prop_info` unconditionally
  • Loading branch information
Jan-Grimo Sobez authored Oct 5, 2023
1 parent c81f416 commit 719975c
Showing 1 changed file with 13 additions and 11 deletions.
24 changes: 13 additions & 11 deletions tightbind/memory.c
Original file line number Diff line number Diff line change
Expand Up @@ -662,17 +662,20 @@ is present.\n");

void cleanup_memory()
{
int i;
for(i=0;i<details->num_KPOINTS;i++){
CONDITIONAL_FREE(avg_prop_info[i].orbs);
CONDITIONAL_FREE(avg_prop_info[i].orbsI);
CONDITIONAL_FREE(avg_prop_info[i].chg_mat);
CONDITIONAL_FREE(avg_prop_info[i].energies);
if( details->num_FMO_frags || details->num_FCO_frags){
CONDITIONAL_FREE(avg_prop_info[i].FMO_orbs);
CONDITIONAL_FREE(avg_prop_info[i].FMO_orbsI);
CONDITIONAL_FREE(avg_prop_info[i].FMO_chg_mat);
if(avg_prop_info != NULL) {
for(int i=0;i<details->num_KPOINTS;i++){
CONDITIONAL_FREE(avg_prop_info[i].orbs);
CONDITIONAL_FREE(avg_prop_info[i].orbsI);
CONDITIONAL_FREE(avg_prop_info[i].chg_mat);
CONDITIONAL_FREE(avg_prop_info[i].energies);
if( details->num_FMO_frags || details->num_FCO_frags){
CONDITIONAL_FREE(avg_prop_info[i].FMO_orbs);
CONDITIONAL_FREE(avg_prop_info[i].FMO_orbsI);
CONDITIONAL_FREE(avg_prop_info[i].FMO_chg_mat);
}
}
free(avg_prop_info);
avg_prop_info = NULL;
}
sym_op_type *next=sym_ops_present;
while(next){
Expand All @@ -697,7 +700,6 @@ void cleanup_memory()
CONDITIONAL_FREE(cmplx_overlap);
CONDITIONAL_FREE(cmplx_work);
CONDITIONAL_FREE(orbital_lookup_table);
CONDITIONAL_FREE(avg_prop_info);
CONDITIONAL_FREE(orbital_ordering);
CONDITIONAL_FREE(OP_mat);
CONDITIONAL_FREE(net_chgs);
Expand Down

0 comments on commit 719975c

Please sign in to comment.