Skip to content

Commit ca3426a

Browse files
authored
Fix #612: reject floating literals in preprocessor conditions (#706)
1 parent ab23e4a commit ca3426a

2 files changed

Lines changed: 70 additions & 0 deletions

File tree

‎simplecpp.cpp‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3123,12 +3123,27 @@ long long simplecpp::characterLiteralToLL(const std::string& str)
31233123
return multivalue;
31243124
}
31253125

3126+
static bool isFloatingPointLiteral(const std::string &str)
3127+
{
3128+
const bool startsWithDotDigit = str.size() > 1U && str[0] == '.' &&
3129+
std::isdigit(static_cast<unsigned char>(str[1]));
3130+
if (!simplecpp::Token::isNumberLike(str) && !startsWithDotDigit)
3131+
return false;
3132+
3133+
if (str.find('.') != std::string::npos)
3134+
return true;
3135+
3136+
return str.find_first_of(isHex(str) ? "pP" : "eE") != std::string::npos;
3137+
}
3138+
31263139
/**
31273140
* @throws std::runtime_error thrown on invalid literal
31283141
*/
31293142
static void simplifyNumbers(simplecpp::TokenList &expr)
31303143
{
31313144
for (simplecpp::Token *tok = expr.front(); tok; tok = tok->next) {
3145+
if (isFloatingPointLiteral(tok->str()))
3146+
throw std::runtime_error("floating point literal in preprocessor expression");
31323147
if (tok->str().size() == 1U)
31333148
continue;
31343149
if (tok->str().compare(0,2,"0x") == 0)

‎test.cpp‎

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2471,6 +2471,60 @@ static void ifA()
24712471
ASSERT_EQUALS("\nX", preprocess(code, dui));
24722472
}
24732473

2474+
static void ifFloatingPointLiteral() // #612
2475+
{
2476+
simplecpp::OutputList outputList;
2477+
simplecpp::DUI dui;
2478+
dui.defines.emplace_back("A=1");
2479+
2480+
const char issue[] = "#if A == 1.0\n"
2481+
"int i;\n"
2482+
"#endif\n";
2483+
ASSERT_EQUALS("", preprocess(issue, dui, &outputList));
2484+
ASSERT_EQUALS("file0,1,syntax_error,failed to evaluate #if condition, floating point literal in preprocessor expression\n", toString(outputList));
2485+
2486+
outputList.clear();
2487+
2488+
const char otherSpelling[] = "#if 0\n"
2489+
"#elif .5\n"
2490+
"int i;\n"
2491+
"#endif\n";
2492+
ASSERT_EQUALS("", preprocess(otherSpelling, &outputList));
2493+
ASSERT_EQUALS("file0,2,syntax_error,failed to evaluate #elif condition, floating point literal in preprocessor expression\n", toString(outputList));
2494+
2495+
outputList.clear();
2496+
2497+
const char exponent[] = "#if 1e3\n"
2498+
"int i;\n"
2499+
"#endif\n";
2500+
ASSERT_EQUALS("", preprocess(exponent, &outputList));
2501+
ASSERT_EQUALS("file0,1,syntax_error,failed to evaluate #if condition, floating point literal in preprocessor expression\n", toString(outputList));
2502+
2503+
outputList.clear();
2504+
2505+
const char suffix[] = "#if 1.f\n"
2506+
"int i;\n"
2507+
"#endif\n";
2508+
ASSERT_EQUALS("", preprocess(suffix, &outputList));
2509+
ASSERT_EQUALS("file0,1,syntax_error,failed to evaluate #if condition, floating point literal in preprocessor expression\n", toString(outputList));
2510+
2511+
outputList.clear();
2512+
2513+
const char hexadecimal[] = "#if 0x1p3\n"
2514+
"int i;\n"
2515+
"#endif\n";
2516+
ASSERT_EQUALS("", preprocess(hexadecimal, &outputList));
2517+
ASSERT_EQUALS("file0,1,syntax_error,failed to evaluate #if condition, floating point literal in preprocessor expression\n", toString(outputList));
2518+
2519+
outputList.clear();
2520+
2521+
const char integer[] = "#if A == 1\n"
2522+
"int i;\n"
2523+
"#endif\n";
2524+
ASSERT_EQUALS("\nint i ;", preprocess(integer, dui, &outputList));
2525+
ASSERT_EQUALS("", toString(outputList));
2526+
}
2527+
24742528
static void ifCharLiteral()
24752529
{
24762530
const char code[] = "#if ('A'==0x41)\n"
@@ -4973,6 +5027,7 @@ static void runTests(int argc, char **argv, Input input)
49735027
TEST_CASE(ifdef2);
49745028
TEST_CASE(ifndef);
49755029
TEST_CASE(ifA);
5030+
TEST_CASE(ifFloatingPointLiteral); // #612
49765031
TEST_CASE(ifCharLiteral);
49775032
TEST_CASE(ifDefined);
49785033
TEST_CASE(ifDefinedNoPar);

0 commit comments

Comments
 (0)