Skip to content

Commit b1a98cf

Browse files
authored
fixed #14732 - fixed potential crash with incomplete preprocessor directive (#8539)
1 parent 0045a09 commit b1a98cf

3 files changed

Lines changed: 34 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: 29 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,33 @@ class TestPreprocessor : public TestFixture {
27152717
ASSERT_EQUALS("\nX\nY=Y\nZ\n", getConfigsStr(filedata));
27162718
}
27172719

2720+
void getConfigsInvalid() { // #14732
2721+
{
2722+
const char filedata[] = "#if<";
2723+
ASSERT_EQUALS("\n", getConfigsStr(filedata));
2724+
}
2725+
{
2726+
const char filedata[] = "#if>";
2727+
ASSERT_EQUALS("\n", getConfigsStr(filedata));
2728+
}
2729+
{
2730+
const char filedata[] = "#if==";
2731+
ASSERT_EQUALS("\n", getConfigsStr(filedata));
2732+
}
2733+
{
2734+
const char filedata[] = "#if<=";
2735+
ASSERT_EQUALS("\n", getConfigsStr(filedata));
2736+
}
2737+
{
2738+
const char filedata[] = "#if>=";
2739+
ASSERT_EQUALS("\n", getConfigsStr(filedata));
2740+
}
2741+
{
2742+
const char filedata[] = "#if!";
2743+
ASSERT_EQUALS("\n", getConfigsStr(filedata));
2744+
}
2745+
}
2746+
27182747
void if_sizeof() { // #4071
27192748
const char code[] = "#if sizeof(unsigned short) == 2\n"
27202749
"Fred & Wilma\n"

0 commit comments

Comments
 (0)