Skip to content

Commit 139e022

Browse files
zhaozhiwenclaude
andcommitted
Detect YAML inputs by extension and report a missing file cleanly
Two defects in YAML handling: - findYamls classified any token *containing* ".yaml"/".yml" as an input file (substring match). An option value like -prefix=run.yaml.bak was misclassified, then skipped from option parsing (line 122) and silently dropped, and fed to YAML::LoadFile. Match a trailing extension instead. - setOptionsValuesFromYamlFile caught only YAML::ParserException. YAML::LoadFile throws YAML::BadFile (a sibling, both derive from YAML::Exception) for a missing/unreadable path, so a mistyped *.yaml filename escaped the catch and called std::terminate. Add a BadFile catch with a clean "cannot open" message. Verified: "gemc nonexistent.yaml" now prints "Cannot open yaml file ... Check the path and spelling." and exits cleanly instead of terminating with an uncaught exception (Geant4 11.4.1 dev container). Fixes #125 Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
1 parent 48f83e5 commit 139e022

1 file changed

Lines changed: 12 additions & 4 deletions

File tree

gemc/goptions/goptions.cc

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -284,12 +284,15 @@ void GOptions::printOptionOrSwitchHelp(const std::string& tag) const {
284284
// Private method: see header. Kept undocumented here to avoid duplicate param docs.
285285
vector<string> GOptions::findYamls(int argc, char* argv[]) {
286286
vector<string> yaml_files;
287+
auto ends_with = [](const string& s, const string& suffix) {
288+
return s.size() >= suffix.size() &&
289+
s.compare(s.size() - suffix.size(), suffix.size(), suffix) == 0;
290+
};
287291
for (int i = 1; i < argc; i++) {
288292
string arg = argv[i];
289-
size_t pos = arg.find(".yaml");
290-
if (pos != string::npos) yaml_files.push_back(arg);
291-
pos = arg.find(".yml");
292-
if (pos != string::npos) yaml_files.push_back(arg);
293+
// Match a trailing .yaml/.yml extension, not any substring, so option values such as
294+
// -prefix=run.yaml.bak are not mistaken for input files (and silently dropped).
295+
if (ends_with(arg, ".yaml") || ends_with(arg, ".yml")) yaml_files.push_back(arg);
293296
}
294297
return yaml_files;
295298
}
@@ -310,6 +313,11 @@ void GOptions::setOptionsValuesFromYamlFile(const std::string& yaml) {
310313
try {
311314
config = YAML::LoadFile(yaml);
312315
}
316+
catch (YAML::BadFile& e) {
317+
cerr << FATALERRORL << "Cannot open yaml file " << YELLOWHHL << yaml << RSTHHR
318+
<< ". Check the path and spelling." << endl;
319+
exit(EC__YAML_PARSING_ERROR);
320+
}
313321
catch (YAML::ParserException& e) {
314322
cerr << FATALERRORL << "Error parsing " << YELLOWHHL << yaml << RSTHHR
315323
<< " yaml file." << endl;

0 commit comments

Comments
 (0)