diff options
author | Simon McVittie <simon.mcvittie@collabora.co.uk> | 2011-09-22 18:29:27 +0100 |
---|---|---|
committer | Simon McVittie <simon.mcvittie@collabora.co.uk> | 2013-10-21 12:37:45 +0100 |
commit | 021d157ba43702b05973cbd96fafcf6ec3b96ece (patch) | |
tree | a00f489f5e767032e4fc5d8eeec4f4b058a16dca | |
parent | 8f7fbad772460e1ec9509de481bd572a7a9233d0 (diff) | |
download | dbus-glib-021d157ba43702b05973cbd96fafcf6ec3b96ece.tar.gz |
test-variant-recursion: free our GValues and the output string correctly
Previously, if we'd freed the GValues, it would have crashed, because
g_value_take_boxed on a value of type G_TYPE_VALUE requires that the inner
GValue was g_malloc'd individually, but we were allocating them as a
block. This only "worked" because *none* of them were freed...
The returned string was also leaked; free it too, but only on success.
Bug: https://bugs.freedesktop.org/show_bug.cgi?id=41129
Signed-off-by: Simon McVittie <simon.mcvittie@collabora.co.uk>
-rw-r--r-- | test/core/test-variant-recursion.c | 45 |
1 files changed, 26 insertions, 19 deletions
diff --git a/test/core/test-variant-recursion.c b/test/core/test-variant-recursion.c index 4229c2b..e479f65 100644 --- a/test/core/test-variant-recursion.c +++ b/test/core/test-variant-recursion.c @@ -13,29 +13,36 @@ make_recursive_stringify_call (int recursion_depth, DBusGProxy *proxy, GError **error) { - char *out_str; - + gchar *out_str; + gboolean ret; int i; - GValue *vals = g_new0 (GValue, recursion_depth+1); + GValue *val = g_new0 (GValue, 1); - for (i = recursion_depth-1; i >= 0; i--) - { - GValue *curval = &(vals[i]); - g_value_init (curval, G_TYPE_VALUE); - } - for (i = 0; i < recursion_depth; i++) + g_value_init (val, G_TYPE_STRING); + g_value_set_string (val, "end of the line"); + + for (i = 0; i < recursion_depth; i++) { - GValue *curval = &(vals[i]); - GValue *nextval = &(vals[i+1]); - g_value_take_boxed (curval, nextval); + GValue *tmp = g_new0 (GValue, 1); + + g_value_init (tmp, G_TYPE_VALUE); + g_value_take_boxed (tmp, val); + val = tmp; } - g_value_init (&(vals[recursion_depth]), G_TYPE_STRING); - g_value_set_string (&(vals[recursion_depth]), "end of the line"); - return dbus_g_proxy_call (proxy, "Stringify", error, - G_TYPE_VALUE, &(vals[0]), - G_TYPE_INVALID, - G_TYPE_STRING, &out_str, - G_TYPE_INVALID); + + ret = dbus_g_proxy_call (proxy, "Stringify", error, + G_TYPE_VALUE, val, + G_TYPE_INVALID, + G_TYPE_STRING, &out_str, + G_TYPE_INVALID); + + g_boxed_free (G_TYPE_VALUE, val); + + /* the out parameter is meaningless if it failed */ + if (ret) + g_free (out_str); + + return ret; } int |