Skip to content

Commit 7f59eec

Browse files
authored
fixed #498 - avoid potential leak in simplecpp::Macro::parseDefine() (#527)
1 parent 3deeb91 commit 7f59eec

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

simplecpp.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1709,7 +1709,9 @@ namespace simplecpp {
17091709
nameTokDef = nametoken;
17101710
variadic = false;
17111711
variadicOpt = false;
1712+
delete optExpandValue;
17121713
optExpandValue = nullptr;
1714+
delete optNoExpandValue;
17131715
optNoExpandValue = nullptr;
17141716
if (!nameTokDef) {
17151717
valueToken = endToken = nullptr;
@@ -2383,8 +2385,8 @@ namespace simplecpp {
23832385
bool variadicOpt;
23842386

23852387
/** Expansion value for varadic macros with __VA_OPT__ expanded and discarded respectively */
2386-
const TokenList *optExpandValue;
2387-
const TokenList *optNoExpandValue;
2388+
const TokenList *optExpandValue{};
2389+
const TokenList *optNoExpandValue{};
23882390

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

test.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3246,6 +3246,7 @@ static void safe_api()
32463246
#endif
32473247
}
32483248

3249+
// crashes detected by fuzzer
32493250
static void fuzz_crash()
32503251
{
32513252
{
@@ -3260,6 +3261,16 @@ static void fuzz_crash()
32603261
}
32613262
}
32623263

3264+
// memory leaks detected by LSAN/valgrind
3265+
static void leak()
3266+
{
3267+
{ // #498
3268+
const char code[] = "#define e(...)__VA_OPT__()\n"
3269+
"#define e\n";
3270+
(void)preprocess(code, simplecpp::DUI());
3271+
}
3272+
}
3273+
32633274
int main(int argc, char **argv)
32643275
{
32653276
TEST_CASE(backslash);
@@ -3516,5 +3527,7 @@ int main(int argc, char **argv)
35163527

35173528
TEST_CASE(fuzz_crash);
35183529

3530+
TEST_CASE(leak);
3531+
35193532
return numberOfFailedAssertions > 0 ? EXIT_FAILURE : EXIT_SUCCESS;
35203533
}

0 commit comments

Comments
 (0)