Tk Source Code

Check-in [46289902]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:[Bug 2809525] Abort on overlong color name
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | core-8-4-branch
Files: files | file ages | folders
SHA1: 4628990279a2d8a3dba863e4d078d7e7c14b4a7e
User & Date: jan.nijtmans 2012-03-21 22:17:23
Context
2012-03-26
12:32
fix scoping of buf (introduced with fix for [Bug 2809525] check-in: bde05b36 user: jan.nijtmans tags: core-8-4-branch
2012-03-21
22:22
[Bug 2809525] Abort on overlong color name check-in: 89ebf1b8 user: jan.nijtmans tags: core-8-5-branch
22:17
[Bug 2809525] Abort on overlong color name check-in: 46289902 user: jan.nijtmans tags: core-8-4-branch
2012-03-18
21:00
rfe-3503317: XParseColor speedup check-in: 26bdfc04 user: jan.nijtmans tags: core-8-4-branch
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to ChangeLog.






1
2
3
4
5
6
7





2012-03-18  Jan Nijtmans  <[email protected]>

	* xlib/xcolors.c:   [RFE 3503317]: XParseColor speedup
	* xlib/rgb.txt:     List of all colors accepted by Tk in Xorg format
	* tests/color.test: Added test case for all colors in rgb.txt

2012-03-04  Jan Nijtmans  <[email protected]>
>
>
>
>
>







1
2
3
4
5
6
7
8
9
10
11
12
2012-03-21  Jan Nijtmans  <[email protected]>

	* generic/tkColor.c:  [Bug 2809525] Abort on overlong color name.
	* unix/tkUnixColor.c:

2012-03-18  Jan Nijtmans  <[email protected]>

	* xlib/xcolors.c:   [RFE 3503317]: XParseColor speedup
	* xlib/rgb.txt:     List of all colors accepted by Tk in Xorg format
	* tests/color.test: Added test case for all colors in rgb.txt

2012-03-04  Jan Nijtmans  <[email protected]>

Changes to generic/tkColor.c.

810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837









838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861



862
863
864




865

866
867
868
    return resultPtr;
}

#ifndef __WIN32__
/* This function is not necessary for Win32,
 * since XParseColor already does the right thing */
Status
TkParseColor(display, map, spec, colorPtr)
    Display * display;		/* The display */
    Colormap map;			/* Color map */
    CONST char* spec;     /* String to be parsed */
    XColor * colorPtr;
{
    if (*spec == '#') {
    char buf[14];
    buf[0] = '#'; buf[13] = '\0';
	if (!*(++spec) || !*(++spec) || !*(++spec)) {
	/* Not at least 3 hex digits, so invalid */
	return 0;
	} else if (!*(++spec)) {
	/* Exactly 3 hex digits */
	buf[9] = buf[10] = buf[11] = buf[12] = *(--spec);
	buf[5] = buf[6] = buf[7] = buf[8] = *(--spec);
	buf[1] = buf[2] = buf[3] = buf[4] = *(--spec);
	spec = buf;
	} else if (!*(++spec)	|| !*(++spec)) {
	/* Not at least 6 hex digits, so invalid */
	return 0;









	} else if (!*(++spec)) {
	/* Exactly 6 hex digits */
	buf[10] = buf[12] = *(--spec);
	buf[9] = buf[11] = *(--spec);
	buf[6] = buf[8] = *(--spec);
	buf[5] = buf[7] = *(--spec);
	buf[2] = buf[4] = *(--spec);
	buf[1] = buf[3] = *(--spec);
	spec = buf;
	} else if (!*(++spec) || !*(++spec)) {
	/* Not at least 9 hex digits, so invalid */
	return 0;
	} else if (!*(++spec)) {
	/* Exactly 9 hex digits */
	buf[11] = *(--spec);
	buf[10] = *(--spec);
	buf[9] = buf[12] = *(--spec);
	buf[7] = *(--spec);
	buf[6] = *(--spec);
	buf[5] = buf[8] = *(--spec);
	buf[3] = *(--spec);
	buf[2] = *(--spec);
	buf[1] = buf[4] = *(--spec);
	spec = buf;



	} else {
	    spec -= 10;
	}




    }

    return XParseColor(display, map, spec, colorPtr);
}
#endif /* __WIN32__ */







|


|
|

|
|
|
|
|

|
|
|
|
|
|
|
|
|
>
>
>
>
>
>
>
>
>
|
<
<
<
<
<
<
<
<
<
|
|
|
|
|
|
|
|
|
|
|
|
|
|
>
>
>

|

>
>
>
>
|
>
|


810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847









848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
    return resultPtr;
}

#ifndef __WIN32__
/* This function is not necessary for Win32,
 * since XParseColor already does the right thing */
Status
TkParseColor(display, map, name, color)
    Display * display;		/* The display */
    Colormap map;			/* Color map */
    CONST char* name;     /* String to be parsed */
    XColor * color;
{
    if (*name == '#') {
	char buf[14];
	buf[0] = '#'; buf[13] = '\0';
	if (!*(++name) || !*(++name) || !*(++name)) {
	    /* Not at least 3 hex digits, so invalid */
	return 0;
	} else if (!*(++name)) {
	    /* Exactly 3 hex digits */
	    buf[9] = buf[10] = buf[11] = buf[12] = *(--name);
	    buf[5] = buf[6] = buf[7] = buf[8] = *(--name);
	    buf[1] = buf[2] = buf[3] = buf[4] = *(--name);
	    name = buf;
	} else if (!*(++name)	|| !*(++name)) {
	    /* Not at least 6 hex digits, so invalid */
	    return 0;
	} else if (!*(++name)) {
	    /* Exactly 6 hex digits */
	    buf[10] = buf[12] = *(--name);
	    buf[9] = buf[11] = *(--name);
	    buf[6] = buf[8] = *(--name);
	    buf[5] = buf[7] = *(--name);
	    buf[2] = buf[4] = *(--name);
	    buf[1] = buf[3] = *(--name);
	    name = buf;
	} else if (!*(++name) || !*(++name)) {









	    /* Not at least 9 hex digits, so invalid */
	    return 0;
	} else if (!*(++name)) {
	    /* Exactly 9 hex digits */
	    buf[11] = *(--name);
	    buf[10] = *(--name);
	    buf[9] = buf[12] = *(--name);
	    buf[7] = *(--name);
	    buf[6] = *(--name);
	    buf[5] = buf[8] = *(--name);
	    buf[3] = *(--name);
	    buf[2] = *(--name);
	    buf[1] = buf[4] = *(--name);
	    name = buf;
	} else if (!*(++name) || !*(++name) || *(++name)) {
	    /* Not exactly 12 hex digits, so invalid */
	    return 0;
	} else {
	    name -= 13;
	}
    } else {
	if (strlen(name) > 99) {
	    /* Don't bother to parse this. [Bug 2809525]*/
	    return 0;
	}
    }
    return XParseColor(display, map, name, color);
}
#endif /* __WIN32__ */

Changes to unix/tkUnixColor.c.

124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
    Tk_Uid name;		/* Name of color to allocated (in form
				 * suitable for passing to XParseColor). */
{
    Display *display = Tk_Display(tkwin);
    Colormap colormap = Tk_Colormap(tkwin);
    XColor color;
    TkColor *tkColPtr;
    char buf[100];
    unsigned len = strlen(name);

    /*
     * Make sure that we never exceed a reasonable length of color name. A
     * good maximum length is 99, arbitrary, but larger than any known color
     * name. [Bug 2809525]
     */

    if (len > 99) {
	len = 99;
    }
    memcpy(buf, name, len);
    buf[len] = '\0';

    /*
     * Map from the name to a pixel value.  Call XAllocNamedColor rather than
     * XParseColor for non-# names: this saves a server round-trip for those
     * names.
     */

    if (*name != '#') {
	XColor screen;




	if (XAllocNamedColor(display, colormap, buf, &screen, &color) != 0) {
	    DeleteStressedCmap(display, colormap);
	} else {
	    /*
	     * Couldn't allocate the color.  Try translating the name to
	     * a color value, to see whether the problem is a bad color
	     * name or a full colormap.  If the colormap is full, then
	     * pick an approximation to the desired color.
	     */

	    if (XLookupColor(display, colormap, buf, &color, &screen) == 0) {
		return (TkColor *) NULL;
	    }
	    FindClosestColor(tkwin, &screen, &color);
	}
    } else {
	if (TkParseColor(display, colormap, buf, &color) == 0) {
	    return (TkColor *) NULL;
	}
	if (XAllocColor(display, colormap, &color) != 0) {
	    DeleteStressedCmap(display, colormap);
	} else {
	    FindClosestColor(tkwin, &color, &color);
	}







<
<
<
<
<
<
<
<
<
<
<
<
<
<










>
>
>
|









|





|







124
125
126
127
128
129
130














131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
    Tk_Uid name;		/* Name of color to allocated (in form
				 * suitable for passing to XParseColor). */
{
    Display *display = Tk_Display(tkwin);
    Colormap colormap = Tk_Colormap(tkwin);
    XColor color;
    TkColor *tkColPtr;















    /*
     * Map from the name to a pixel value.  Call XAllocNamedColor rather than
     * XParseColor for non-# names: this saves a server round-trip for those
     * names.
     */

    if (*name != '#') {
	XColor screen;

	if (strlen(name) > 99) {
	/* Don't bother to parse this. [Bug 2809525]*/
	return (TkColor *) NULL;
    } else if (XAllocNamedColor(display, colormap, name, &screen, &color) != 0) {
	    DeleteStressedCmap(display, colormap);
	} else {
	    /*
	     * Couldn't allocate the color.  Try translating the name to
	     * a color value, to see whether the problem is a bad color
	     * name or a full colormap.  If the colormap is full, then
	     * pick an approximation to the desired color.
	     */

	    if (XLookupColor(display, colormap, name, &color, &screen) == 0) {
		return (TkColor *) NULL;
	    }
	    FindClosestColor(tkwin, &screen, &color);
	}
    } else {
	if (TkParseColor(display, colormap, name, &color) == 0) {
	    return (TkColor *) NULL;
	}
	if (XAllocColor(display, colormap, &color) != 0) {
	    DeleteStressedCmap(display, colormap);
	} else {
	    FindClosestColor(tkwin, &color, &color);
	}