Skip to content

Commit 7cff6d1

Browse files
committed
Fix #726 (#if: ?: treats a zero condition as true unless it is written exactly as 0)
?: and ! compared the operand with the string "0", so zero values such as 00, -0 and 0L were taken as true. Compare the numeric value instead.
1 parent 45de9af commit 7cff6d1

2 files changed

Lines changed: 29 additions & 2 deletions

File tree

‎simplecpp.cpp‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1305,7 +1305,7 @@ void simplecpp::TokenList::constFoldUnaryNotPosNeg(simplecpp::Token *tok)
13051305
tok->setstr(tok->next->str());
13061306
deleteToken(tok->next);
13071307
} else if (tok->op == '!' && tok->next && tok->next->number) {
1308-
tok->setstr(tok->next->str() == "0" ? "1" : "0");
1308+
tok->setstr(stringToLL(tok->next->str()) == 0 ? "1" : "0");
13091309
deleteToken(tok->next);
13101310
} else if (tok->op == '~' && tok->next && tok->next->number) {
13111311
tok->setstr(toString(~stringToLL(tok->next->str())));
@@ -1571,7 +1571,7 @@ void simplecpp::TokenList::constFoldQuestionOp(Token *&tok1)
15711571
deleteToken(trueTok);
15721572
deleteToken(falseTok);
15731573
} else {
1574-
Token * const result = (condTok->str() != "0" ? trueTok : falseTok);
1574+
Token * const result = (stringToLL(condTok->str()) != 0 ? trueTok : falseTok);
15751575
if (condTok == tok1)
15761576
tok1 = result;
15771577
deleteToken(condTok->next); // ?

‎test.cpp‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2887,6 +2887,32 @@ static void ifShortCircuit()
28872887
ASSERT_EQUALS("file0,1,syntax_error,failed to evaluate #if condition, division/modulo by zero\n", toString(outputList));
28882888
}
28892889

2890+
static void ifZeroCondition()
2891+
{
2892+
// a zero value that is not written as 0 is false
2893+
const char * const exprs[] = {
2894+
"00 ? 1 : 0",
2895+
"-0 ? 1 : 0",
2896+
"-X ? 1 : 0",
2897+
"0L ? 1 : 0",
2898+
"!00 ? 0 : 1",
2899+
"!0L ? 0 : 1"
2900+
};
2901+
for (const char * const expr : exprs) {
2902+
const std::string code = std::string("#if ") + expr + "\n"
2903+
"1\n"
2904+
"#else\n"
2905+
"2\n"
2906+
"#endif\n";
2907+
ASSERT_EQUALS("\n\n\n2", preprocess(code.c_str()));
2908+
}
2909+
2910+
// the selected operand is evaluated
2911+
simplecpp::OutputList outputList;
2912+
ASSERT_EQUALS("", preprocess("#if 00 ? 1 : 1/0\n#endif\n", &outputList));
2913+
ASSERT_EQUALS("file0,1,syntax_error,failed to evaluate #if condition, division/modulo by zero\n", toString(outputList));
2914+
}
2915+
28902916
static void ifTrueFalse()
28912917
{
28922918
const char code[] = "#if true && !false\n"
@@ -5123,6 +5149,7 @@ static void runTests(int argc, char **argv, Input input)
51235149
TEST_CASE(ifoverflow);
51245150
TEST_CASE(ifdiv0);
51255151
TEST_CASE(ifShortCircuit);
5152+
TEST_CASE(ifZeroCondition);
51265153
TEST_CASE(ifTrueFalse);
51275154
TEST_CASE(ifalt); // using "and", "or", etc
51285155
TEST_CASE(ifexpr);

0 commit comments

Comments
 (0)