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
1=== modified file 'compizconfig/libcompizconfig/src/main.c'
2--- compizconfig/libcompizconfig/src/main.c 2013-02-21 18:33:06 +0000
3+++ compizconfig/libcompizconfig/src/main.c 2013-04-02 20:34:21 +0000
4@@ -4376,14 +4376,22 @@
5 CCSContextPrivate *cPrivate = GET_PRIVATE (CCSContextPrivate, context);
6
7 if (asprintf (&keyName, "+s%d_%s", cPrivate->screenNum, ccsSettingGetName (setting)) == -1)
8+ {
9+ free (sectionName);
10 return FALSE;
11+ }
12
13 if (ccsIniGetString (dict, sectionName, keyName, &iniValue))
14 {
15 CCSSetting *newSetting = malloc (sizeof (CCSSetting));
16
17 if (!newSetting)
18+ {
19+ free (sectionName);
20+ free (keyName);
21+ free (iniValue);
22 return FALSE;
23+ }
24
25 ccsObjectInit (newSetting, &ccsDefaultObjectAllocator);
26
27@@ -4484,8 +4492,13 @@
28 }
29 case TypeAction:
30 default:
31- /* FIXME: cleanup */
32+ {
33+ free (newSetting);
34+ free (sectionName);
35+ free (keyName);
36+ free (iniValue);
37 return FALSE;
38+ }
39 }
40
41 CCSSettingList listIter = upgrade->clearValueSettings;
42@@ -4514,10 +4527,11 @@
43
44 return TRUE;
45 }
46-
47+
48 free (keyName);
49 free (sectionName);
50-
51+ free (iniValue);
52+
53 return FALSE;
54 }
55
56@@ -4534,14 +4548,22 @@
57 CCSContextPrivate *cPrivate = GET_PRIVATE (CCSContextPrivate, context);
58
59 if (asprintf (&keyName, "-s%d_%s", cPrivate->screenNum, ccsSettingGetName (setting)) == -1)
60+ {
61+ free (sectionName);
62 return FALSE;
63+ }
64
65 if (ccsIniGetString (dict, sectionName, keyName, &iniValue))
66 {
67 CCSSetting *newSetting = malloc (sizeof (CCSSetting));
68
69 if (!newSetting)
70+ {
71+ free (sectionName);
72+ free (keyName);
73+ free (iniValue);
74 return FALSE;
75+ }
76
77 ccsObjectInit (newSetting, &ccsDefaultObjectAllocator);
78
79@@ -4642,8 +4664,13 @@
80 }
81 case TypeAction:
82 default:
83- /* FIXME: cleanup */
84+ {
85+ free (newSetting);
86+ free (sectionName);
87+ free (keyName);
88+ free (iniValue);
89 return FALSE;
90+ }
91 }
92
93 CCSSettingList listIter = upgrade->addValueSettings;
94@@ -4675,6 +4702,7 @@
95
96 free (keyName);
97 free (sectionName);
98+ free (iniValue);
99
100 return FALSE;
101 }

Subscribers

People subscribed via source and target branches

to all changes: