Skip to content

Commit d8b9a4a

Browse files
author
abacus_fixer
committed
fix: prevent out-of-bounds write in write_save_to_flat for nspin=1
For nspin==1, uom_save is allocated as a single block (pot_index is doubled only when nspin==2). However, write_save_to_flat previously wrote both spin channels unconditionally, causing part of the first block to be overwritten and indices past the vector end to be accessed (undefined behavior via operator[]). Fix by guarding the channel-[1] write with `if (nspin_ == 2)`, consistent with write_to_flat() and read_from_flat() which already handle the two channels correctly.
1 parent e8c2852 commit d8b9a4a

1 file changed

Lines changed: 8 additions & 2 deletions

File tree

source/source_estate/occ_matrix.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -291,11 +291,17 @@ void OccupationMatrix::write_save_to_flat(const UnitCell& cell,
291291
}
292292
else if (nspin_ == 1 || nspin_ == 2)
293293
{
294-
const int half_size = uom_save.size() / 2;
295294
for (int mm = 0; mm < size; mm++)
296295
{
297296
uom_save[index[iat] + mm] = occ_[iat][target_l][0][0].c[mm];
298-
uom_save[half_size + index[iat] + mm] = occ_[iat][target_l][0][1].c[mm];
297+
}
298+
if (nspin_ == 2)
299+
{
300+
const int half_size = uom_save.size() / 2;
301+
for (int mm = 0; mm < size; mm++)
302+
{
303+
uom_save[half_size + index[iat] + mm] = occ_[iat][target_l][0][1].c[mm];
304+
}
299305
}
300306
}
301307
}

0 commit comments

Comments
 (0)