Skip to content

Commit

Permalink
Fix #409 (fuzzing crash in simplecpp::Macro::expandToken()) (#412)
Browse files Browse the repository at this point in the history
  • Loading branch information
danmar authored Feb 12, 2025
1 parent 48a958f commit 9b0c842
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
2 changes: 1 addition & 1 deletion simplecpp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2125,7 +2125,7 @@ namespace simplecpp {
if (expandArg(&temp, defToken, parametertokens))
macroName = temp.cback()->str();
if (expandArg(&temp, defToken->next->next->next, parametertokens))
macroName += temp.cback()->str();
macroName += temp.cback() ? temp.cback()->str() : "";
else
macroName += defToken->next->next->next->str();
lastToken = defToken->next->next->next;
Expand Down
12 changes: 12 additions & 0 deletions test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1717,6 +1717,17 @@ static void ifDefinedHashHash()
ASSERT_EQUALS("file0,4,#error,#error FOO is enabled\n", toString(outputList));
}

static void ifDefinedHashHash2()
{
// #409
// do not crash when expanding P() (as ## rhs is "null")
// note: gcc outputs "defined E"
const char code[] = "#define P(p)defined E##p\n"
"P()\n";
simplecpp::OutputList outputList;
ASSERT_EQUALS("\n0", preprocess(code, &outputList));
}

static void ifLogical()
{
const char code[] = "#if defined(A) || defined(B)\n"
Expand Down Expand Up @@ -3149,6 +3160,7 @@ int main(int argc, char **argv)
TEST_CASE(ifDefinedInvalid1);
TEST_CASE(ifDefinedInvalid2);
TEST_CASE(ifDefinedHashHash);
TEST_CASE(ifDefinedHashHash2);
TEST_CASE(ifLogical);
TEST_CASE(ifSizeof);
TEST_CASE(elif);
Expand Down

0 comments on commit 9b0c842

Please sign in to comment.