Skip to content

Commit 70d7646

Browse files
danmardmcppcheckclaude
authored
Fix #731 (#if: an operator with a missing operand silently evaluates to false) (#732)
Co-authored-by: Daniel Marjamäki <daniel.marjamaki@cppcheck.com> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent a62b8ee commit 70d7646

2 files changed

Lines changed: 48 additions & 3 deletions

File tree

‎simplecpp.cpp‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3283,8 +3283,9 @@ static long long evaluate(simplecpp::TokenList &expr, const simplecpp::DUI &dui,
32833283
simplifyName(expr, dui);
32843284
simplifyNumbers(expr);
32853285
expr.constFold();
3286-
// TODO: handle invalid expressions
3287-
return expr.cfront() && expr.cfront() == expr.cback() && expr.cfront()->number ? stringToLL(expr.cfront()->str()) : 0LL;
3286+
if (!expr.cfront() || expr.cfront() != expr.cback() || !expr.cfront()->number)
3287+
throw std::runtime_error("invalid expression");
3288+
return stringToLL(expr.cfront()->str());
32883289
}
32893290

32903291
static const simplecpp::Token *gotoNextLine(const simplecpp::Token *tok)

‎test.cpp‎

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1842,7 +1842,8 @@ static void hashEscapeTrailingBackslash()
18421842
"#endif\n";
18431843
simplecpp::OutputList outputList;
18441844
preprocess(code, &outputList);
1845-
ASSERT_EQUALS(std::string("file0,2,portability_stringify,") + msg, toString(outputList));
1845+
ASSERT_EQUALS(std::string("file0,2,portability_stringify,") + msg +
1846+
"file0,2,syntax_error,failed to evaluate #if condition, invalid expression\n", toString(outputList));
18461847
}
18471848
{
18481849
// even number of backslashes is fine
@@ -2942,6 +2943,48 @@ static void ifUnaryOperators()
29422943
}
29432944
}
29442945

2946+
static void ifInvalidExpression()
2947+
{
2948+
const char * const exprs[] = {
2949+
// missing operand
2950+
"1 +",
2951+
"(1 +)",
2952+
"(1 +) + 1",
2953+
"* 1",
2954+
"(* 1)",
2955+
"1 + * 2",
2956+
"1 <<",
2957+
"(1 ==)",
2958+
"1 &",
2959+
"(1 &&)",
2960+
"|| 1",
2961+
"!",
2962+
"(!)",
2963+
"-",
2964+
"~",
2965+
"(? 1 : 2)",
2966+
"(1 ? 2 :)",
2967+
"(1 ? 2 3)",
2968+
"(1 ? : 2)",
2969+
// missing operator
2970+
"1 2",
2971+
"(1 2)",
2972+
// empty parentheses
2973+
"()",
2974+
"1 + ()",
2975+
// syntax errors are reported also in operands that are not evaluated
2976+
"0 && (1 +)"
2977+
};
2978+
for (const char * const expr : exprs) {
2979+
const std::string code = std::string("#if ") + expr + "\n"
2980+
"1\n"
2981+
"#endif\n";
2982+
simplecpp::OutputList outputList;
2983+
ASSERT_EQUALS("", preprocess(code.c_str(), &outputList));
2984+
ASSERT_EQUALS("file0,1,syntax_error,failed to evaluate #if condition, invalid expression\n", toString(outputList));
2985+
}
2986+
}
2987+
29452988
static void ifTrueFalse()
29462989
{
29472990
const char code[] = "#if true && !false\n"
@@ -5180,6 +5223,7 @@ static void runTests(int argc, char **argv, Input input)
51805223
TEST_CASE(ifShortCircuit);
51815224
TEST_CASE(ifZeroCondition);
51825225
TEST_CASE(ifUnaryOperators);
5226+
TEST_CASE(ifInvalidExpression);
51835227
TEST_CASE(ifTrueFalse);
51845228
TEST_CASE(ifalt); // using "and", "or", etc
51855229
TEST_CASE(ifexpr);

0 commit comments

Comments
 (0)