Merge lp:~mc-return/compiz/compiz.merge-fix-memory-leaks-in-libcompizconfig into lp:compiz/0.9.10

Proposed by MC Return
Status: Merged
Approved by: Sam Spilsbury
Approved revision: 3467
Merged at revision: 3644
Proposed branch: lp:~mc-return/compiz/compiz.merge-fix-memory-leaks-in-libcompizconfig
Merge into: lp:compiz/0.9.10
Diff against target: 101 lines (+32/-4)
1 file modified
compizconfig/libcompizconfig/src/main.c (+32/-4)
To merge this branch: bzr merge lp:~mc-return/compiz/compiz.merge-fix-memory-leaks-in-libcompizconfig
Reviewer Review Type Date Requested Status
PS Jenkins bot (community) continuous-integration Approve
Sam Spilsbury Approve
MC Return Needs Resubmitting
Daniel van Vugt Pending
Review via email: mp+156677@code.launchpad.net

This proposal supersedes a proposal from 2012-11-08.

Commit message

Hopefully fixed all memory leaks in libcompizconfig.
Minor whitespace fixes.

(LP: #1076297)

Description of the change

Note: Usually switch-case commands do not use {} brackets for the individual cases, but I kept the style already used.

Note 2: case TypeAction has no break, so it is the same as default (probably intentional).

To post a comment you must log in.
Revision history for this message
Daniel van Vugt (vanvugt) wrote : Posted in a previous version of this proposal

One branch/proposal per bug please. It gets too confusing for Launchpad when there are multiple branches to fix a single bug.

review: Needs Resubmitting
Revision history for this message
MC Return (mc-return) wrote : Posted in a previous version of this proposal

> One branch/proposal per bug please. It gets too confusing for Launchpad when
> there are multiple branches to fix a single bug.

Created a new bug report for memory leaks in libcompizconfig.
The old one showed wrong line numbers anyway, so it was outdated already.

(Info: cppcheck seems to be wrong on the third reported leak of the pointer upgradeName, so I did not fix it)

review: Needs Resubmitting
Revision history for this message
Daniel van Vugt (vanvugt) wrote : Posted in a previous version of this proposal

There's a return under asprintf that needs fixing also:
    char *keyName = NULL;
    char *sectionName = strdup (ccsPluginGetName (ccsSettingGetParent (setting)));
    char *iniValue = NULL;

    CCSContextPrivate *cPrivate = GET_PRIVATE (CCSContextPrivate, context);

    if (asprintf (&keyName, "+s%d_%s", cPrivate->screenNum, ccsSettingGetName (setting)) == -1)
---> return FALSE;

    if (ccsIniGetString (dict, sectionName, keyName, &iniValue))
    {
        CCSSetting *newSetting = malloc (sizeof (CCSSetting));

        if (!newSetting)
        {
            free (sectionName);
            return FALSE;
        }

and also two more in ccsProcessSettingMinus ...

        if (!newSetting)
---> return FALSE;
        ...
           default:
                /* FIXME: cleanup */
---> return FALSE;

review: Needs Fixing
Revision history for this message
MC Return (mc-return) wrote : Posted in a previous version of this proposal

> There's a return under asprintf that needs fixing also:
> char *keyName = NULL;
> char *sectionName = strdup (ccsPluginGetName (ccsSettingGetParent
> (setting)));
> char *iniValue = NULL;
>
> CCSContextPrivate *cPrivate = GET_PRIVATE (CCSContextPrivate, context);
>
> if (asprintf (&keyName, "+s%d_%s", cPrivate->screenNum, ccsSettingGetName
> (setting)) == -1)
> ---> return FALSE;
>
> if (ccsIniGetString (dict, sectionName, keyName, &iniValue))
> {
> CCSSetting *newSetting = malloc (sizeof (CCSSetting));
>
> if (!newSetting)
> {
> free (sectionName);
> return FALSE;
> }
>
> and also two more in ccsProcessSettingMinus ...
>
> if (!newSetting)
> ---> return FALSE;
> ...
> default:
> /* FIXME: cleanup */
> ---> return FALSE;

Oh - you are right. Thanks for making me aware - I did not notice it :)
Hopefully fixed in r3459.

Revision history for this message
Daniel van Vugt (vanvugt) wrote : Posted in a previous version of this proposal

I think the FIXME needs to be removed and replaced with:
    free (newSetting)
at least.

review: Needs Fixing
Revision history for this message
MC Return (mc-return) wrote : Posted in a previous version of this proposal

> I think the FIXME needs to be removed and replaced with:
> free (newSetting)
> at least.

Done in r3461.

review: Needs Resubmitting
Revision history for this message
MC Return (mc-return) wrote : Posted in a previous version of this proposal

Ping.

Revision history for this message
Daniel van Vugt (vanvugt) wrote : Posted in a previous version of this proposal

Another FIXME in the area of the code being changed (line 4422)
            default:
                /* FIXME: cleanup */
                return FALSE;

review: Needs Fixing
Revision history for this message
MC Return (mc-return) wrote : Posted in a previous version of this proposal

> Another FIXME in the area of the code being changed (line 4422)
> default:
> /* FIXME: cleanup */
> return FALSE;

Hopefully fixed in r3462. :)

review: Needs Resubmitting
Revision history for this message
Sam Spilsbury (smspillaz) wrote : Posted in a previous version of this proposal

Thats correct.
On 20/11/2012 9:24 AM, "MC Return" <email address hidden> wrote:

> The proposal to merge
> lp:~mc-return/compiz/compiz.merge-fix-memory-leaks-in-libcompizconfig into
> lp:compiz has been updated.
>
> Description changed to:
>
> Note: Usually switch-case commands do not use {} brackets for the
> individual cases, but I kept the style already used.
>
> Note 2: case TypeAction has no break, so it is the same as default
> (probably intentional).
>
> For more details, see:
>
> https://code.launchpad.net/~mc-return/compiz/compiz.merge-fix-memory-leaks-in-libcompizconfig/+merge/133432
> --
>
> https://code.launchpad.net/~mc-return/compiz/compiz.merge-fix-memory-leaks-in-libcompizconfig/+merge/133432
> Your team Compiz Maintainers is subscribed to branch lp:compiz.
>

Revision history for this message
Daniel van Vugt (vanvugt) wrote : Posted in a previous version of this proposal

Second, third, fifth and sixth blocks are missing two frees each:
    free (keyName);
    free (iniValue);
So that's two frees missing twice in each function = 4 frees.

Sorry I didn't notice before.

I recommend a single return statement per function to simplify memory management and easily avoid leaks of any sort:

Bool ret = FALSE;
void *p1 = NULL;
void *p2 = NULL;
...
if (error)
    goto cleanup;
...
if (some other error)
    goto cleanup;
...
ret = TRUE;
cleanup:
free (p1);
free (p2);
return ret;

free() is safe to use with NULL. But for other resource types you'd just use "if (r != INVALID) FreeR(r)".

review: Needs Fixing
Revision history for this message
Sam Spilsbury (smspillaz) wrote : Posted in a previous version of this proposal

Avoid using goto please.

Use a dedicated cleanup function.

void freeFunctionData (Foo *, Bar *)
{
free (foo);
barDestroy (bar);
}
On 20/11/2012 6:29 PM, "Daniel van Vugt" <email address hidden>
wrote:

> Review: Needs Fixing
>
> Second, third, fifth and sixth blocks are missing two frees each:
> free (keyName);
> free (iniValue);
> So that's two frees missing twice in each function = 4 frees.
>
> Sorry I didn't notice before.
>
> I recommend a single return statement per function to simplify memory
> management and easily avoid leaks of any sort:
>
> Bool ret = FALSE;
> void *p1 = NULL;
> void *p2 = NULL;
> ...
> if (error)
> goto cleanup;
> ...
> if (some other error)
> goto cleanup;
> ...
> ret = TRUE;
> cleanup:
> free (p1);
> free (p2);
> return ret;
>
> free() is safe to use with NULL. But for other resource types you'd just
> use "if (r != INVALID) FreeR(r)".
> --
>
> https://code.launchpad.net/~mc-return/compiz/compiz.merge-fix-memory-leaks-in-libcompizconfig/+merge/133432
> Your team Compiz Maintainers is subscribed to branch lp:compiz.
>

Revision history for this message
MC Return (mc-return) wrote :

I did not rewrite everything here, but the leaks should be fixed.
IMHO we do not really need anything more complicated here...

review: Needs Resubmitting
Revision history for this message
Sam Spilsbury (smspillaz) wrote :

This will do for now, but the relevant cleanup should be refactored.

review: Approve
Revision history for this message
PS Jenkins bot (ps-jenkins) :
review: Approve (continuous-integration)
Revision history for this message
Marco Trevisan (TreviƱo) (3v1n0) wrote : Posted in a previous version of this proposal

Could you please resubmit this for 0.9.9 as well?

Thanks.

review: Needs Resubmitting
Revision history for this message
MC Return (mc-return) wrote : Posted in a previous version of this proposal

@3v1nO:
I could do that, but this is not the only important fix that went into 0.9.10-dev.
It would mean a lot of non-exciting work and would cost a lot of time to backport everything important to 0.9.9...
I am sure I do not want to do that unpaid in my free time...

The best solution would be to make current 0.9.10-dev the 0.9.9.2 release.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'compizconfig/libcompizconfig/src/main.c'
--- compizconfig/libcompizconfig/src/main.c 2013-02-21 18:33:06 +0000
+++ compizconfig/libcompizconfig/src/main.c 2013-04-02 20:34:21 +0000
@@ -4376,14 +4376,22 @@
4376 CCSContextPrivate *cPrivate = GET_PRIVATE (CCSContextPrivate, context);4376 CCSContextPrivate *cPrivate = GET_PRIVATE (CCSContextPrivate, context);
43774377
4378 if (asprintf (&keyName, "+s%d_%s", cPrivate->screenNum, ccsSettingGetName (setting)) == -1)4378 if (asprintf (&keyName, "+s%d_%s", cPrivate->screenNum, ccsSettingGetName (setting)) == -1)
4379 {
4380 free (sectionName);
4379 return FALSE;4381 return FALSE;
4382 }
43804383
4381 if (ccsIniGetString (dict, sectionName, keyName, &iniValue))4384 if (ccsIniGetString (dict, sectionName, keyName, &iniValue))
4382 {4385 {
4383 CCSSetting *newSetting = malloc (sizeof (CCSSetting));4386 CCSSetting *newSetting = malloc (sizeof (CCSSetting));
43844387
4385 if (!newSetting)4388 if (!newSetting)
4389 {
4390 free (sectionName);
4391 free (keyName);
4392 free (iniValue);
4386 return FALSE;4393 return FALSE;
4394 }
43874395
4388 ccsObjectInit (newSetting, &ccsDefaultObjectAllocator);4396 ccsObjectInit (newSetting, &ccsDefaultObjectAllocator);
43894397
@@ -4484,8 +4492,13 @@
4484 }4492 }
4485 case TypeAction:4493 case TypeAction:
4486 default:4494 default:
4487 /* FIXME: cleanup */4495 {
4496 free (newSetting);
4497 free (sectionName);
4498 free (keyName);
4499 free (iniValue);
4488 return FALSE;4500 return FALSE;
4501 }
4489 }4502 }
44904503
4491 CCSSettingList listIter = upgrade->clearValueSettings;4504 CCSSettingList listIter = upgrade->clearValueSettings;
@@ -4514,10 +4527,11 @@
4514 4527
4515 return TRUE;4528 return TRUE;
4516 }4529 }
4517 4530
4518 free (keyName);4531 free (keyName);
4519 free (sectionName);4532 free (sectionName);
4520 4533 free (iniValue);
4534
4521 return FALSE;4535 return FALSE;
4522}4536}
45234537
@@ -4534,14 +4548,22 @@
4534 CCSContextPrivate *cPrivate = GET_PRIVATE (CCSContextPrivate, context);4548 CCSContextPrivate *cPrivate = GET_PRIVATE (CCSContextPrivate, context);
45354549
4536 if (asprintf (&keyName, "-s%d_%s", cPrivate->screenNum, ccsSettingGetName (setting)) == -1)4550 if (asprintf (&keyName, "-s%d_%s", cPrivate->screenNum, ccsSettingGetName (setting)) == -1)
4551 {
4552 free (sectionName);
4537 return FALSE;4553 return FALSE;
4554 }
45384555
4539 if (ccsIniGetString (dict, sectionName, keyName, &iniValue))4556 if (ccsIniGetString (dict, sectionName, keyName, &iniValue))
4540 {4557 {
4541 CCSSetting *newSetting = malloc (sizeof (CCSSetting));4558 CCSSetting *newSetting = malloc (sizeof (CCSSetting));
45424559
4543 if (!newSetting)4560 if (!newSetting)
4561 {
4562 free (sectionName);
4563 free (keyName);
4564 free (iniValue);
4544 return FALSE;4565 return FALSE;
4566 }
45454567
4546 ccsObjectInit (newSetting, &ccsDefaultObjectAllocator);4568 ccsObjectInit (newSetting, &ccsDefaultObjectAllocator);
45474569
@@ -4642,8 +4664,13 @@
4642 }4664 }
4643 case TypeAction:4665 case TypeAction:
4644 default:4666 default:
4645 /* FIXME: cleanup */4667 {
4668 free (newSetting);
4669 free (sectionName);
4670 free (keyName);
4671 free (iniValue);
4646 return FALSE;4672 return FALSE;
4673 }
4647 }4674 }
46484675
4649 CCSSettingList listIter = upgrade->addValueSettings;4676 CCSSettingList listIter = upgrade->addValueSettings;
@@ -4675,6 +4702,7 @@
4675 4702
4676 free (keyName);4703 free (keyName);
4677 free (sectionName);4704 free (sectionName);
4705 free (iniValue);
46784706
4679 return FALSE;4707 return FALSE;
4680}4708}

Subscribers

People subscribed via source and target branches

to all changes: