Repository navigation
Conversation
Guard the qasmtools recursive-descent parser against malformed input reachable through the public entry point: catch out-of-range numeric literals, bound expression recursion depth, guard EOF in string lexing, and replace exception-throwing variant access with safe accessors. No behavioral change on valid input (all bundled .qasm samples parse identically). Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01JdMbZBMa3kMKd2QTRoqnff
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Harden the OpenQASM 2.0 parser against malformed input
Summary
Malformed OpenQASM input reachable via
parse_string(hencepystaq.parse_str/staqCLI) can abort or hang the process. This PR closes five such cases; valid input is unchanged.Cases
> INT_MAXabort()std::out_of_rangefromstd::stoifloatrange (1e99999999)abort()std::out_of_rangefromstd::stof(((…)))parse_exp/parse_atomlex_stringloop omits the EOF checkqreg 5[2];)abort()std::bad_variant_accessfromas_string()/as_int()Cases 1, 2 and 5 raise exceptions other than
ParseError(aParseError-only handler still aborts); cases 3 and 4 bypass exception handling entirely.Changes
lexer.hppstd::stoi/std::stofintry/catch (std::out_of_range)→Token::Kind::error; add&& buf_->peek() != EOFto thelex_stringloopparser.hppMAX_EXPR_DEPTH = 1000) on expression recursion; overflow raisesParseErrortoken.hppas_int/as_real/as_stringmade total viastd::get_if, returning a default on mismatch; parsererror_flag still yieldsParseErrorCHANGES.md# Pre-releaseTesting
ParseErroror parse cleanly — no abort or hang..qasmfiles parse identically before and after.