From 1705a017fd21137f79f0044070e3e1b8364ef6d8 Mon Sep 17 00:00:00 2001 From: Zhiwen Zhao Date: Fri, 12 Jun 2026 22:33:19 -0400 Subject: [PATCH] Guard parseFileAndRemoveComments against a comment before the first newline rfind('\n') returns npos when a comment marker is on the first line, and erase(npos, ...) throws std::out_of_range. Treat firstNL==npos as "erase from position 0" and secondNL==npos (comment on the last line, no trailing newline) as "erase to end". Fixes #133 Co-Authored-By: Claude Fable 5 --- gemc/guts/gutilities.cc | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/gemc/guts/gutilities.cc b/gemc/guts/gutilities.cc index 09942f22..dcb70c37 100644 --- a/gemc/guts/gutilities.cc +++ b/gemc/guts/gutilities.cc @@ -389,7 +389,11 @@ string parseFileAndRemoveComments(const string& filename, const string& commentC while ((nFPos = parsedString.find(commentChars)) != string::npos) { size_t firstNL = parsedString.rfind('\n', nFPos); size_t secondNL = parsedString.find('\n', nFPos); - parsedString.erase(firstNL, secondNL - firstNL); + size_t eraseStart = (firstNL == string::npos) ? 0 : firstNL; + size_t eraseLen = (secondNL == string::npos) + ? string::npos + : secondNL - eraseStart; + parsedString.erase(eraseStart, eraseLen); } return parsedString;