diff options
| author | Po Lu | 2022-03-08 09:14:41 +0800 |
|---|---|---|
| committer | Po Lu | 2022-03-08 09:40:21 +0800 |
| commit | bacd7ae4b6dbe3b8c8d5bcf100f97b4e856aac39 (patch) | |
| tree | dcea68483ffb93677935647505d90394d898e626 | |
| parent | b19db4861f552a2571a97dab590f6fdf59c5c81b (diff) | |
| download | emacs-bacd7ae4b6dbe3b8c8d5bcf100f97b4e856aac39.tar.gz emacs-bacd7ae4b6dbe3b8c8d5bcf100f97b4e856aac39.zip | |
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.
| -rw-r--r-- | src/xterm.c | 35 |
1 files changed, 30 insertions, 5 deletions
diff --git a/src/xterm.c b/src/xterm.c index e20f9ca406d..52df2e9f046 100644 --- a/src/xterm.c +++ b/src/xterm.c | |||
| @@ -3858,10 +3858,12 @@ x_alloc_nearest_color_1 (Display *dpy, Colormap cmap, XColor *color) | |||
| 3858 | Status status; | 3858 | Status status; |
| 3859 | bool retry = false; | 3859 | bool retry = false; |
| 3860 | int ncolor_cells, i; | 3860 | int ncolor_cells, i; |
| 3861 | bool temp_allocated; | ||
| 3862 | XColor temp; | ||
| 3861 | 3863 | ||
| 3862 | start: | 3864 | start: |
| 3863 | |||
| 3864 | cells = x_color_cells (dpy, &no_cells); | 3865 | cells = x_color_cells (dpy, &no_cells); |
| 3866 | temp_allocated = false; | ||
| 3865 | 3867 | ||
| 3866 | nearest = 0; | 3868 | nearest = 0; |
| 3867 | /* I'm assuming CSE so I'm not going to condense this. */ | 3869 | /* 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) | |||
| 3881 | * ((color->blue >> 8) - (cells[x].blue >> 8)))); | 3883 | * ((color->blue >> 8) - (cells[x].blue >> 8)))); |
| 3882 | if (trial_delta < nearest_delta) | 3884 | if (trial_delta < nearest_delta) |
| 3883 | { | 3885 | { |
| 3884 | nearest = x; | 3886 | /* We didn't decide to use this color, so free it. */ |
| 3885 | nearest_delta = trial_delta; | 3887 | if (temp_allocated) |
| 3888 | { | ||
| 3889 | XFreeColors (dpy, cmap, &temp.pixel, 1, 0); | ||
| 3890 | temp_allocated = false; | ||
| 3891 | } | ||
| 3892 | |||
| 3893 | temp.red = cells[x].red; | ||
| 3894 | temp.green = cells[x].green; | ||
| 3895 | temp.blue = cells[x].blue; | ||
| 3896 | status = XAllocColor (dpy, cmap, &temp); | ||
| 3897 | |||
| 3898 | if (status) | ||
| 3899 | { | ||
| 3900 | temp_allocated = true; | ||
| 3901 | nearest = x; | ||
| 3902 | nearest_delta = trial_delta; | ||
| 3903 | } | ||
| 3886 | } | 3904 | } |
| 3887 | } | 3905 | } |
| 3888 | color->red = cells[nearest].red; | 3906 | color->red = cells[nearest].red; |
| 3889 | color->green = cells[nearest].green; | 3907 | color->green = cells[nearest].green; |
| 3890 | color->blue = cells[nearest].blue; | 3908 | color->blue = cells[nearest].blue; |
| 3891 | status = XAllocColor (dpy, cmap, color); | ||
| 3892 | 3909 | ||
| 3893 | if (status != 0 && !retry) | 3910 | if (!temp_allocated) |
| 3911 | status = XAllocColor (dpy, cmap, color); | ||
| 3912 | else | ||
| 3913 | { | ||
| 3914 | *color = temp; | ||
| 3915 | status = 1; | ||
| 3916 | } | ||
| 3917 | |||
| 3918 | if (status == 0 && !retry) | ||
| 3894 | { | 3919 | { |
| 3895 | /* Our private cache of color cells is probably out of date. | 3920 | /* Our private cache of color cells is probably out of date. |
| 3896 | Refresh it here, and try to allocate the nearest color | 3921 | Refresh it here, and try to allocate the nearest color |