Skip to content

Commit 9f2ed55

Browse files
committed
fixed #498 - avoid potential leak in simplecpp::Macro::parseDefine()
1 parent 6c9eaf4 commit 9f2ed55

2 files changed

Lines changed: 17 additions & 2 deletions

File tree

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: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3217,6 +3217,7 @@ static void safe_api()
32173217
#endif
32183218
}
32193219

3220+
// crashes detected by fuzzer
32203221
static void fuzz_crash()
32213222
{
32223223
{
@@ -3231,6 +3232,16 @@ static void fuzz_crash()
32313232
}
32323233
}
32333234

3235+
// memory leaks detected by LSAN/valgrind
3236+
static void leak()
3237+
{
3238+
{ // #498
3239+
const char code[] = "#define e(...)__VA_OPT__()\n"
3240+
"#define e\n";
3241+
(void)preprocess(code, simplecpp::DUI());
3242+
}
3243+
}
3244+
32343245
int main(int argc, char **argv)
32353246
{
32363247
TEST_CASE(backslash);
@@ -3487,5 +3498,7 @@ int main(int argc, char **argv)
34873498

34883499
TEST_CASE(fuzz_crash);
34893500

3501+
TEST_CASE(leak);
3502+
34903503
return numberOfFailedAssertions > 0 ? EXIT_FAILURE : EXIT_SUCCESS;
34913504
}

0 commit comments

Comments
 (0)