Tk Source Code

Check-in [550636bb]
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 | trunk
Files: files | file ages | folders
SHA1: 550636bb493213960f40920b25006e5fad9f77d4
User & Date: jan.nijtmans 2012-03-21 22:23:32
Context
2012-03-26
12:35
fix for scoping of buf (introduced with fix for [Bug 2809525]) check-in: e8d270f5 user: jan.nijtmans tags: trunk
12:21
implement for Unix as well check-in: 4e406d9e user: jan.nijtmans tags: jn-web-colors
2012-03-21
22:23
[Bug 2809525] Abort on overlong color name check-in: 550636bb user: jan.nijtmans tags: trunk
22:22
[Bug 2809525] Abort on overlong color name check-in: 89ebf1b8 user: jan.nijtmans tags: core-8-5-branch
2012-03-18
21:03
rfe-3503317: XParseColor speedup check-in: fb808d0d user: jan.nijtmans tags: trunk
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-13  Donal K. Fellows  <[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-13  Donal K. Fellows  <[email protected]>

Changes to generic/tkColor.c.

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



877
878
879




880

881
882
883
884
885
886
887
888
}

#ifndef __WIN32__
/* This function is not necessary for Win32,
 * since XParseColor already does the right thing */
Status
TkParseColor(
    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__ */
/*
 * Local Variables:
 * mode: c
 * c-basic-offset: 4
 * fill-column: 78







|

|
|

|
|
|
|
|

|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
>
>
>

|

>
>
>
>
|
>
|







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
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
}

#ifndef __WIN32__
/* This function is not necessary for Win32,
 * since XParseColor already does the right thing */
Status
TkParseColor(
    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__ */
/*
 * Local Variables:
 * mode: c
 * c-basic-offset: 4
 * fill-column: 78

Changes to unix/tkUnixColor.c.

122
123
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
    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 NULL;
	    }
	    FindClosestColor(tkwin, &screen, &color);
	}
    } else {
	if (TkParseColor(display, colormap, buf, &color) == 0) {
	    return NULL;
	}
	if (XAllocColor(display, colormap, &color) != 0) {
	    DeleteStressedCmap(display, colormap);
	} else {
	    FindClosestColor(tkwin, &color, &color);
	}







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










>
>
>
|









|





|







122
123
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
    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 NULL;
	    }
	    FindClosestColor(tkwin, &screen, &color);
	}
    } else {
	if (TkParseColor(display, colormap, name, &color) == 0) {
	    return NULL;
	}
	if (XAllocColor(display, colormap, &color) != 0) {
	    DeleteStressedCmap(display, colormap);
	} else {
	    FindClosestColor(tkwin, &color, &color);
	}