Repository navigation
Fix #15298: Internal error with invalid escape sequence. - #9104
ludviggunne wants to merge 4 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.
dmcppcheck
left a comment
There was a problem hiding this comment.
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
cppcheckandtestrunnerin pr/ andcppcheckin base/ with no errors. ./testrunner TestSimplifyTemplate TestTokenizer TestValueFlow: 0 failures.- I ran the PR's cppcheck with
--enable=all --inconclusiveon a mixed file. It covered template argsV<('\s' == 1)>,U<('\s' > 3 && '\q' < 2)>,switch/case, array sizes, arithmetic,static_assert, bit-fields and enums. All of these give onlynonStandardCharLiteral, 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') {}andif (c == 'a' && c == '\q') {}fail the same way. - Without
--enable=stylethere is no error.
- gdb shows that the throw in the PR comes from
Automated review of 5a5606b 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
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
left a comment
There was a problem hiding this comment.
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::simplifyCalculationsnow catches the error. checkIncorrectLogicOperatorno longer gives an internalError either, becauseparseComparisonnow 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
cppcheckandtestrunnerin 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.cppwithV<('\s' == 1)> v;andU<('\s' > 3)> u2;: no output, exit code 0.pr/cppcheck -q --enable=style b.cwithif (c == '\s' && c == '\q') {}andif (c > '\s' || c < 'a') {}: onlynonStandardCharLiteralwarnings.
-
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::stringoperations (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
nonStandardCharLiteralwarnings and ordinary findings.--debug --dumpgave no internal error either. - Conditions:
-
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.
No description provided.