]> Pileus Git - ~andy/gtk/blobdiff - gdk/gdkrgb.c
Fix #99593: Fix a memory leak when XmbLookupString returns XBufferOverflow
[~andy/gtk] / gdk / gdkrgb.c
index 8868583109b7a4a1cc8a21f5f4b5ae1acc98cafd..20f1eef31f1f95f3b41f42a659ae5915f80bb9ad 100644 (file)
@@ -146,7 +146,7 @@ static gboolean gdk_rgb_verbose = FALSE;
 static gint
 gdk_rgb_cmap_fail (const char *msg, GdkColormap *cmap, gulong *pixels)
 {
-  gulong free_pixels[256];
+  GdkColor free_colors[256];
   gint n_free;
   gint i;
 
@@ -156,9 +156,9 @@ gdk_rgb_cmap_fail (const char *msg, GdkColormap *cmap, gulong *pixels)
   n_free = 0;
   for (i = 0; i < 256; i++)
     if (pixels[i] < 256)
-      free_pixels[n_free++] = pixels[i];
+      free_colors[n_free++].pixel = pixels[i];
   if (n_free)
-    gdk_colors_free (cmap, free_pixels, n_free, 0);
+    gdk_colormap_free_colors (cmap, free_colors, n_free);
   return 0;
 }
 
@@ -181,7 +181,7 @@ gdk_rgb_make_colorcube (GdkRgbInfo *image_info, gulong *pixels,
     {
       image_info->colorcube[i] = pixels[rt[i >> 8] + gt[(i >> 4) & 0x0f] + bt[i & 0x0f]];
 #ifdef VERBOSE
-      g_print ("%03x %02x %x %x %x\n", i, image-info->colorcube[i], rt[i >> 8], gt[(i >> 4) & 0x0f], bt[i & 0x0f]);
+      g_print ("%03x %02x %x %x %x\n", i, image_info->colorcube[i], rt[i >> 8], gt[(i >> 4) & 0x0f], bt[i & 0x0f]);
 #endif
     }
 }
@@ -257,22 +257,27 @@ gdk_rgb_try_colormap (GdkRgbInfo *image_info, gboolean force,
        b0 = bi * 255 / (nb - 1);
        idx = ((ri * nr) + gi) * nb + bi;
        d2 = (r - r0) * (r - r0) + (g - g0) * (g - g0) + (b - b0) * (b - b0);
-       if (d2 < best[idx]) {
-         if (pixels[idx] < 256)
-           gdk_colors_free (cmap, pixels + idx, 1, 0);
-         else
-           colors_needed--;
-         color = cmap->colors[i];
-         if (!gdk_colormap_alloc_color (cmap, &color, FALSE, FALSE))
-           return gdk_rgb_cmap_fail ("error allocating system color\n",
-                                     cmap, pixels);
-         pixels[idx] = color.pixel; /* which is almost certainly i */
-         best[idx] = d2;
-       }
+       if (d2 < best[idx])
+         {
+           if (pixels[idx] < 256)
+             {
+               color.pixel = pixels[idx];
+               gdk_colormap_free_colors (cmap, &color, 1);
+             }
+           else
+             colors_needed--;
+           color = cmap->colors[i];
+           if (!gdk_colormap_alloc_color (cmap, &color, FALSE, FALSE))
+             return gdk_rgb_cmap_fail ("error allocating system color\n",
+                                       cmap, pixels);
+           pixels[idx] = color.pixel; /* which is almost certainly i */
+           best[idx] = d2;
+         }
       }
 #endif
 
-  if (colors_needed)
+  if (colors_needed && 
+      image_info->visual->type != GDK_VISUAL_STATIC_COLOR)
     {
       if (!gdk_colors_alloc (cmap, 0, NULL, 0, junk, colors_needed))
        {
@@ -564,7 +569,7 @@ gdk_rgb_free_info (GdkRgbInfo *image_info)
     gdk_rgb_cmap_free (image_info->gray_cmap);
 
   if (image_info->own_gc)
-    gdk_gc_unref (image_info->own_gc);
+    g_object_unref (image_info->own_gc);
 
   if (image_info->colorcube)
     g_free (image_info->colorcube);
@@ -638,11 +643,12 @@ gdk_rgb_create_info (GdkVisual *visual, GdkColormap *colormap)
        image_info->visual->depth >= 3))
     {
       if (!image_info->cmap)
-       image_info->cmap = gdk_colormap_ref (gdk_screen_get_system_colormap (screen));
+       image_info->cmap = g_object_ref (gdk_screen_get_system_colormap (screen));
       
       gdk_rgb_colorcube_222 (image_info);
     }
-  else if (image_info->visual->type == GDK_VISUAL_PSEUDO_COLOR)
+  else if (image_info->visual->type == GDK_VISUAL_PSEUDO_COLOR
+    || image_info->visual->type == GDK_VISUAL_STATIC_COLOR)
     {
       if (!image_info->cmap &&
          (gdk_rgb_install_cmap || image_info->visual != gdk_screen_get_system_visual (screen)))
@@ -663,7 +669,7 @@ gdk_rgb_create_info (GdkVisual *visual, GdkColormap *colormap)
                 image_info->nblue_shades);
 
       if (!image_info->cmap)
-       image_info->cmap = gdk_colormap_ref (gdk_screen_get_system_colormap (screen));
+       image_info->cmap = g_object_ref (gdk_screen_get_system_colormap (screen));
     }
 #ifdef ENABLE_GRAYSCALE
   else if (image_info->visual->type == GDK_VISUAL_GRAYSCALE)
@@ -684,7 +690,7 @@ gdk_rgb_create_info (GdkVisual *visual, GdkColormap *colormap)
          /* Always install colormap in direct color. */
          if (image_info->visual->type != GDK_VISUAL_DIRECT_COLOR &&
              image_info->visual == gdk_screen_get_system_visual (screen))
-           image_info->cmap = gdk_colormap_ref (gdk_screen_get_system_colormap (screen));
+           image_info->cmap = g_object_ref (gdk_screen_get_system_colormap (screen));
          else
            {
              image_info->cmap = gdk_colormap_new (image_info->visual, FALSE);
@@ -797,7 +803,7 @@ gdk_rgb_xpixel_from_rgb (guint32 rgb)
   guint32 g = rgb & 0xff00;
   guint32 b = rgb & 0xff;
 
-  return gdk_rgb_xpixel_from_rgb_internal (gdk_screen_get_rgb_colormap (gdk_get_default_screen ()),
+  return gdk_rgb_xpixel_from_rgb_internal (gdk_screen_get_rgb_colormap (gdk_screen_get_default ()),
                                           (r >> 8) + (r >> 16), g + (g >> 8), b + (b << 8));
 }
 
@@ -2997,7 +3003,9 @@ gdk_rgb_select_conv (GdkRgbInfo *image_info)
     }
   else if (bpp == 8 &&
           depth <= 8 &&
+          depth > 4 &&
           (vtype == GDK_VISUAL_PSEUDO_COLOR
+           || vtype == GDK_VISUAL_STATIC_COLOR
 #ifdef ENABLE_GRAYSCALE
            || vtype == GDK_VISUAL_GRAYSCALE
 #endif
@@ -3104,15 +3112,7 @@ gdk_draw_rgb_image_core (GdkRgbInfo *image_info,
   if (image_info->bitmap)
     {
       if (image_info->own_gc == NULL)
-       {
-         GdkColor color;
-
-         image_info->own_gc = gdk_gc_new (drawable);
-         gdk_color_white (image_info->cmap, &color);
-         gdk_gc_set_foreground (image_info->own_gc, &color);
-         gdk_color_black (image_info->cmap, &color);
-         gdk_gc_set_background (image_info->own_gc, &color);
-       }
+       image_info->own_gc = gdk_gc_new (drawable);
       gc = image_info->own_gc;
     }
   for (y0 = 0; y0 < height; y0 += GDK_SCRATCH_IMAGE_HEIGHT)
@@ -3323,6 +3323,7 @@ gdk_draw_gray_image (GdkDrawable *drawable,
   if (image_info->bpp == 1 &&
       image_info->gray_cmap == NULL &&
       (image_info->visual->type == GDK_VISUAL_PSEUDO_COLOR ||
+       image_info->visual->type == GDK_VISUAL_STATIC_COLOR ||
        image_info->visual->type == GDK_VISUAL_GRAYSCALE))
     gdk_rgb_make_gray_cmap (image_info);
   
@@ -3346,9 +3347,10 @@ gdk_rgb_cmap_get_info (GdkRgbCmap *cmap,
   int i, j;
   guint32 rgb;
 
-  /* We only need a LUT for pseudo-color and grayscale cmaps */
+  /* We don't need a LUT for TrueColor or DirectColor visuals */
   if (image_info->bpp != 1 ||
       !(image_info->visual->type == GDK_VISUAL_PSEUDO_COLOR ||
+       image_info->visual->type == GDK_VISUAL_STATIC_COLOR ||
        image_info->visual->type == GDK_VISUAL_GRAYSCALE))
     return NULL;
   
@@ -3372,7 +3374,7 @@ gdk_rgb_cmap_get_info (GdkRgbCmap *cmap,
        ((rgb & 0xf000) >> 8) |
        ((rgb & 0xf0) >> 4);
 #ifdef VERBOSE
-      g_print ("%d %x %x %d\n", i, j, image_info->colorcube[j]);
+      g_print ("%d %x %x\n", i, j, image_info->colorcube[j]);
 #endif
       cmap_info->lut[i] = image_info->colorcube[j];
     }
@@ -3412,6 +3414,7 @@ gdk_rgb_cmap_free (GdkRgbCmap *cmap)
       GdkRgbCmapInfo *cmap_info = tmp_list->data;
       cmap_info->image_info->cmap_info_list = g_slist_remove (cmap_info->image_info->cmap_info_list, cmap_info);
       g_free (cmap_info);
+      tmp_list = tmp_list->next;
     }
   g_slist_free (cmap->info_list);
   
@@ -3476,7 +3479,7 @@ gdk_rgb_get_colormap (void)
   static GdkColormap *cmap = NULL;
   if (!cmap)
     {
-      GdkRgbInfo *image_info = gdk_rgb_create_info (gdk_rgb_choose_visual (gdk_get_default_screen ()), NULL);
+      GdkRgbInfo *image_info = gdk_rgb_create_info (gdk_rgb_choose_visual (gdk_screen_get_default ()), NULL);
       cmap = image_info->cmap;
     }
 
@@ -3546,5 +3549,5 @@ gdk_screen_get_rgb_visual (GdkScreen *screen)
 GdkVisual *
 gdk_rgb_get_visual (void)
 {
-  return gdk_screen_get_rgb_visual (gdk_get_default_screen ());
+  return gdk_screen_get_rgb_visual (gdk_screen_get_default ());
 }