From b0a398bc4f75bf86e4e4df171df79b52e9fd3503 Mon Sep 17 00:00:00 2001 From: Alban Crequy Date: Wed, 7 Dec 2011 16:50:02 +0000 Subject: dataforms: fix crash in add_field_to_node_using_default() field->raw_value_contents was not initialized when the field was not coming from a WockyNode. It leads to a segfault in add_field_to_node_using_default(). The following code was triggering the crash: form = g_object_new (WOCKY_TYPE_DATA_FORM, NULL); wocky_data_form_set_string (form, "field", "value", TRUE); wocky_data_form_add_to_node (form, ...); This patch initializes field->raw_value_contents when the WockyNode is created through data_form_set_value(). The unit tests are updated to check this. https://bugs.freedesktop.org/show_bug.cgi?id=43584 --- wocky/wocky-data-form.c | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/wocky/wocky-data-form.c b/wocky/wocky-data-form.c index 16b7100..6856b52 100644 --- a/wocky/wocky-data-form.c +++ b/wocky/wocky-data-form.c @@ -687,6 +687,7 @@ data_form_set_value (WockyDataForm *self, gboolean create_if_missing) { WockyDataFormField *field; + GType t; g_return_val_if_fail (field_name != NULL, FALSE); g_return_val_if_fail (value != NULL, FALSE); @@ -714,6 +715,35 @@ data_form_set_value (WockyDataForm *self, wocky_g_value_slice_free (field->value); field->value = value; + + g_strfreev (field->raw_value_contents); + + t = G_VALUE_TYPE (field->value); + if (t == G_TYPE_STRING) + { + const gchar const *value_str[] = + { g_value_get_string (field->value), NULL }; + + field->raw_value_contents = g_strdupv ((GStrv) value_str); + } + else if (t == G_TYPE_BOOLEAN) + { + const gchar const *value_str[] = + { g_value_get_boolean (field->value) ? "1" : "0", NULL }; + + field->raw_value_contents = g_strdupv ((GStrv) value_str); + } + else if (t == G_TYPE_STRV) + { + const GStrv value_str = g_value_get_boxed (field->value); + + field->raw_value_contents = g_strdupv (value_str); + } + else + { + g_assert_not_reached (); + } + return TRUE; } @@ -1034,6 +1064,7 @@ add_field_to_node_using_default (WockyDataFormField *field, wocky_node_set_attribute (field_node, "type", type_to_str (field->type)); + g_assert (field->raw_value_contents != NULL); for (s = field->raw_value_contents; *s != NULL; s++) wocky_node_add_child_with_content (field_node, "value", *s); } -- cgit v1.2.3