Dict in dict bug

Richard Hult richard@imendio.com
Sun, 30 Nov 2003 16:58:22 +0100


--=-637lgiX7YncIj0Pb8DMo
Content-Type: text/plain
Content-Transfer-Encoding: 7bit

Hi,

Currently, you can't put a dict inside a dict, since
iter->wrote_dict_key isn't reset like when appending other types to a
dict. The attached patch fixes that, and adds a test case for the bug.

Does it look OK?

/Richard

--=-637lgiX7YncIj0Pb8DMo
Content-Disposition: attachment; filename=dict-in-dict.patch
Content-Type: text/x-patch; name=dict-in-dict.patch; charset=iso-8859-15
Content-Transfer-Encoding: 7bit

Index: ChangeLog
===================================================================
RCS file: /cvs/dbus/dbus/ChangeLog,v
retrieving revision 1.443
diff -u -b -B -p -r1.443 ChangeLog
--- ChangeLog	27 Nov 2003 01:25:49 -0000	1.443
+++ ChangeLog	30 Nov 2003 15:57:42 -0000
@@ -1,3 +1,13 @@
+2003-11-30  Richard Hult  <richard@imendio.com>
+
+	* dbus/dbus-message.c (dbus_message_iter_append_dict): Set
+	wrote_dict_key to FALSE on the iter that the dict is appended to,
+	just like when appending other types. Fixes a bug where a dict
+	couldn't be put inside a dict.
+	(dbus_message_iter_append_dict_key): Fix typo in warning message.
+	(message_iter_test, _dbus_message_test): Add test case for dict
+	inside dict.
+
 2003-11-26  Mikael Hallendal  <micke@imendio.com>
 
 	* bus/*.[ch]:
Index: dbus/dbus-message.c
===================================================================
RCS file: /cvs/dbus/dbus/dbus/dbus-message.c,v
retrieving revision 1.115
diff -u -b -B -p -r1.115 dbus-message.c
--- dbus/dbus-message.c	27 Nov 2003 01:25:49 -0000	1.115
+++ dbus/dbus-message.c	30 Nov 2003 15:57:42 -0000
@@ -3874,7 +3874,7 @@ dbus_message_iter_append_dict_key (DBusM
   
   if (real->wrote_dict_key)
     {
-      _dbus_warn ("Appendinging multiple dict key names\n");
+      _dbus_warn ("Appending multiple dict key names\n");
       return FALSE;
     }
   
@@ -4082,6 +4082,8 @@ dbus_message_iter_append_dict (DBusMessa
 
   dbus_message_iter_append_done (dict_real);
   
+  real->wrote_dict_key = FALSE;
+  
   return TRUE;
 }
 
@@ -5800,7 +5802,7 @@ dbus_message_type_from_string (const cha
 static void
 message_iter_test (DBusMessage *message)
 {
-  DBusMessageIter iter, dict, array, array2;
+  DBusMessageIter iter, dict, dict2, array, array2;
   char *str;
   unsigned char *data;
   dbus_int32_t *our_int_array;
@@ -5899,6 +5900,32 @@ message_iter_test (DBusMessage *message)
   if (dbus_message_iter_get_uint32 (&dict) != 0xDEADBEEF)
     _dbus_assert_not_reached ("wrong dict entry value");
 
+  /* dict (in dict) */
+
+  if (!dbus_message_iter_next (&dict))
+    _dbus_assert_not_reached ("reached end of dict");
+  
+  if (dbus_message_iter_get_arg_type (&dict) != DBUS_TYPE_DICT)
+    _dbus_assert_not_reached ("not dict type");
+    
+  dbus_message_iter_init_dict_iterator (&dict, &dict2);
+  
+  str = dbus_message_iter_get_dict_key (&dict2);
+  if (str == NULL || strcmp (str, "dictkey") != 0)
+    _dbus_assert_not_reached ("wrong dict key");
+  dbus_free (str);
+  
+  if (dbus_message_iter_get_arg_type (&dict2) != DBUS_TYPE_STRING)
+    _dbus_assert_not_reached ("wrong dict entry type");
+  
+  str = dbus_message_iter_get_string (&dict2);
+  if (str == NULL || strcmp (str, "dictvalue") != 0)
+    _dbus_assert_not_reached ("wrong dict entry value");
+  dbus_free (str);
+  
+  if (dbus_message_iter_next (&dict2))
+    _dbus_assert_not_reached ("didn't reach end of dict");
+
   if (!dbus_message_iter_next (&dict))
     _dbus_assert_not_reached ("reached end of dict");
   
@@ -7107,6 +7135,13 @@ _dbus_message_test (const char *test_dat
   dbus_message_iter_append_dict_key (&child_iter, "test");
   dbus_message_iter_append_uint32 (&child_iter, 0xDEADBEEF);
 
+  /* dict (in dict) */
+  dbus_message_iter_append_dict_key (&child_iter, "testdict");
+  dbus_message_iter_append_dict (&child_iter, &child_iter2);
+
+  dbus_message_iter_append_dict_key (&child_iter2, "dictkey");
+  dbus_message_iter_append_string (&child_iter2, "dictvalue");
+
   /* array of array of int32  (in dict) */
   dbus_message_iter_append_dict_key (&child_iter, "array");
   dbus_message_iter_append_array (&child_iter, &child_iter2, DBUS_TYPE_ARRAY);

--=-637lgiX7YncIj0Pb8DMo--