Skip to content

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

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

ludviggunne wants to merge 4 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.

@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

With --enable=style, an invalid escape sequence in a logical comparison still gives an internalError. Valueflow now skips the bad %char% token. But CheckCondition::checkIncorrectLogicOperator (lib/checkcondition.cpp:1311) still calls MathLib::toBigNumber(value1, expr1) on the char string with no catch, so if (c == '\s' && c == '\q') {} aborts the analysis of the whole file. The base fails on the same input, but there the throw comes earlier, from valueflow. So the PR moves the crash for this input and does not fix it. The earlier TemplateSimplifier::simplifyCalculations comment is fixed, and a test covers it.

Comments

# Comment By Where Status
1 Uncaught throw in TemplateSimplifier::simplifyCalculations for invalid char in template args AI review lib/vf_common.cpp:141 ✅ Fixed
2 Invalid char literal still gives internalError in checkIncorrectLogicOperator AI review lib/vf_common.cpp:141 🆕 New

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

Testing

  • I built cppcheck and testrunner in pr/ and cppcheck in base/ with no errors.
  • ./testrunner TestSimplifyTemplate TestTokenizer TestValueFlow: 0 failures.
  • I ran the PR's cppcheck with --enable=all --inconclusive on a mixed file. It covered template args V<('\s' == 1)>, U<('\s' > 3 && '\q' < 2)>, switch/case, array sizes, arithmetic, static_assert, bit-fields and enums. All of these give only nonStandardCharLiteral, except comparisons joined with &&/||.
  • Internal error that remains (b.c):
    void f(char c) {
      if (c == '\s' && c == '\q') {}
    }
    $ pr/cppcheck -q --enable=style b.c
    b.c:2:12: portability: Non-standard character literal '\s'. invalid escape sequence [nonStandardCharLiteral]
    b.c:2:25: portability: Non-standard character literal '\q'. invalid escape sequence [nonStandardCharLiteral]
    b.c:2:7: error: Internal Error. MathLib::toBigNumber: characterLiteralToLL('\s') => invalid escape sequence [internalError]
    $ base/cppcheck -q --enable=style b.c
    b.c:2:12: error: Internal Error. MathLib::toBigNumber: characterLiteralToLL('\s') => invalid escape sequence [internalError]
    
    • gdb shows that the throw in the PR comes from CheckConditionImpl::checkIncorrectLogicOperator (checkcondition.cpp:1311).
    • One valid literal is enough to trigger it: if (c > '\s' || c < 'a') {} and if (c == 'a' && c == '\q') {} fail the same way.
    • Without --enable=style there is no error.

Automated review of 5a5606b 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

With this catch, valueflow skips the bad literal. Checks that convert %char% tokens themselves still throw, though. CheckConditionImpl::checkIncorrectLogicOperator (checkcondition.cpp:1311) calls MathLib::toBigNumber(value1, expr1) on the string that parseComparison returns, and nothing catches the error:

void f(char c) {
  if (c == '\s' && c == '\q') {}
}
$ cppcheck -q --enable=style b.c
b.c:2:7: error: Internal Error. MathLib::toBigNumber: characterLiteralToLL('\s') => invalid escape sequence [internalError]

if (c > '\s' || c < 'a') {} fails the same way. The base also fails on this input, but there the throw comes from valueflow; the PR only moves the crash. Two possible fixes: make parseComparison reject char literals that can't be converted, or catch the error in checkIncorrectLogicOperator. Add a TestCondition case for it.

@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

I found no new problems. Both problems from the earlier reviews are fixed:

  • Invalid character literals inside parenthesized template arguments no longer give an internalError. TemplateSimplifier::simplifyCalculations now catches the error.
  • checkIncorrectLogicOperator no longer gives an internalError either, because parseComparison now rejects character literals that it can't convert.

Each of these has a test. The same goes for case '\s' ... 'z' ranges and array sizes. I fuzzed many contexts and literal kinds, and none of them gave an internalError.

Comments

# Comment By Where Status
1 Uncaught throw in TemplateSimplifier::simplifyCalculations for invalid char in template args AI review lib/vf_common.cpp:141 ✅ Fixed
2 Invalid char literal still gives internalError in checkIncorrectLogicOperator AI review lib/vf_common.cpp:141 ✅ Fixed

Testing

  • I built cppcheck and testrunner in pr/ with no errors.

  • ./testrunner TestTokenizer TestSimplifyTemplate TestCondition TestValueFlow TestMathLib: 0 failures.

  • I re-ran the reproducers from the earlier reviews with the PR's cppcheck. Neither gives an internalError any more:

    • pr/cppcheck -q e4.cpp with V<('\s' == 1)> v; and U<('\s' > 3)> u2;: no output, exit code 0.
    • pr/cppcheck -q --enable=style b.c with if (c == '\s' && c == '\q') {} and if (c > '\s' || c < 'a') {}: only nonStandardCharLiteral warnings.
  • I ran --enable=all --inconclusive (some files also with --check-level=exhaustive) on about 25 scratch files. They put invalid literals into these contexts:

    • Conditions: &&/|| comparisons mixed with valid literals and with floats, bitmask comparisons, nested equal conditions, !'\s', '\s' == '\s', ?:.
    • switch: case ranges with a bad literal at either end.
    • Templates: U<'\s'>, U<sizeof('\s')>, U<('\s' + 1)>, U<(1 == '\s')> and default template arguments.
    • Declarations: bit-fields, enum values, array sizes and indexes.
    • Library calls: memset, toupper, isdigit, fputc, printf, std::string operations (with --library=posix).
    • Other literal kinds: 'abcde', u'\U12345678', U'\xFFFFFFFFFF', u8'ab', L'ab', '\777', '\u12', '\U0000D800'.

    None of them gave an internalError; there were only nonStandardCharLiteral warnings and ordinary findings. --debug --dump gave no internal error either.

  • I did not build base/, because the earlier reviews already established the base behaviour.


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

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