From cdaadaea113ee6ae9d9dcd90b66892e65c35b6ac Mon Sep 17 00:00:00 2001
From: Martin Nilsson <mani@lysator.liu.se>
Date: Sat, 6 Jan 2007 07:06:36 +0100
Subject: [PATCH] Fixed some malloc issues.

Rev: src/post_modules/GTK2/source/gdkcolormap.pre:1.3
Rev: src/post_modules/GTK2/source/gdkdisplay.pre:1.10
Rev: src/post_modules/GTK2/source/gdkscreen.pre:1.7
Rev: src/post_modules/GTK2/source/gobject.pre:1.13
---
 src/post_modules/GTK2/source/gdkcolormap.pre | 19 ++++++++++++++-----
 src/post_modules/GTK2/source/gdkdisplay.pre  |  2 +-
 src/post_modules/GTK2/source/gdkscreen.pre   |  2 +-
 src/post_modules/GTK2/source/gobject.pre     |  4 +++-
 4 files changed, 19 insertions(+), 8 deletions(-)

diff --git a/src/post_modules/GTK2/source/gdkcolormap.pre b/src/post_modules/GTK2/source/gdkcolormap.pre
index 15386b3f85..935e493f7f 100644
--- a/src/post_modules/GTK2/source/gdkcolormap.pre
+++ b/src/post_modules/GTK2/source/gdkcolormap.pre
@@ -37,8 +37,17 @@ int alloc_colors(array(GDK2.Color) colors, int writeable,
     get_all_args("alloc_colors",args,"%A%i%i",&a,&wr,&best);
     if (a==NULL)
       Pike_error("Invalid array.\n");
-    gc=(GdkColor *)xmalloc(sizeof(GdkColor)*a->size);
-    suc=(int *)xmalloc(sizeof(int)*a->size);
+
+    gc=(GdkColor *)malloc(sizeof(GdkColor)*a->size);
+    suc=(int *)malloc(sizeof(int)*a->size);
+    if(!gc || !suc)
+    {
+      if(gc) free(gc);
+      SIMPLE_OUT_OF_MEMORY_ERROR("alloc_colors",
+                                 sizeof(GdkColor)*a->size +
+                                 sizeof(int)*a->size);
+    }
+
     for (i=0; i<a->size; i++) {
       struct object *o;
       o=ITEM(a)[i].u.object;
@@ -82,7 +91,7 @@ void free_colors(array(GDK2.Color) colors)
     get_all_args("free_colors",args,"%A",&a);
     if (a==NULL)
       Pike_error("Invalid array.\n");
-    gc=(GdkColor *)xmalloc(sizeof(GdkColor)*a->size);
+    gc=(GdkColor *)xalloc(sizeof(GdkColor)*a->size);
     for (i=0; i<a->size; i++) {
       struct object *o;
       o=ITEM(a)[i].u.object;
@@ -106,9 +115,9 @@ void free_colors(array(GDK2.Color) colors)
     GdkColor *gc;
 
     get_all_args("query_color",args,"%i",&pix);
-    pgtk2_pop_n_elems(args);
-    gc=(GdkColor *)xmalloc(sizeof(GdkColor));
+    gc=(GdkColor *)xalloc(sizeof(GdkColor));
     gdk_colormap_query_color(GDK_COLORMAP(THIS->obj),pix,gc);
+    pgtk2_pop_n_elems(args);
     push_gdkobject(gc,color);
   }
 }
diff --git a/src/post_modules/GTK2/source/gdkdisplay.pre b/src/post_modules/GTK2/source/gdkdisplay.pre
index 6ae183e688..c984f74ec7 100644
--- a/src/post_modules/GTK2/source/gdkdisplay.pre
+++ b/src/post_modules/GTK2/source/gdkdisplay.pre
@@ -257,7 +257,7 @@ void store_clipboard(GDK2.Window clipboard_window,
       GdkAtom *targets;
       int n;
 
-      targets=xmalloc(a->size);
+      targets=xalloc(a->size);
       for (n=0; n<a->size; n++)
 	targets[n]=get_gdkatom(ITEM(a)[n].u.object);
       gdk_display_store_clipboard(GDK_DISPLAY_OBJECT(THIS->obj),
diff --git a/src/post_modules/GTK2/source/gdkscreen.pre b/src/post_modules/GTK2/source/gdkscreen.pre
index cd29addc34..bdb41d229b 100644
--- a/src/post_modules/GTK2/source/gdkscreen.pre
+++ b/src/post_modules/GTK2/source/gdkscreen.pre
@@ -231,7 +231,7 @@ int get_n_monitors();
     GdkRectangle *gr;
 
     get_all_args("get_monitor_geometry",args,"%i",&a);
-    gr=(GdkRectangle *)xmalloc(sizeof(GdkRectangle));
+    gr=(GdkRectangle *)xalloc(sizeof(GdkRectangle));
     gdk_screen_get_monitor_geometry(GDK_SCREEN(THIS->obj),a,gr);
     pgtk2_pop_n_elems(args);
     push_gdkobject(gr,rectangle);
diff --git a/src/post_modules/GTK2/source/gobject.pre b/src/post_modules/GTK2/source/gobject.pre
index 83b3c52148..8f04f46a97 100644
--- a/src/post_modules/GTK2/source/gobject.pre
+++ b/src/post_modules/GTK2/source/gobject.pre
@@ -221,11 +221,13 @@ int new_signal(string name, array types, string return_type)
       } else {
 	return_type=G_TYPE_POINTER;
       }
-      param_types=xmalloc(sizeof(GType)*a->size);
+
+      param_types=malloc(sizeof(GType)*a->size);
       if (param_types==NULL) {
 	g_free(name);
 	SIMPLE_OUT_OF_MEMORY_ERROR("new_signal",sizeof(GType)*a->size);
       }
+
       for (i=n_params=0; i<a->size; i++) {
 	if (ITEM(a)[i].type!=PIKE_T_STRING)
 	  continue;
-- 
GitLab