Fix an out-of-bound read in XC module at dft_functional=HF - #7837
Merged
Conversation
There was a problem hiding this comment.
Pull request overview
This PR addresses issue #5404 by preventing out-of-bounds reads in the XC gradient-correction path when dft_functional=HF leaves XC_Functional::func_id empty, which previously could yield a non-zero “phantom” XC energy/potential for nspin=2.
Changes:
- Added guards to skip gradient-correction routines when
func_idis empty (HF / no (semi-)local functional configured). - Hardened
func_idindexing by checkingfunc_id.size()before accessingfunc_id[1], and added a safe fallback when only one functional ID is present.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| source/source_hamilt/module_xc/xc_grad.cpp | Adds func_id.empty() early-return and guards func_id[1] access in gradcorr. |
| source/source_hamilt/module_xc/xc_gga_wrap.cpp | Adds func_id.empty() guards in gcx_spin/gcc_spin and avoids OOB by selecting func_id[1] only when present. |
Suppressed comments (1)
source/source_hamilt/module_xc/xc_grad.cpp:61
- The new early-return when func_id is empty skips initializing stress_gga for is_stress==true. Callers like source/source_pw/module_pwdft/stress_gga.cpp index stress_gga[0..8] after gradcorr returns, so leaving the vector empty will cause an out-of-bounds access (e.g., HF sets func_type=4 but leaves func_id empty).
if(func_id.empty())
{
return;
}
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
mohanchen
approved these changes
Aug 19, 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.
Fix #5404