[gimp] Bug 704118 - crash on invalid number of PLTE entries



commit 37372555e5820f7f0c3a98ce9975ef66c2d8422c
Author: Michael Natterer <mitch gimp org>
Date:   Sun Jul 14 22:25:44 2013 +0200

    Bug 704118 - crash on invalid number of PLTE entries
    
    Make sure an indexed image always has a colormap. This was the case
    before, except one could set a NULL colormap via the PDB.
    
    Add gimp_image_unset_colormap(), and make gimp_image_set_colormap()
    never set the colormap to NULL, even if NULL is passed. Change the
    only places where actual unsetting makes sense to use unset().
    
    Make some GUI places deal gracefully with palettes/colormaps with zero
    entries.

 app/actions/colormap-actions.c    |    2 +-
 app/core/gimpimage-colormap.c     |   36 ++++++++++++++++++++++++++++--------
 app/core/gimpimage-colormap.h     |    2 ++
 app/core/gimpimage-convert-type.c |    2 +-
 app/core/gimpimage.c              |    2 +-
 app/core/gimpimageundo.c          |    9 ++++++---
 app/widgets/gimppaletteview.c     |    2 +-
 7 files changed, 40 insertions(+), 15 deletions(-)
---
diff --git a/app/actions/colormap-actions.c b/app/actions/colormap-actions.c
index 03944e0..cec7881 100644
--- a/app/actions/colormap-actions.c
+++ b/app/actions/colormap-actions.c
@@ -109,7 +109,7 @@ colormap_actions_update (GimpActionGroup *group,
         gimp_action_group_set_action_color (group, action, color, FALSE);
 
   SET_SENSITIVE ("colormap-edit-color",
-                 image && indexed);
+                 image && indexed && num_colors > 0);
   SET_SENSITIVE ("colormap-add-color-from-fg",
                  image && indexed && num_colors < 256);
   SET_SENSITIVE ("colormap-add-color-from-bg",
diff --git a/app/core/gimpimage-colormap.c b/app/core/gimpimage-colormap.c
index 3d7bfdf..d7af0d7 100644
--- a/app/core/gimpimage-colormap.c
+++ b/app/core/gimpimage-colormap.c
@@ -41,8 +41,8 @@
 
 /*  local function prototype  */
 
-void   gimp_image_colormap_set_palette_entry (GimpImage *image,
-                                              gint       index);
+static void   gimp_image_colormap_set_palette_entry (GimpImage *image,
+                                                     gint       index);
 
 
 /*  public functions  */
@@ -207,11 +207,6 @@ gimp_image_set_colormap (GimpImage    *image,
 
       memcpy (private->colormap, colormap, n_colors * 3);
     }
-  else if (private->colormap)
-    {
-      gimp_image_colormap_dispose (image);
-      gimp_image_colormap_free (image);
-    }
 
   private->n_colors = n_colors;
 
@@ -235,6 +230,31 @@ gimp_image_set_colormap (GimpImage    *image,
 }
 
 void
+gimp_image_unset_colormap (GimpImage *image,
+                           gboolean   push_undo)
+{
+  GimpImagePrivate *private;
+
+  g_return_if_fail (GIMP_IS_IMAGE (image));
+
+  private = GIMP_IMAGE_GET_PRIVATE (image);
+
+  if (push_undo)
+    gimp_image_undo_push_image_colormap (image,
+                                         C_("undo-type", "Unset Colormap"));
+
+  if (private->colormap)
+    {
+      gimp_image_colormap_dispose (image);
+      gimp_image_colormap_free (image);
+    }
+
+  private->n_colors = 0;
+
+  gimp_image_colormap_changed (image, -1);
+}
+
+void
 gimp_image_get_colormap_entry (GimpImage *image,
                                gint       color_index,
                                GimpRGB   *color)
@@ -320,7 +340,7 @@ gimp_image_add_colormap_entry (GimpImage     *image,
 
 /*  private functions  */
 
-void
+static void
 gimp_image_colormap_set_palette_entry (GimpImage *image,
                                        gint       index)
 {
diff --git a/app/core/gimpimage-colormap.h b/app/core/gimpimage-colormap.h
index b71e28b..e4c1cad 100644
--- a/app/core/gimpimage-colormap.h
+++ b/app/core/gimpimage-colormap.h
@@ -37,6 +37,8 @@ void           gimp_image_set_colormap             (GimpImage       *image,
                                                     const guchar    *colormap,
                                                     gint             n_colors,
                                                     gboolean         push_undo);
+void           gimp_image_unset_colormap           (GimpImage       *image,
+                                                    gboolean         push_undo);
 
 void           gimp_image_get_colormap_entry       (GimpImage       *image,
                                                     gint             color_index,
diff --git a/app/core/gimpimage-convert-type.c b/app/core/gimpimage-convert-type.c
index 1726820..2cb18e5 100644
--- a/app/core/gimpimage-convert-type.c
+++ b/app/core/gimpimage-convert-type.c
@@ -1030,7 +1030,7 @@ gimp_image_convert_type (GimpImage               *image,
     case GIMP_RGB:
     case GIMP_GRAY:
       if (old_type == GIMP_INDEXED)
-        gimp_image_set_colormap (image, NULL, 0, TRUE);
+        gimp_image_unset_colormap (image, TRUE);
       break;
 
     case GIMP_INDEXED:
diff --git a/app/core/gimpimage.c b/app/core/gimpimage.c
index 74975ab..112f3c9 100644
--- a/app/core/gimpimage.c
+++ b/app/core/gimpimage.c
@@ -1171,7 +1171,7 @@ gimp_image_real_colormap_changed (GimpImage *image,
 {
   GimpImagePrivate *private = GIMP_IMAGE_GET_PRIVATE (image);
 
-  if (private->colormap)
+  if (private->colormap && private->n_colors > 0)
     {
       babl_palette_set_palette (private->babl_palette_rgb,
                                 gimp_babl_format (GIMP_RGB,
diff --git a/app/core/gimpimageundo.c b/app/core/gimpimageundo.c
index b570822..a6f0877 100644
--- a/app/core/gimpimageundo.c
+++ b/app/core/gimpimageundo.c
@@ -432,9 +432,12 @@ gimp_image_undo_pop (GimpUndo            *undo,
         colormap   = g_memdup (gimp_image_get_colormap (image),
                                GIMP_IMAGE_COLORMAP_SIZE);
 
-        gimp_image_set_colormap (image,
-                                 image_undo->colormap, image_undo->num_colors,
-                                 FALSE);
+        if (image_undo->colormap)
+          gimp_image_set_colormap (image,
+                                   image_undo->colormap, image_undo->num_colors,
+                                   FALSE);
+        else
+          gimp_image_unset_colormap (image, FALSE);
 
         if (image_undo->colormap)
           g_free (image_undo->colormap);
diff --git a/app/widgets/gimppaletteview.c b/app/widgets/gimppaletteview.c
index 4b6128c..8b8fad2 100644
--- a/app/widgets/gimppaletteview.c
+++ b/app/widgets/gimppaletteview.c
@@ -434,7 +434,7 @@ gimp_palette_view_find_entry (GimpPaletteView *view,
   palette  = GIMP_PALETTE (GIMP_VIEW (view)->renderer->viewable);
   renderer = GIMP_VIEW_RENDERER_PALETTE (GIMP_VIEW (view)->renderer);
 
-  if (! palette)
+  if (! palette || ! gimp_palette_get_n_colors (palette))
     return NULL;
 
   col = x / renderer->cell_width;


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