emacs-diffs
[Top][All Lists]
Advanced

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

master bacd7ae4b6: Avoid color leaks while better ensuring a close color


From: Po Lu
Subject: master bacd7ae4b6: Avoid color leaks while better ensuring a close color is found
Date: Mon, 7 Mar 2022 20:44:45 -0500 (EST)

branch: master
commit bacd7ae4b6dbe3b8c8d5bcf100f97b4e856aac39
Author: Po Lu <luangruo@yahoo.com>
Commit: Po Lu <luangruo@yahoo.com>

    Avoid color leaks while better ensuring a close color is found
    
    * src/xterm.c (x_alloc_nearest_color_1): Verify nearest can be
    allocated, and use that color value.
---
 src/xterm.c | 35 ++++++++++++++++++++++++++++++-----
 1 file changed, 30 insertions(+), 5 deletions(-)

diff --git a/src/xterm.c b/src/xterm.c
index e20f9ca406..52df2e9f04 100644
--- a/src/xterm.c
+++ b/src/xterm.c
@@ -3858,10 +3858,12 @@ x_alloc_nearest_color_1 (Display *dpy, Colormap cmap, 
XColor *color)
       Status status;
       bool retry = false;
       int ncolor_cells, i;
+      bool temp_allocated;
+      XColor temp;
 
     start:
-
       cells = x_color_cells (dpy, &no_cells);
+      temp_allocated = false;
 
       nearest = 0;
       /* I'm assuming CSE so I'm not going to condense this. */
@@ -3881,16 +3883,39 @@ x_alloc_nearest_color_1 (Display *dpy, Colormap cmap, 
XColor *color)
                            * ((color->blue >> 8) - (cells[x].blue >> 8))));
          if (trial_delta < nearest_delta)
            {
-             nearest = x;
-             nearest_delta = trial_delta;
+             /* We didn't decide to use this color, so free it.  */
+             if (temp_allocated)
+               {
+                 XFreeColors (dpy, cmap, &temp.pixel, 1, 0);
+                 temp_allocated = false;
+               }
+
+             temp.red = cells[x].red;
+             temp.green = cells[x].green;
+             temp.blue = cells[x].blue;
+             status = XAllocColor (dpy, cmap, &temp);
+
+             if (status)
+               {
+                 temp_allocated = true;
+                 nearest = x;
+                 nearest_delta = trial_delta;
+               }
            }
        }
       color->red = cells[nearest].red;
       color->green = cells[nearest].green;
       color->blue = cells[nearest].blue;
-      status = XAllocColor (dpy, cmap, color);
 
-      if (status != 0 && !retry)
+      if (!temp_allocated)
+       status = XAllocColor (dpy, cmap, color);
+      else
+       {
+         *color = temp;
+         status = 1;
+       }
+
+      if (status == 0 && !retry)
        {
          /* Our private cache of color cells is probably out of date.
             Refresh it here, and try to allocate the nearest color



reply via email to

[Prev in Thread] Current Thread [Next in Thread]