Skip to content

Fix #15298: Internal error with invalid escape sequence. - #9104

Open
ludviggunne wants to merge 2 commits into
cppcheck-opensource:mainfrom
ludviggunne:15298-invalid-escape
Open

ludviggunne wants to merge 2 commits into
cppcheck-opensource:mainfrom
ludviggunne:15298-invalid-escape

Conversation

@ludviggunne

Copy link
Copy Markdown
Collaborator

No description provided.

@dmcppcheck dmcppcheck left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AI review

The fix in valueFlowSetConstantValue works: the reported int a = '\s'; and many variations (arithmetic, switch cases, enums, arrays, L/u8 prefixes, '\xZZ', '\8', bodies of instantiated templates) no longer give an internalError. With --enable=portability they now give the nonStandardCharLiteral warning instead.

One path still fails. TemplateSimplifier::simplifyCalculations (lib/templatesimplifier.cpp:2766-2769) also calls MathLib::toBigNumber on a %char% without a catch. So a parenthesized comparison with an invalid escape inside template arguments, like V<('\s' == 1)> v;, still gives Internal Error ... characterLiteralToLL('\s') => invalid escape sequence. The base gives the same error there, so this is not a regression; it is a case of the same ticket that the PR does not handle.

Comments

# Comment By Where Status
1 Same uncaught throw remains in TemplateSimplifier::simplifyCalculations AI review lib/vf_common.cpp:141 🆕 New

@ludviggunne, please fix, resolve or reply to the open and new comments: #1.

Testing

  • I built cppcheck and testrunner in pr/ and cppcheck in base/ with no errors.
  • ./testrunner TestTokenizer TestValueFlow TestMathLib: 520 tests, 0 failed. The new nonStandardCharLiteral test passes.
  • I compared base and PR with --enable=portability,warning,style on scratch files that use invalid escapes in many contexts: initialisers, '\s' + 1, array initialisers, case '\s':, local variables, '\z' << 2, unsigned u = '\y', '\8', '\xZZ', u8'\s', L'\s', constexpr, static_assert, array sizes, enum values, bit-fields, template arguments, and a condition inside the body of an instantiated template.
    • The base gave internalError on each file.
    • The PR gives only nonStandardCharLiteral portability warnings.
    • #if '\s' gives a simplecpp syntaxError, as it did before; the PR does not change that.
  • Case still failing in the PR (base fails the same way):
    // e4.cpp
    template<bool B> struct V {};
    V<('\s' == 1)> v;
    Command and output:
    $ pr/cppcheck -q e4.cpp
    e4.cpp:2:4: error: Internal Error. MathLib::toBigNumber: characterLiteralToLL('\s') => invalid escape sequence [internalError]
    
    U<('\s' > 3)> u2; gives the same error. gdb shows that the throw comes from TemplateSimplifier::simplifyCalculations (templatesimplifier.cpp:2769), called from simplifyTemplateArgs → addInstantiation. Without the parentheses (U<'\s' > 3> u2;) there is no error in the PR.

Automated review of 700b926 by Claude. The pull request was built and tested locally.

Comment thread lib/vf_common.cpp
setTokenValue(tok, std::move(value), settings);
} catch (const std::exception & /*e*/) {
// Bad character literal
} catch (const InternalError & /*e*/) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AI review

This catches the throw in valueflow. But TemplateSimplifier::simplifyCalculations (templatesimplifier.cpp:2766-2769) also calls MathLib::toBigNumber(tok) on a %char% token with no catch. Inside template arguments the invalid escape still gives an internal error:

template<bool B> struct V {};
V<('\s' == 1)> v;
$ ./cppcheck -q e4.cpp
e4.cpp:2:4: error: Internal Error. MathLib::toBigNumber: characterLiteralToLL('\s') => invalid escape sequence [internalError]

The same happens with U<('\s' > 3)> u2; for template<int N> struct U {};. To fix it, catch the error there too and leave the token unchanged, and add a test for it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants