GNOME Bugzilla – Bug 659923
Add g_variant_new_fixed_array() function
Last modified: 2011-09-25 06:27:57 UTC
Using g_variant_new_from_data() for creating new byte arrays is non-obvious. This patch adds a g_variant_new_fixed_array() function.
Created attachment 197326 [details] [review] gvariant: Add g_variant_get_fixed_array() Using g_variant_new_from_data() for creating new byte arrays is non-obvious. This patch adds a g_variant_new_fixed_array() function.
Review of attachment 197326 [details] [review]: Thanks a lot Stef. This looks almost exactly right. Just some minor details in the implementation... ::: glib/gvariant.c @@ +1148,3 @@ + gpointer data; + + g_return_val_if_fail (g_variant_type_is_definite (element_type), NULL); in addition to checking that the type is definite, you should also check that it is fixed-sized (ie: bools, bytes, int/uint{16,32,64}, doubles and maybe fd array indices). you should also check that the user's given element_size is correct as per the type. g_variant_type_info_query_element could help here, but i'm not completely sure how i feel about using it from gvariant.c (since it's normally only used in gvariant-core.c). @@ +1152,3 @@ + + data = g_memdup (elements, n_elements * element_size); + return g_variant_new_from_data (g_variant_type_new_array (element_type), this leaks the array GVariantType
Created attachment 197386 [details] [review] Thanks for the review. Does this look good to go in? Updated attachment to check that element size matches element_type, and check that element_type represents a type with a fixed size. Used g_variant_type_info_query_element() for this, as its already used elsewhere in gvariant.c in g_variant_get_fixed_array(). Fixed leaks, tested (by hand) that criticals fire when invalid stuff is passed.
Created attachment 197387 [details] [review] Errr, now with fixed indentation. How's that?
Review of attachment 197387 [details] [review]: Feel free to push this with the very minor tweak. Thanks very much (and thanks for writing a test, too). ::: glib/gvariant.c @@ +1165,3 @@ + array_element_size, element_size); + else + g_critical ("g_variant_get_fixed_array: array does not have fixed size."); you should return NULL after the g_criticals()
Great. Merged with that change.