Skip to content

Commit 4fa356d

Browse files
committed
fixed #498 - avoid potential leak in simplecpp::Macro::parseDefine()
1 parent 37fa4f4 commit 4fa356d

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

simplecpp.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1693,7 +1693,9 @@ namespace simplecpp {
16931693
nameTokDef = nametoken;
16941694
variadic = false;
16951695
variadicOpt = false;
1696+
delete optExpandValue;
16961697
optExpandValue = nullptr;
1698+
delete optNoExpandValue;
16971699
optNoExpandValue = nullptr;
16981700
if (!nameTokDef) {
16991701
valueToken = endToken = nullptr;
@@ -2367,8 +2369,8 @@ namespace simplecpp {
23672369
bool variadicOpt;
23682370

23692371
/** Expansion value for varadic macros with __VA_OPT__ expanded and discarded respectively */
2370-
const TokenList *optExpandValue;
2371-
const TokenList *optNoExpandValue;
2372+
const TokenList *optExpandValue{};
2373+
const TokenList *optNoExpandValue{};
23722374

23732375
/** was the value of this macro actually defined in the code? */
23742376
bool valueDefinedInCode_;

test.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3231,6 +3231,15 @@ static void fuzz_crash()
32313231
}
32323232
}
32333233

3234+
static void leak()
3235+
{
3236+
{
3237+
const char code[] = "#define e(...)__VA_OPT__()\n"
3238+
"#define e\n";
3239+
(void)preprocess(code, simplecpp::DUI());
3240+
}
3241+
}
3242+
32343243
int main(int argc, char **argv)
32353244
{
32363245
TEST_CASE(backslash);
@@ -3487,5 +3496,7 @@ int main(int argc, char **argv)
34873496

34883497
TEST_CASE(fuzz_crash);
34893498

3499+
TEST_CASE(leak);
3500+
34903501
return numberOfFailedAssertions > 0 ? EXIT_FAILURE : EXIT_SUCCESS;
34913502
}

0 commit comments

Comments
 (0)