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

Proposed by MC Return
Status: Superseded
Proposed branch: lp:~mc-return/compiz/compiz.merge-fix-memory-leaks-in-libcompizconfig
Merge into: lp:compiz/0.9.9
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
Marco Trevisan (Treviño) Needs Resubmitting
Daniel van Vugt Needs Fixing
MC Return Needs Resubmitting
Review via email: mp+133432@code.launchpad.net

This proposal has been superseded by a proposal from 2013-04-02.

Commit message

libcompizconfig memory leak fixes:

Fixed memory leak in static Bool ccsProcessSettingMinus (...).
Free the memory pointed to by *SectionName if (asprintf (&keyName, "-s%d_%s", cPrivate->screenNum, ccsSettingGetName (setting)) == -1) also.

Fixed memory leak in static Bool ccsProcessSettingPlus (...).
Free the memory *sectionName is pointing to if (!newSetting) before returning FALSE.

(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 :

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 :

> 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 :

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
3459. By MC Return

Hopefully fixed all remaining potential memory leaks caused by sectionName

3460. By MC Return

Merged latest lp:compiz

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

> 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 :

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

review: Needs Fixing
3461. By MC Return

Also free (newSetting) by default if other cases do not match

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

> 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 :

Ping.

Revision history for this message
Daniel van Vugt (vanvugt) wrote :

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

review: Needs Fixing
3462. By MC Return

Free newSetting and sectionName in the case TypeAction and default

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

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

Hopefully fixed in r3462. :)

review: Needs Resubmitting
3463. By MC Return

Added forgotten brackets

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

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 :

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 :

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.
>

3464. By MC Return

Merged latest lp:compiz

3465. By MC Return

Merged latest lp:compiz

3466. By MC Return

Hopefully fixed all remaining leaks

3467. By MC Return

Merged latest lp:compiz

Revision history for this message
Marco Trevisan (Treviño) (3v1n0) wrote :

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 :

@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.

Unmerged revisions

3467. By MC Return

Merged latest lp:compiz

3466. By MC Return

Hopefully fixed all remaining leaks

3465. By MC Return

Merged latest lp:compiz

3464. By MC Return

Merged latest lp:compiz

3463. By MC Return

Added forgotten brackets

3462. By MC Return

Free newSetting and sectionName in the case TypeAction and default

3461. By MC Return

Also free (newSetting) by default if other cases do not match

3460. By MC Return

Merged latest lp:compiz

3459. By MC Return

Hopefully fixed all remaining potential memory leaks caused by sectionName

3458. By MC Return

Fixed memory leak in static Bool ccsProcessSettingMinus (...)
Free the memory pointed to by *SectionName if (asprintf (&keyName, "-s%d_%s", cPrivate->screenNum, ccsSettingGetName (setting)) == -1) also

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-01 12:47:22 +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