Skip to content

Commit cab4bca

Browse files
author
abacus_fixer
committed
fix(klist): restore nkstot loop bound in pack_kpts to fix heap overflow
bee21a3 changed the kvec_c_full loop in pack_kpts from `nkstot` to `kvec_c_full.size()`, but the aux buffer is still allocated as `nkstot_nospin * 3` (which equals `nkstot * 3` at the assignment point in klist.cpp:291, before spin doubling). For nspin=2 cases (spin_mult=2), kvec_c_full has been resized to `nkstot * 2` by renew(), so the new loop writes 2x past the end of kvec_c_full_aux, corrupting the heap and causing `free(): invalid pointer` when ESolver_FP is destroyed. Restore the original `nkstot` bound so the behavior matches the pre-refactor code exactly. The aux buffer (allocated as `nkstot_nospin * 3`) and bcast length (`nkstot_nospin * 3`) are unchanged; only the loop upper bound is restored. Verified: 12 failed 01_PW tests (003, 007, 028, 030, 034, 036, 037, 050, 055, 063, 078, 079, 097x2, 206, 815, scf_deltaspin2) all pass again; valgrind reports no Invalid write from ABACUS code.
1 parent f23ff32 commit cab4bca

1 file changed

Lines changed: 0 additions & 4 deletions

File tree

source/source_cell/klist_io.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -273,10 +273,6 @@ void pack_kpts(const std::vector<int>& isk,
273273
kvec_d_aux[3 * ik] = kvec_d[ik].x;
274274
kvec_d_aux[3 * ik + 1] = kvec_d[ik].y;
275275
kvec_d_aux[3 * ik + 2] = kvec_d[ik].z;
276-
}
277-
const int nkstot_full = static_cast<int>(kvec_c_full.size());
278-
for (int ik = 0; ik < nkstot_full; ik++)
279-
{
280276
kvec_c_full_aux[3 * ik] = kvec_c_full[ik].x;
281277
kvec_c_full_aux[3 * ik + 1] = kvec_c_full[ik].y;
282278
kvec_c_full_aux[3 * ik + 2] = kvec_c_full[ik].z;

0 commit comments

Comments
 (0)