Skip to content

Commit 1cda86e

Browse files
committed
Fix #612: reject floating literals in preprocessor conditions
1 parent b892cf7 commit 1cda86e

2 files changed

Lines changed: 69 additions & 0 deletions

File tree

‎simplecpp.cpp‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3032,12 +3032,26 @@ long long simplecpp::characterLiteralToLL(const std::string& str)
30323032
return multivalue;
30333033
}
30343034

3035+
static bool isFloatingPointLiteral(const std::string &str)
3036+
{
3037+
if (!simplecpp::Token::isNumberLike(str) &&
3038+
!(str.size() > 1U && str[0] == '.' && std::isdigit(static_cast<unsigned char>(str[1]))))
3039+
return false;
3040+
3041+
if (str.find('.') != std::string::npos)
3042+
return true;
3043+
3044+
return str.find_first_of(isHex(str) ? "pP" : "eE") != std::string::npos;
3045+
}
3046+
30353047
/**
30363048
* @throws std::runtime_error thrown on invalid literal
30373049
*/
30383050
static void simplifyNumbers(simplecpp::TokenList &expr)
30393051
{
30403052
for (simplecpp::Token *tok = expr.front(); tok; tok = tok->next) {
3053+
if (isFloatingPointLiteral(tok->str()))
3054+
throw std::runtime_error("floating point literal in preprocessor expression");
30413055
if (tok->str().size() == 1U)
30423056
continue;
30433057
if (tok->str().compare(0,2,"0x") == 0)

‎test.cpp‎

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

2296+
static void ifFloatingPointLiteral() // #612
2297+
{
2298+
simplecpp::OutputList outputList;
2299+
simplecpp::DUI dui;
2300+
dui.defines.emplace_back("A=1");
2301+
2302+
const char issue[] = "#if A == 1.0\n"
2303+
"int i;\n"
2304+
"#endif\n";
2305+
ASSERT_EQUALS("", preprocess(issue, dui, &outputList));
2306+
ASSERT_EQUALS("file0,1,syntax_error,failed to evaluate #if condition, floating point literal in preprocessor expression\n", toString(outputList));
2307+
2308+
outputList.clear();
2309+
2310+
const char otherSpelling[] = "#if 0\n"
2311+
"#elif .5\n"
2312+
"int i;\n"
2313+
"#endif\n";
2314+
ASSERT_EQUALS("", preprocess(otherSpelling, &outputList));
2315+
ASSERT_EQUALS("file0,2,syntax_error,failed to evaluate #elif condition, floating point literal in preprocessor expression\n", toString(outputList));
2316+
2317+
outputList.clear();
2318+
2319+
const char exponent[] = "#if 1e3\n"
2320+
"int i;\n"
2321+
"#endif\n";
2322+
ASSERT_EQUALS("", preprocess(exponent, &outputList));
2323+
ASSERT_EQUALS("file0,1,syntax_error,failed to evaluate #if condition, floating point literal in preprocessor expression\n", toString(outputList));
2324+
2325+
outputList.clear();
2326+
2327+
const char suffix[] = "#if 1.f\n"
2328+
"int i;\n"
2329+
"#endif\n";
2330+
ASSERT_EQUALS("", preprocess(suffix, &outputList));
2331+
ASSERT_EQUALS("file0,1,syntax_error,failed to evaluate #if condition, floating point literal in preprocessor expression\n", toString(outputList));
2332+
2333+
outputList.clear();
2334+
2335+
const char hexadecimal[] = "#if 0x1p3\n"
2336+
"int i;\n"
2337+
"#endif\n";
2338+
ASSERT_EQUALS("", preprocess(hexadecimal, &outputList));
2339+
ASSERT_EQUALS("file0,1,syntax_error,failed to evaluate #if condition, floating point literal in preprocessor expression\n", toString(outputList));
2340+
2341+
outputList.clear();
2342+
2343+
const char integer[] = "#if A == 1\n"
2344+
"int i;\n"
2345+
"#endif\n";
2346+
ASSERT_EQUALS("\nint i ;", preprocess(integer, dui, &outputList));
2347+
ASSERT_EQUALS("", toString(outputList));
2348+
}
2349+
22962350
static void ifCharLiteral()
22972351
{
22982352
const char code[] = "#if ('A'==0x41)\n"
@@ -4717,6 +4771,7 @@ static void runTests(int argc, char **argv, Input input)
47174771
TEST_CASE(ifdef2);
47184772
TEST_CASE(ifndef);
47194773
TEST_CASE(ifA);
4774+
TEST_CASE(ifFloatingPointLiteral); // #612
47204775
TEST_CASE(ifCharLiteral);
47214776
TEST_CASE(ifDefined);
47224777
TEST_CASE(ifDefinedNoPar);

0 commit comments

Comments
 (0)