fix(ipi): reject unknown atom names#5793
Conversation
📝 WalkthroughWalkthroughChangesi-PI validation flow
Estimated code review effort: 3 (Moderate) | ~20 minutes Sequence Diagram(s)sequenceDiagram
participant dp_ipi
participant main
participant Convert
dp_ipi->>main: start with coordinate and config files
main->>Convert: construct atom conversion
Convert-->>main: throw std::invalid_argument for unknown atom
main-->>dp_ipi: print error and return 1
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
source/ipi/driver.cc (1)
52-220: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low valueAdd a return statement at the end of
run_driver.Although the
while (true)loop makes the end of the function statically unreachable,run_driveris declared to return anint. Unlikemain, regular functions do not implicitly return0. Adding areturn 0;at the very end ensures the code complies with C++ standards and avoids potential-Wreturn-typecompiler warnings.Additionally, because there is no
breakin thewhile (true)loop, the cleanup code at the end of the function (lines 217-220) is dead code and will never be executed. If an exception is thrown, the process will exit and the OS will reclaim the memory, so the leak is benign, but cleaning it up or using a smart pointer likestd::unique_ptr<double[]>would be more robust.♻️ Proposed refactor
if (msg_buff != NULL) { delete[] msg_buff; } + return 0; }🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@source/ipi/driver.cc` around lines 52 - 220, Add an explicit return 0 at the end of run_driver after the msg_buff cleanup block to satisfy the function’s int return contract. Since the while loop has no break and makes that cleanup unreachable, either remove the dead manual cleanup or replace msg_buff with ownership-managed storage such as std::unique_ptr<double[]> while preserving its existing buffer usage.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Nitpick comments:
In `@source/ipi/driver.cc`:
- Around line 52-220: Add an explicit return 0 at the end of run_driver after
the msg_buff cleanup block to satisfy the function’s int return contract. Since
the while loop has no break and makes that cleanup unreachable, either remove
the dead manual cleanup or replace msg_buff with ownership-managed storage such
as std::unique_ptr<double[]> while preserving its existing buffer usage.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro
Run ID: 97b1e83a-8abc-4ca4-b7f0-cdbd13da42f7
📒 Files selected for processing (3)
source/ipi/driver.ccsource/ipi/src/Convert.ccsource/ipi/tests/test_driver_input_validation.py
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #5793 +/- ##
==========================================
- Coverage 79.85% 79.57% -0.28%
==========================================
Files 1022 1029 +7
Lines 117351 117582 +231
Branches 4313 4310 -3
==========================================
- Hits 93706 93570 -136
- Misses 22101 22464 +363
- Partials 1544 1548 +4 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Look up coordinate-file atom names without std::map::operator[] so missing names cannot be inserted and silently treated as valid type 0. Report a clear invalid-argument diagnostic before model loading. Add a top-level dp_ipi exception boundary with a stable exit code and a backend-independent regression using an unmapped atom and deliberately missing model file. Fixes deepmodeling#5648. Coding-Agent: Codex Codex-Version: codex-cli 0.144.1 Model: gpt-5.6-sol Reasoning-Effort: xhigh
0c09a60 to
d895434
Compare
wanghan-iapcm
left a comment
There was a problem hiding this comment.
Correct fix. Convert used name_type_map[atomname] (std::map::operator[]), which inserts a value-initialized 0 for a missing key -- and type 0 is a valid DeePMD type, so an unknown or mistyped atom name silently produced wrong-but-plausible energies/forces. Switching to .find() + std::invalid_argument, caught in main to return exit 1 with a stable diagnostic before model loading, is the right fix. The regression genuinely fails pre-fix: with a deliberately missing graph_file, the old code reaches model loading (a different error / non-1 exit) instead of the exact 'Unknown atom name' message, which nicely proves validation happens before model init. Keeping file parsing in main to avoid a spurious CodeQL uncontrolled-path finding is a thoughtful touch. LGTM.
Dismissing to re-review through the /code-review skill per process.
|
Follow-up on the Coding agent: Codex |
wanghan-iapcm
left a comment
There was a problem hiding this comment.
Re-reviewed via the code-review skill. Correct fix: Convert's operator[] silently inserted unknown atom names as the valid type 0; switching to .find()+std::invalid_argument, caught in main around only the Convert construction (file parsing kept in main to avoid the CodeQL uncontrolled-path false positive), returning exit 1 with a stable diagnostic before model loading. All cvt. call sites were converted to cvt->, the unique_ptr+raw-new is the correct C++11 pattern, and the try/catch scope doesn't swallow model-load errors. A reviewer executed the regression and confirmed it genuinely fails pre-fix (exit 134 / unrelated message) and passes post-fix (exit 1 / exact message); both accept and reject branches are covered. One non-blocking, pre-existing note: a missing atom_type key in the config still aborts via an uncaught nlohmann type_error at driver.cc:76 (before the try), so the PR body's broader 'configuration errors' phrasing slightly overclaims -- worth a follow-up, out of scope here. LGTM.
Summary
std::map::operator[];atom_typeinstead of inserting them as DeePMD type 0;Impact
Type 0 is a valid model type. The previous
operator[]lookup silently inserted every unknown name with value 0, so a typo or incompleteatom_typemapping could produce plausible-looking but scientifically incorrect energies and forces.Why existing tests missed this
The existing i-PI tests use complete O/H mappings and exercise successful socket and inference workflows. None supplies an unknown coordinate-file name, so the default-insertion behavior was never observed.
The new test uses atom
Xxwith onlyOconfigured and deliberately pointsgraph_fileat a missing model. It requires the unknown atom diagnostic and exact exit code before any model-loading error can occur.Exception boundary and CodeQL
The first version wrapped the entire driver in a helper called from
main. That preserved runtime behavior but caused CodeQL to treat the pre-existing command-line configuration-file access as a newly introduced uncontrolled path flow. The revised implementation keeps file parsing directly inmainand limits the exception boundary toConvertconstruction, which is the operation that can reject an unknown atom name. This avoids suppressing the security scan while retaining the intended stable diagnostic.Validation
dp_ipi;dp_ipitarget build succeeded after the localized exception-boundary revision;Convertcompiled separately under C++11 with-Wall -Wextra -Werror;ruff format .;ruff check .;git diff --check.Closes #5648.
Coding agent: Codex
Codex version: codex-cli 0.144.1
Model: gpt-5.6-sol
Reasoning effort: xhigh