Repository navigation
Fix(cell): reject invalid k-point counts and Monkhorst-Pack grid dimensions - #8353
Merged
mohanchen merged 5 commits intoNov 22, 2026
Merged
Conversation
Confirmed against current develop with a small driver through the public read_kpoints path: a negative k-point count is silently accepted (nkstot=-4 returns success), a zero or non-numeric Monkhorst-Pack grid dimension is accepted with nkstot=0, and a negative dimension crashes with an uncaught std::length_error from renew(). - parse_kfile(): reject nkstot < 0 next to the existing MAX_KPOINTS check; - read_mp_mesh(): require successful extraction of all three grid dimensions, all positive, with the product within int range; - QList::read_from_file(): apply the same grid validation, following its existing warning-and-reset style. Refs deepmodeling#8352.
The cell and domain_decomposition object libraries reference NeighborSearch::init/build_neighbors/refresh_mdcell; mirror the mdcell_adapter test target, which compiles the three module_neighlist sources into the test executable.
ABACUS-PR-BOT
left a comment
Collaborator
There was a problem hiding this comment.
Two malformed-grid cases remain unhandled: two negative dimensions pass the guard, and the product can overflow before validation. Details and a regression-test suggestion are inline.
Static source review at 3da188d2e5bf9712e8c0f05d453eb47f3c79e461; no builds or tests were run, and CI was not independently verified.
Address review on deepmodeling#8353: a negative dimension pair (-1 -1 4) has a positive product and passed the product-sign check, and the long long product itself can overflow for extreme dimensions. Validate each dimension individually and compare before multiplying (nmp[0] <= INT_MAX/nmp[1], then nmp[0]*nmp[1] <= INT_MAX/nmp[2]), in both the KPT and QPT readers. Verified with the local driver: -1 -1 4 and 2097152^3 are now rejected, valid grids unaffected. Also make the QPT regression test discriminating: a negative dimension crashed the old code, instead of the zero-dimension case that left nkstot==0 in both old and new implementations.
2097152^3 = 2^63 overflows even a 64-bit signed product; the guard compares before multiplying, and these cases pin that behavior down.
… fix/kpt_mp_grid_validation
mohanchen
approved these changes
Nov 22, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Reminder
AGENTS.mdanddocs/developers_guide/agent_governance.md.source/changes.Linked Issue
Fixes #8352.
What's changed?
K_Vectors::parse_kfile(): rejectnkstot < 0next to the existingMAX_KPOINTScheck (previously a negative count fell through both branches and the function returned success);K_Vectors::read_mp_mesh(): require successful extraction of the three Monkhorst-Pack grid dimensions, all positive, with the product inintrange;QList::read_from_file(): the same grid validation, in its existing warning-and-reset style.Bug confirmation (empirical)
A small throwaway driver through the public
read_kpointspath, compiled directly against the sources, confirmed on current develop before the fix:-4-> success withnkstot=-40 4 4-> success withnkstot=0x 4 4-> success (extraction failure unnoticed,nmpstays{0,0,0})-1 4 4-> uncaughtstd::length_errorcrash inrenew()After the fix all four are rejected; a valid
2 2 2grid still yields 8 k-points.Unit Tests and/or Case Tests
source/source_cell/unittests/test_klist.cpp(7 cases: negative total count, zero/non-numeric/negative MP dimension, grid product overflow, valid grid control, malformed QPT grid reset).python3 tools/03_code_analysis/agent_governance_check.py --staged— pass.User-visible behavior changes
Malformed KPT/QPT files are now rejected with a clear message instead of being silently accepted or crashing the program. Valid files are unaffected. No INPUT/STRU changes.
Governance Notes
source_cellinput parsing only; no interface changes.