aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMattias EngdegÄrd2020-06-08 13:06:51 +0200
committerMattias EngdegÄrd2020-06-08 13:24:29 +0200
commit64e25cde324b2e270acf82958abb59018e67f841 (patch)
tree8648c8efadfa1d9208533094f3ea0e8ece95482e
parent46304c474c433a98ad55198e1fbdb0b1136645c1 (diff)
downloademacs-64e25cde324b2e270acf82958abb59018e67f841.tar.gz
emacs-64e25cde324b2e270acf82958abb59018e67f841.zip
More robust NS hex colour string parsing
Invalid arguments to color-values, such as "#abcdefg" or "#1234", or valid ones like "#111222333", should not yield nonsense values. * src/nsterm.m (ns_get_color): Only accept "#RGB" strings with 1-4 digits per components, equal number of digits each, and no trailing characters. Parse 12-bit colours correctly.
-rw-r--r--src/nsterm.m21
1 files changed, 12 insertions, 9 deletions
diff --git a/src/nsterm.m b/src/nsterm.m
index 19531389545..3dc7e1db7c9 100644
--- a/src/nsterm.m
+++ b/src/nsterm.m
@@ -2399,20 +2399,23 @@ ns_get_color (const char *name, NSColor **col)
2399 scaling = (snprintf (hex, sizeof hex, "%s", name + 4) - 2) / 3; 2399 scaling = (snprintf (hex, sizeof hex, "%s", name + 4) - 2) / 3;
2400 else if (name[0] == '#') /* An old X11 format; convert to newer */ 2400 else if (name[0] == '#') /* An old X11 format; convert to newer */
2401 { 2401 {
2402 int len = (strlen(name) - 1); 2402 int len = 0;
2403 int start = (len % 3 == 0) ? 1 : len / 4 + 1; 2403 while (isxdigit (name[len + 1]))
2404 int i; 2404 len++;
2405 scaling = strlen(name+start) / 3; 2405 if (name[len + 1] == '\0' && len >= 1 && len <= 12 && len % 3 == 0)
2406 for (i = 0; i < 3; i++) 2406 {
2407 sprintf (hex + i * (scaling + 1), "%.*s/", scaling, 2407 scaling = len / 3;
2408 name + start + i * scaling); 2408 for (int i = 0; i < 3; i++)
2409 hex[3 * (scaling + 1) - 1] = '\0'; 2409 sprintf (hex + i * (scaling + 1), "%.*s/", scaling,
2410 name + 1 + i * scaling);
2411 hex[3 * (scaling + 1) - 1] = '\0';
2412 }
2410 } 2413 }
2411 2414
2412 if (hex[0]) 2415 if (hex[0])
2413 { 2416 {
2414 unsigned int rr, gg, bb; 2417 unsigned int rr, gg, bb;
2415 float fscale = scaling == 4 ? 65535.0 : (scaling == 2 ? 255.0 : 15.0); 2418 float fscale = (1 << (scaling * 4)) - 1;
2416 if (sscanf (hex, "%x/%x/%x", &rr, &gg, &bb)) 2419 if (sscanf (hex, "%x/%x/%x", &rr, &gg, &bb))
2417 { 2420 {
2418 r = rr / fscale; 2421 r = rr / fscale;