[glib] array: return_if_fail() if element size is 0



commit a6e149e41f359c4bf2ddab341dad20e7fb2bd9f3
Author: Benjamin Otte <otte redhat com>
Date:   Sat Jan 14 01:13:42 2012 +0100

    array: return_if_fail() if element size is 0
    
    This is particular useful for:
      g_array_new (sizeof (MyStruct), FALSE, FALSE);
    because the correct incantation is
      g_array_new (FALSE, FALSE, sizeof (MyStruct));
    and these warnings will trigger in the first situation.

 glib/garray.c |   10 ++++++++--
 1 files changed, 8 insertions(+), 2 deletions(-)
---
diff --git a/glib/garray.c b/glib/garray.c
index 14aef84..5a5f9fa 100644
--- a/glib/garray.c
+++ b/glib/garray.c
@@ -162,7 +162,9 @@ g_array_new (gboolean zero_terminated,
 	     gboolean clear,
 	     guint    elt_size)
 {
-  return (GArray*) g_array_sized_new (zero_terminated, clear, elt_size, 0);
+  g_return_val_if_fail (elt_size > 0, NULL);
+
+  return g_array_sized_new (zero_terminated, clear, elt_size, 0);
 }
 
 /**
@@ -185,7 +187,11 @@ GArray* g_array_sized_new (gboolean zero_terminated,
 			   guint    elt_size,
 			   guint    reserved_size)
 {
-  GRealArray *array = g_slice_new (GRealArray);
+  GRealArray *array;
+  
+  g_return_val_if_fail (elt_size > 0, NULL);
+
+  array = g_slice_new (GRealArray);
 
   array->data            = NULL;
   array->len             = 0;



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]