Skip to content

Fix an out-of-bound read in XC module at dft_functional=HF - #7837

Merged
mohanchen merged 2 commits into
deepmodeling:developfrom
maki49:fix-hf-xc
Aug 19, 2026
Merged

Fix an out-of-bound read in XC module at dft_functional=HF#7837
mohanchen merged 2 commits into
deepmodeling:developfrom
maki49:fix-hf-xc

Conversation

@maki49

@maki49 maki49 commented Aug 18, 2026

Copy link
Copy Markdown
Collaborator

Fix #5404

Copilot AI lite review requested due to automatic review settings August 18, 2026 18:57

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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_id is empty (HF / no (semi-)local functional configured).
  • Hardened func_id indexing by checking func_id.size() before accessing func_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.

Comment thread source/source_hamilt/module_xc/xc_grad.cpp
@mohanchen mohanchen added Bugs Bugs that only solvable with sufficient knowledge of DFT Refactor Refactor ABACUS codes labels Aug 19, 2026
@mohanchen
mohanchen merged commit 978e3b6 into deepmodeling:develop Aug 19, 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 Refactor Refactor ABACUS codes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

XC energy is not zero when dft_functional=hf and nspin=2

3 participants