2026PKUCourseHW5:replace atoi/atof with safe parsing helper#7142
2026PKUCourseHW5:replace atoi/atof with safe parsing helper#7142xiaoxuPekingUniversity wants to merge 4 commits intodeepmodeling:developfrom
Conversation
There was a problem hiding this comment.
Pull request overview
This PR aims to improve robustness of UPF 2.0.1 header parsing by replacing some atoi/atof usages in read_pp_upf201.cpp with helper functions based on std::stoi/std::stod.
Changes:
- Added
parse_int/parse_doublehelpers (anonymous namespace) for stricter string-to-number parsing. - Switched several numeric fields in
read_pseudo_upf201_headerfromatoi/atof/std::stodto the new helpers. - Accidentally added an editor swap file (
.swp) to the repository.
Reviewed changes
Copilot reviewed 1 out of 2 changed files in this pull request and generated 5 comments.
| File | Description |
|---|---|
source/source_cell/read_pp_upf201.cpp |
Adds safe parsing helpers and uses them for several UPF201 header numeric fields. |
source/source_cell/.read_pp_upf201.cpp.swp |
Newly added Vim swap file (should not be committed). |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| return value; | ||
| } | ||
| catch (...) | ||
| { | ||
| throw std::runtime_error("Invalid floating-point value: " + s); | ||
| } |
There was a problem hiding this comment.
Catching ... here will also intercept unrelated exceptions (e.g., std::bad_alloc) and lose the original diagnostic. Prefer catching std::invalid_argument and std::out_of_range from std::stoi/std::stod and report those via the project’s error mechanism so the failure reason is preserved.
| namespace//辅助函数用于后续类型替换 | ||
| { | ||
| int parse_int(const std::string& s) | ||
| { | ||
| try | ||
| { |
There was a problem hiding this comment.
PR description says atoi/atof are replaced in read_pp_upf201.cpp, but this file still contains multiple remaining atoi/atof uses (e.g., in read_pseudo_upf201_mesh, PP_NONLOCAL, PP_PSWFC, and SO parsing). Either update the description to reflect the narrower scope (header-only), or complete the replacement consistently across the file.
| int value = std::stoi(s, &pos); | ||
| if (pos != s.size()) | ||
| { | ||
| throw std::runtime_error("extra characters"); | ||
| } |
There was a problem hiding this comment.
These helpers use std::runtime_error, but this translation unit only includes read_pp.h and does not explicitly include <stdexcept>. Please add the proper standard header in this .cpp to avoid relying on transitive includes.
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
3a2a3a8 to
75ee501
Compare
|
Nice try! Thanks for your contribution. |
This PR replaces atoi/atof in read_pp_upf201.cpp with safer helper functions based on std::stoi and std::stod.
The change improves robustness of string-to-number conversion while keeping the original parsing logic unchanged.