Repository navigation
Fix #15298: Internal error with invalid escape sequence. - #9104
ludviggunne wants to merge 2 commits into
Conversation
dmcppcheck
left a comment
There was a problem hiding this comment.
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
cppcheckandtestrunnerin pr/ andcppcheckin base/ with no errors. ./testrunner TestTokenizer TestValueFlow TestMathLib: 520 tests, 0 failed. The newnonStandardCharLiteraltest passes.- I compared base and PR with
--enable=portability,warning,styleon 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
internalErroron each file. - The PR gives only
nonStandardCharLiteralportability warnings. #if '\s'gives a simplecppsyntaxError, as it did before; the PR does not change that.
- The base gave
- Case still failing in the PR (base fails the same way):
Command and output:
// e4.cpp template<bool B> struct V {}; V<('\s' == 1)> v;
$ 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 fromTemplateSimplifier::simplifyCalculations(templatesimplifier.cpp:2769), called fromsimplifyTemplateArgs→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.
| setTokenValue(tok, std::move(value), settings); | ||
| } catch (const std::exception & /*e*/) { | ||
| // Bad character literal | ||
| } catch (const InternalError & /*e*/) { |
There was a problem hiding this comment.
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.
No description provided.