Skip to content

Commit 9b0c842

Browse files
authored
Fix #409 (fuzzing crash in simplecpp::Macro::expandToken()) (#412)
1 parent 48a958f commit 9b0c842

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

simplecpp.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -2125,7 +2125,7 @@ namespace simplecpp {
21252125
if (expandArg(&temp, defToken, parametertokens))
21262126
macroName = temp.cback()->str();
21272127
if (expandArg(&temp, defToken->next->next->next, parametertokens))
2128-
macroName += temp.cback()->str();
2128+
macroName += temp.cback() ? temp.cback()->str() : "";
21292129
else
21302130
macroName += defToken->next->next->next->str();
21312131
lastToken = defToken->next->next->next;

test.cpp

+12
Original file line numberDiff line numberDiff line change
@@ -1717,6 +1717,17 @@ static void ifDefinedHashHash()
17171717
ASSERT_EQUALS("file0,4,#error,#error FOO is enabled\n", toString(outputList));
17181718
}
17191719

1720+
static void ifDefinedHashHash2()
1721+
{
1722+
// #409
1723+
// do not crash when expanding P() (as ## rhs is "null")
1724+
// note: gcc outputs "defined E"
1725+
const char code[] = "#define P(p)defined E##p\n"
1726+
"P()\n";
1727+
simplecpp::OutputList outputList;
1728+
ASSERT_EQUALS("\n0", preprocess(code, &outputList));
1729+
}
1730+
17201731
static void ifLogical()
17211732
{
17221733
const char code[] = "#if defined(A) || defined(B)\n"
@@ -3149,6 +3160,7 @@ int main(int argc, char **argv)
31493160
TEST_CASE(ifDefinedInvalid1);
31503161
TEST_CASE(ifDefinedInvalid2);
31513162
TEST_CASE(ifDefinedHashHash);
3163+
TEST_CASE(ifDefinedHashHash2);
31523164
TEST_CASE(ifLogical);
31533165
TEST_CASE(ifSizeof);
31543166
TEST_CASE(elif);

0 commit comments

Comments
 (0)