[dia] Bug 643285 - more graceful out of memory handling



commit 12bb4f716c2442dcc1939cb6d0921daa187a112b
Author: Hans Breuer <hans breuer org>
Date:   Sun Mar 13 21:30:52 2011 +0100

    Bug 643285 - more graceful out of memory handling
    
    If there was not enough memory to convert the image in the
    renderer Dia was aborting with:
    Message : Glib-ERROR **: gmem.c:136: failed to allocate ...
    
    Now these allocations are done with g_try_malloc() and
    converted into "Not enough memory for image drawing."

 lib/dia_image.c                     |    9 +++++++--
 plug-ins/cairo/diacairo-renderer.c  |   28 +++++++++++++++++++---------
 plug-ins/cgm/cgm.c                  |    4 ++++
 plug-ins/libart/dialibartrenderer.c |    4 ++++
 plug-ins/metapost/render_metapost.c |    4 ++++
 plug-ins/postscript/diapsrenderer.c |    4 ++++
 plug-ins/pstricks/render_pstricks.c |    5 ++++-
 plug-ins/python/pydia-image.c       |   14 +++++++++++---
 plug-ins/wpg/wpg.c                  |    4 ++++
 9 files changed, 61 insertions(+), 15 deletions(-)
---
diff --git a/lib/dia_image.c b/lib/dia_image.c
index 13409eb..a8066f2 100644
--- a/lib/dia_image.c
+++ b/lib/dia_image.c
@@ -376,7 +376,10 @@ dia_image_rgb_data(const DiaImage *image)
   int height = dia_image_height(image);
   int rowstride = dia_image_rowstride(image);
   int size = height*rowstride;
-  guint8 *rgb_pixels = g_malloc(size);
+  guint8 *rgb_pixels = g_try_malloc(size);
+
+  if (!rgb_pixels)
+    return NULL;
 
   if (gdk_pixbuf_get_has_alpha(image->image)) {
     guint8 *pixels = gdk_pixbuf_get_pixels(image->image);
@@ -418,7 +421,9 @@ dia_image_mask_data(const DiaImage *image)
   size = gdk_pixbuf_get_width(image->image)*
     gdk_pixbuf_get_height(image->image);
 
-  mask = g_malloc(size);
+  mask = g_try_malloc(size);
+  if (!mask)
+    return NULL;
 
   /* Pick every fourth byte (the alpha channel) into mask */
   for (i = 0; i < size; i++)
diff --git a/plug-ins/cairo/diacairo-renderer.c b/plug-ins/cairo/diacairo-renderer.c
index 367785d..53b3188 100644
--- a/plug-ins/cairo/diacairo-renderer.c
+++ b/plug-ins/cairo/diacairo-renderer.c
@@ -789,12 +789,16 @@ draw_image(DiaRenderer *self,
   if (dia_image_rgba_data (image))
     {
       const guint8 *p1 = dia_image_rgba_data (image);
-      /* we need to make a copy to rearrange channels 
-       * (also need to use malloc, cause Cairo insists to free() it)
-       */
-      guint8 *p2 = data = g_malloc (h * rs);
+      /* we need to make a copy to rearrange channels ... */
+      guint8 *p2 = data = g_try_malloc (h * rs);
       int i;
 
+      if (!data) 
+        {
+          message_warning (_("Not enough memory for image drawing."));
+	  return;
+        }
+
       for (i = 0; i < (h * rs) / 4; i++)
         {
 #  if G_BYTE_ORDER == G_LITTLE_ENDIAN
@@ -816,14 +820,20 @@ draw_image(DiaRenderer *self,
     }
   else
     {
-      guint8 *p, *p2;
+      guint8 *p = NULL, *p2;
       guint8 *p1 = data = dia_image_rgb_data (image);
-      /* need to copy to be owned by cairo/pixman, urgh.
-       * Also cairo wants RGB24 32 bit aligned
-       */
+      /* cairo wants RGB24 32 bit aligned, so copy ... */
       int x, y;
 
-      p = p2 = g_malloc(h*w*4);
+      if (data)
+	p = p2 = g_try_malloc(h*w*4);
+	
+      if (!p)
+        {
+          message_warning (_("Not enough memory for image drawing."));
+	  return;
+        }
+
       for (y = 0; y < h; y++)
         {
           for (x = 0; x < w; x++)
diff --git a/plug-ins/cgm/cgm.c b/plug-ins/cgm/cgm.c
index 22b9014..e0d2b78 100644
--- a/plug-ins/cgm/cgm.c
+++ b/plug-ins/cgm/cgm.c
@@ -1109,6 +1109,10 @@ draw_image(DiaRenderer *self,
     }
 
     ptr = pImg = dia_image_rgb_data(image);
+    if (!pImg) {
+      message_warning (_("Not enough memory for image drawing."));
+      return;
+    }
 
     while (lines > 0) {
 	chunk = MIN(rowlen * lines, maxlen);
diff --git a/plug-ins/libart/dialibartrenderer.c b/plug-ins/libart/dialibartrenderer.c
index 402240a..f5ea7ab 100644
--- a/plug-ins/libart/dialibartrenderer.c
+++ b/plug-ins/libart/dialibartrenderer.c
@@ -1320,6 +1320,10 @@ draw_image(DiaRenderer *self,
     } else {
       guint8 *img_data = dia_image_rgb_data(image);
 
+      if (!img_data) {
+        message_warning (_("Not enough memory for image drawing."));
+        return;
+      }
       art_rgb_affine(renderer->rgb_buffer,
 		     0, 0,
 		     renderer->pixel_width,
diff --git a/plug-ins/metapost/render_metapost.c b/plug-ins/metapost/render_metapost.c
index 0f2dcf8..d5ac028 100644
--- a/plug-ins/metapost/render_metapost.c
+++ b/plug-ins/metapost/render_metapost.c
@@ -1106,6 +1106,10 @@ draw_image(DiaRenderer *self,
     ystep = height/img_height;
 
     rgb_data = dia_image_rgb_data(image);
+    if (!rgb_data) {
+        message_warning (_("Not enough memory for image drawing."));
+        return;
+    }
     mask_data = dia_image_mask_data(image);
 
     fprintf(renderer->file, "  pickup pensquare scaled %sx scaled %s;\n",
diff --git a/plug-ins/postscript/diapsrenderer.c b/plug-ins/postscript/diapsrenderer.c
index 10b71ed..f6bdb1a 100644
--- a/plug-ins/postscript/diapsrenderer.c
+++ b/plug-ins/postscript/diapsrenderer.c
@@ -715,6 +715,10 @@ draw_image(DiaRenderer *self,
   img_height = dia_image_height(image);
 
   rgb_data = dia_image_rgb_data(image);
+  if (!rgb_data) {
+    message_warning (_("Not enough memory for image drawing."));
+    return;
+  }
   mask_data = dia_image_mask_data(image);
 
   ratio = height/width;
diff --git a/plug-ins/pstricks/render_pstricks.c b/plug-ins/pstricks/render_pstricks.c
index dd601e8..e6d85df 100644
--- a/plug-ins/pstricks/render_pstricks.c
+++ b/plug-ins/pstricks/render_pstricks.c
@@ -822,7 +822,10 @@ draw_image(DiaRenderer *self,
     img_height = dia_image_height(image);
 
     rgb_data = dia_image_rgb_data(image);
-  
+    if (!rgb_data) {
+        message_warning (_("Not enough memory for image drawing."));
+        return;
+    }
     ratio = height/width;
 
     fprintf(renderer->file, "\\pscustom{\\code{gsave\n");
diff --git a/plug-ins/python/pydia-image.c b/plug-ins/python/pydia-image.c
index c4bc805..c60346d 100644
--- a/plug-ins/python/pydia-image.c
+++ b/plug-ins/python/pydia-image.c
@@ -103,14 +103,22 @@ PyDiaImage_GetAttr(PyDiaImage *self, gchar *attr)
   else if (!strcmp(attr, "rgb_data")) {
     unsigned char* s = dia_image_rgb_data(self->image);
     int len = dia_image_width(self->image) * dia_image_height(self->image) * 3;
-    PyObject* py_s = PyString_FromStringAndSize(s, len);
+    PyObject* py_s;
+
+    if (!s)
+      return PyErr_NoMemory();
+    py_s = PyString_FromStringAndSize(s, len);
     g_free (s);
     return py_s;
   }
   else if (!strcmp(attr, "mask_data")) {
-    char* s = dia_image_rgb_data(self->image);
+    char* s = dia_image_mask_data(self->image);
     int len = dia_image_width(self->image) * dia_image_height(self->image);
-    PyObject* py_s = PyString_FromStringAndSize(s, len);
+    PyObject* py_s;
+
+    if (!s)
+      return PyErr_NoMemory();
+    py_s = PyString_FromStringAndSize(s, len);
     g_free (s);
     return py_s;
   }
diff --git a/plug-ins/wpg/wpg.c b/plug-ins/wpg/wpg.c
index a2a3bb4..09ea102 100644
--- a/plug-ins/wpg/wpg.c
+++ b/plug-ins/wpg/wpg.c
@@ -887,6 +887,10 @@ draw_image(DiaRenderer *self,
             width, height, bmp.Width, bmp.Height, point->x, point->y));
 
   pDiaImg = dia_image_rgb_data(image);
+  if (!pDiaImg) {
+    message_warning (_("Not enough memory for image drawing."));
+    return;
+  }
   stride = dia_image_rowstride(image);
   pOut = g_new(guint8, bmp.Width * bmp.Height * 2); /* space for worst case RLE */
   p = pOut;



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