Skip to content

Commit b68043f

Browse files
committed
handle other cases
1 parent 5a5606b commit b68043f

5 files changed

Lines changed: 36 additions & 3 deletions

File tree

‎lib/checkcondition.cpp‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "checkcondition.h"
2424

2525
#include "astutils.h"
26+
#include "errortypes.h"
2627
#include "library.h"
2728
#include "platform.h"
2829
#include "settings.h"
@@ -1112,8 +1113,18 @@ static bool parseComparison(const Token *comp, bool &not1, std::string &op, std:
11121113

11131114
inconclusive = inconclusive || (value[0] == '\'' && !(op == "!=" || op == "=="));
11141115

1116+
if (value[0] == '\'') {
1117+
// Bad character literal
1118+
try {
1119+
(void)MathLib::toBigNumber(value, expr);
1120+
} catch (const InternalError & /*e*/) {
1121+
return false;
1122+
}
1123+
return true;
1124+
}
1125+
11151126
// Only float and int values are currently handled
1116-
return MathLib::isInt(value) || MathLib::isFloat(value) || (value[0] == '\'');
1127+
return MathLib::isInt(value) || MathLib::isFloat(value);
11171128
}
11181129

11191130
static std::string conditionString(bool not1, const Token *expr1, const std::string &op, const std::string &value1)

‎lib/tokenize.cpp‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4192,8 +4192,13 @@ void Tokenizer::simplifyCaseRange()
41924192
{
41934193
for (Token* tok = list.front(); tok; tok = tok->next()) {
41944194
if (Token::Match(tok, "case %num%|%char% ... %num%|%char% :")) {
4195-
const MathLib::bigint start = MathLib::toBigNumber(tok->tokAt(1));
4196-
MathLib::bigint end = MathLib::toBigNumber(tok->tokAt(3));
4195+
MathLib::bigint start, end;
4196+
try {
4197+
start = MathLib::toBigNumber(tok->tokAt(1));
4198+
end = MathLib::toBigNumber(tok->tokAt(3));
4199+
} catch (const InternalError & /*e*/) {
4200+
continue; // Bad character literal
4201+
}
41974202
end = std::min(start + 50, end); // Simplify it 50 times at maximum
41984203
if (start < end) {
41994204
tok = tok->tokAt(2);

‎lib/vf_common.cpp‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,9 @@ namespace ValueFlow
326326
catch (const std::exception& /*e*/) {
327327
// Bad integer literal
328328
}
329+
catch (const InternalError& /*e*/) {
330+
// Bad character literal
331+
}
329332
}
330333
sz = 0;
331334
break;

‎test/testcondition.cpp‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ class TestCondition : public TestFixture {
7070
TEST_CASE(incorrectLogicOperator16); // #10070
7171
TEST_CASE(incorrectLogicOperator17);
7272
TEST_CASE(incorrectLogicOperator18); // #12282
73+
TEST_CASE(incorrectLogicOperatorBadCharLiteral);
7374
TEST_CASE(secondAlwaysTrueFalseWhenFirstTrueError);
7475
TEST_CASE(incorrectLogicOp_condSwapping);
7576
TEST_CASE(testBug5895);
@@ -1785,6 +1786,13 @@ class TestCondition : public TestFixture {
17851786
ASSERT_EQUALS("", errout_str());
17861787
}
17871788

1789+
void incorrectLogicOperatorBadCharLiteral() {
1790+
check("void f(char c) {\n"
1791+
" if (c == '\\s' && c == '\\q') {}\n"
1792+
"}\n");
1793+
ASSERT_EQUALS("", errout_str());
1794+
}
1795+
17881796
void incorrectLogicOperator17() { // #12471
17891797
check("struct R {\n"
17901798
" void set() { i = 1; }\n"

‎test/testtokenize.cpp‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9554,6 +9554,12 @@ class TestTokenizer : public TestFixture {
95549554
const Settings s = settingsBuilder(settings2).severity(Severity::portability).build();
95559555
(void)tokenizeAndStringify("int a = '\\s';\n", s, false);
95569556
ASSERT_EQUALS("[test.c:1:9]: (portability) Non-standard character literal '\\s'. invalid escape sequence [nonStandardCharLiteral]\n", errout_str());
9557+
9558+
(void)tokenizeAndStringify("void f(char c) { switch (c) { case '\\s' ... 'z': break; } }\n", s, false);
9559+
ASSERT_EQUALS("[test.c:1:36]: (portability) Non-standard character literal '\\s'. invalid escape sequence [nonStandardCharLiteral]\n", errout_str());
9560+
9561+
(void)tokenizeAndStringify("int n = sizeof(char['\\s']);\n", s, false);
9562+
ASSERT_EQUALS("[test.c:1:21]: (portability) Non-standard character literal '\\s'. invalid escape sequence [nonStandardCharLiteral]\n", errout_str());
95579563
}
95589564
};
95599565

0 commit comments

Comments
 (0)