After an evaluation, GNOME has moved from Bugzilla to GitLab. Learn more about GitLab.
No new issues can be reported in GNOME Bugzilla anymore.
To report an issue in a GNOME project, go to GNOME GitLab.
Do not go to GNOME Gitlab for: Bluefish, Doxygen, GnuCash, GStreamer, java-gnome, LDTP, NetworkManager, Tomboy.
Bug 669412 - mem leak in g_environ_unsetenv
mem leak in g_environ_unsetenv
Status: RESOLVED FIXED
Product: glib
Classification: Platform
Component: general
unspecified
Other Linux
: Normal normal
: ---
Assigned To: gtkdev
gtkdev
Depends on:
Blocks:
 
 
Reported: 2012-02-05 15:01 UTC by Christian Persch
Modified: 2012-02-15 16:46 UTC
See Also:
GNOME target: ---
GNOME version: ---


Attachments
Plug a mem leak in g_environ_unsetenv (1.45 KB, patch)
2012-02-05 15:02 UTC, Christian Persch
needs-work Details | Review
Note that g_environ_setenv() already had the requirement that the passed array was (4.72 KB, patch)
2012-02-05 21:48 UTC, Christian Persch
accepted-commit_now Details | Review

Description Christian Persch 2012-02-05 15:01:44 UTC
It forgets to free the value of the removed element.
Comment 1 Christian Persch 2012-02-05 15:02:22 UTC
Created attachment 206833 [details] [review]
Plug a mem leak in g_environ_unsetenv

==9458== 10 bytes in 1 blocks are definitely lost in loss record 16 of 39
==9458==    at 0x402AD89: malloc (vg_replace_malloc.c:236)
==9458==    by 0x4221A1F: vasprintf (vasprintf.c:78)
==9458==    by 0x40C6065: g_vasprintf (gprintf.c:314)
==9458==    by 0x409D894: g_strdup_vprintf (gstrfuncs.c:509)
==9458==    by 0x409D8C9: g_strdup_printf (gstrfuncs.c:535)
==9458==    by 0x40672E9: g_environ_setenv (genviron.c:156)
==9458==    by 0x80490E7: test_environ_array (environment.c:78)
==9458==    by 0x40A3DB5: test_case_run (gtestutils.c:1662)
==9458==    by 0x40A40B2: g_test_run_suite_internal (gtestutils.c:1715)
==9458==    by 0x40A417C: g_test_run_suite_internal (gtestutils.c:1726)
==9458==    by 0x40A42F9: g_test_run_suite (gtestutils.c:1771)
==9458==    by 0x40A3441: g_test_run (gtestutils.c:1319)
==9458==    by 0x80493F1: main (environment.c:108)

Bug #669412.
Comment 2 Colin Walters 2012-02-05 18:50:34 UTC
Review of attachment 206833 [details] [review]:

Hmm...I don't think there's any guarantee here that the provided environment was allocated with g_malloc().  Yes, it's hard to use this function without leaking, but I think we may need to instead add g_environ_unsetenv_copy() which doesn't mutate the input.  Or g_environ_unsetenv_with_free_func() ?

Dan?
Comment 3 Christian Persch 2012-02-05 19:04:11 UTC
Both g_environ_setenv and g_environ_unsetenv are new in 2.32, so I think we can simply amend their documentation to specify this. Note that g_environ_setenv uses g_strdup_printf() for the member it _adds_, so if we were to allow the passed-in envp to contain non-g_free-compatible memory, there would be no way to free the returned envp at all since some members would be g_free'able while others would be foo_free'able only...
Comment 4 Dan Winship 2012-02-05 19:35:27 UTC
Comment on attachment 206833 [details] [review]
Plug a mem leak in g_environ_unsetenv

g_environ_unsetenv() was definitely not supposed to leak. But this patch is wrong because g_unsetenv() currently uses g_environ_unsetenv() in the fallback case where the system doesn't have unsetenv(), and the system environment strings definitely aren't g_free()able. So you need to copy the existing code back to g_unsetenv() before you can add a g_free() to g_environ_unsetenv().
Comment 5 Colin Walters 2012-02-05 19:42:27 UTC
(In reply to comment #3)
> Both g_environ_setenv and g_environ_unsetenv are new in 2.32, so I think we can
> simply amend their documentation to specify this. Note that g_environ_setenv
> uses g_strdup_printf() for the member it _adds_,

Good point.  Ok, I'm fine with the patch as long as the documentation is updated to reflect that each member must have been allocated with g_malloc() - maybe just point to g_strfreev().
Comment 6 Christian Persch 2012-02-05 21:48:46 UTC
Created attachment 206863 [details] [review]
Note that g_environ_setenv() already had the requirement that the passed array was

allocated with glib's allocator since it uses g_renew on it, and also the requirement
that the elements are also allocated using the glib allocator, since it uses
g_free on them when overwriting an existing value.
Comment 7 Colin Walters 2012-02-07 17:30:06 UTC
Review of attachment 206863 [details] [review]:

Looks right.
Comment 8 Christian Persch 2012-02-15 16:46:45 UTC
Pushed to master.