Skip to content

Fix #409 (fuzzing crash in simplecpp::Macro::expandToken()) #412

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading