Skip to content

Commit 5a6006a

Browse files
committed
fixed #14732 - fixed potential crash with incomplete preprocessor directive
1 parent a5a7dd7 commit 5a6006a

3 files changed

Lines changed: 39 additions & 0 deletions

File tree

lib/preprocessor.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -469,6 +469,8 @@ static std::string readcondition(const simplecpp::Token *iftok, const std::set<s
469469
continue;
470470
}
471471
if (cond->str() == "==" || cond->str() == "<=" || cond->str() == ">=") {
472+
if (!cond->next)
473+
break;
472474
if (cond->next->number) {
473475
const simplecpp::Token *dtok = cond->previous;
474476
if (sameline(iftok,dtok) && dtok->name && defined.find(dtok->str()) == defined.end() && undefined.find(dtok->str()) == undefined.end())
@@ -477,6 +479,8 @@ static std::string readcondition(const simplecpp::Token *iftok, const std::set<s
477479
continue;
478480
}
479481
if (cond->op == '<' || cond->op == '>') {
482+
if (!cond->next)
483+
break;
480484
if (cond->next->number) {
481485
const simplecpp::Token *dtok = cond->previous;
482486
if (sameline(iftok,dtok) && dtok->name && defined.find(dtok->str()) == defined.end() && undefined.find(dtok->str()) == undefined.end()) {
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#if<

test/testpreprocessor.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,8 @@ class TestPreprocessor : public TestFixture {
343343
TEST_CASE(getConfigsAndCodeIssue14317);
344344
TEST_CASE(getConfigsMostGeneralConfigIssue14317);
345345

346+
TEST_CASE(getConfigsInvalid); // #14732
347+
346348
TEST_CASE(if_sizeof);
347349

348350
TEST_CASE(invalid_ifs); // #5909
@@ -2715,6 +2717,38 @@ class TestPreprocessor : public TestFixture {
27152717
ASSERT_EQUALS("\nX\nY=Y\nZ\n", getConfigsStr(filedata));
27162718
}
27172719

2720+
// TODO: should these report errors about being incomplete?
2721+
void getConfigsInvalid() { // #14732
2722+
{
2723+
const char filedata[] = "#if<";
2724+
ASSERT_EQUALS("\n", getConfigsStr(filedata));
2725+
}
2726+
{
2727+
const char filedata[] = "#if>";
2728+
ASSERT_EQUALS("\n", getConfigsStr(filedata));
2729+
}
2730+
{
2731+
const char filedata[] = "#if==";
2732+
ASSERT_EQUALS("\n", getConfigsStr(filedata));
2733+
}
2734+
{
2735+
const char filedata[] = "#if<=";
2736+
ASSERT_EQUALS("\n", getConfigsStr(filedata));
2737+
}
2738+
{
2739+
const char filedata[] = "#if>=";
2740+
ASSERT_EQUALS("\n", getConfigsStr(filedata));
2741+
}
2742+
{
2743+
const char filedata[] = "#if!";
2744+
ASSERT_EQUALS("\n", getConfigsStr(filedata));
2745+
}
2746+
{
2747+
const char filedata[] = "#if defined";
2748+
ASSERT_EQUALS("\n", getConfigsStr(filedata));
2749+
}
2750+
}
2751+
27182752
void if_sizeof() { // #4071
27192753
const char code[] = "#if sizeof(unsigned short) == 2\n"
27202754
"Fred & Wilma\n"

0 commit comments

Comments
 (0)