Skip to content

Fix: validate cube file parsing before copying charge data (#7563), in addition #6458 is fixed - #8023

Merged
mohanchen merged 5 commits into
deepmodeling:developfrom
mohanchen:2026-09-25-line1-4
Sep 26, 2026
Merged

mohanchen merged 5 commits into
deepmodeling:developfrom
mohanchen:2026-09-25-line1-4

Conversation

@mohanchen

@mohanchen mohanchen commented Sep 25, 2026 •

Copy link
Copy Markdown
Collaborator

Fix #7563 and #6458

read_vdata_palgrid() ignored the bool return of read_cube() and used the parsed dimensions and data unconditionally, so a malformed or truncated cube file could lead to out-of-bounds access in memcpy or trilinear_interpolate().

  • read_cube() now checks the stream state after each parsing stage, rejects negative natom and non-positive grid dimensions, guards the nxnynz product against int overflow, and returns false if any expected value is missing.
  • read_vdata_palgrid() checks the return value, logs a warning, and propagates the failure to its caller.
  • Add test_read_cube.cpp covering valid files, truncated data, invalid dimensions, negative natom, and failure propagation through read_vdata_palgrid().

Verification: cmake --build build --target MODULE_IO_read_cube MODULE_IO_rho_io; OMP_NUM_THREADS=1 ctest -V -R
"MODULE_IO_read_cube|MODULE_IO_rho_io" (10/10 passed); agent_governance_check.py --staged (no blocking findings; docs update not required, no INPUT behavior change).

Reminder

  • I have read AGENTS.md and docs/developers_guide/agent_governance.md.
  • I have linked an issue or explained why this PR does not need one.
  • I have added adequate unit tests and/or case tests, or explained why not.
  • I have listed the exact verification commands run and their results.
  • I have described user-visible behavior changes, including INPUT parameter changes.
  • I have explained core-module impact for ESolver, HSolver, ElecState, Hamilt, Operator, Psi, or other source/ changes.
  • I have requested any needed governance exception below.

Linked Issue

Fix #

Unit Tests and/or Case Tests for my changes

  • Commands run:
  • Result summary:
  • Checks not run, with reason:

What's changed?

  • Example: brief summary of the user-visible or developer-facing change.

Governance Notes

  • INPUT/docs changes:
  • Core module impact:
  • Exceptions requested:

…ing#7563)

read_vdata_palgrid() ignored the bool return of read_cube() and used
the parsed dimensions and data unconditionally, so a malformed or
truncated cube file could lead to out-of-bounds access in memcpy or
trilinear_interpolate().

- read_cube() now checks the stream state after each parsing stage,
  rejects negative natom and non-positive grid dimensions, guards the
  nx*ny*nz product against int overflow, and returns false if any
  expected value is missing.
- read_vdata_palgrid() checks the return value, logs a warning, and
  propagates the failure to its caller.
- Add test_read_cube.cpp covering valid files, truncated data,
  invalid dimensions, negative natom, and failure propagation through
  read_vdata_palgrid().

Verification: cmake --build build --target MODULE_IO_read_cube
MODULE_IO_rho_io; OMP_NUM_THREADS=1 ctest -V -R
"MODULE_IO_read_cube|MODULE_IO_rho_io" (10/10 passed);
agent_governance_check.py --staged (no blocking findings; docs update
not required, no INPUT behavior change).
@mohanchen mohanchen added Bugs Bugs that only solvable with sufficient knowledge of DFT Input&Output Suitable for coders without knowing too many DFT details Refactor Refactor ABACUS codes labels Sep 25, 2026
Comment thread source/source_io/module_output/read_cube.cpp
abacus_fixer and others added 2 commits September 26, 2026 06:32
ModuleIO::read_vdata_palgrid previously returned false on the root rank
when the cube file was missing or malformed, while the other ranks
entered Parallel_Grid::bcast() and blocked in MPI_Recv waiting for data
that would never be sent. Replace the early returns with
ModuleBase::WARNING_QUIT so the run terminates on every rank.

This makes the bool error code meaningless, so remove the now-dead
fallback paths that depended on it: the nspin=2/4 "rearrange electron
density later" branch, the meta-GGA tau TF fallback, the atomic-rho
fallback for failed file reads, and the read_error/read_kin_error
plumbing in Charge::init_rho. Update the unit test to expect death on a
malformed cube file.
…x.md

The online documentation for hs_matrix.md only described the new
(develop) filenames for Hamiltonian/overlap matrix output, which
confused LTS users. Add explicit notes mapping out_hsk to the LTS
keyword out_mat_hs (files data-0-H, data-0-S) and out_hsr to the
LTS keyword out_mat_hs2 (files data-HR-sparse_SPIN0.csr,
data-SR-sparse_SPIN0.csr).

Fixes deepmodeling#6458
@mohanchen mohanchen changed the title Fix: validate cube file parsing before copying charge data (#7563) Fix: validate cube file parsing before copying charge data (#7563), in addition #6458 is fixed Sep 26, 2026
… files

Commit 6601fb6 made a failed cube read abort instead of hanging non-root
ranks, but in doing so it dropped the init_chg=auto semantics: when no
density file exists, auto must silently fall back to the atomic density.
This broke ASE/abacuslite MD, whose first ionic step legitimately runs in a
fresh directory with no charge file and relies on that fallback.

read_rho_file/read_kin_file now probe for the cube file on the parsing rank
and broadcast the result with Parallel_Common::bcast_bool, so all ranks agree
to skip together (no MPI hang) instead of aborting inside read_vdata_palgrid.
init_rho aborts only for init_chg=file; auto falls back to atomic density
(and TF tau for meta-GGA). Add regression tests for both paths.

Verified: cmake --build build --target elecstate passes;
agent_governance_check.py --base HEAD --head HEAD reports no findings.
@mohanchen
mohanchen merged commit 1202e6f into deepmodeling:develop Sep 26, 2026
17 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Bugs Bugs that only solvable with sufficient knowledge of DFT Input&Output Suitable for coders without knowing too many DFT details Refactor Refactor ABACUS codes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Code scan] Validate cube read success before copying charge data

2 participants