From e776f2a4d9a335a41d9cbc8b06ba97a660051614 Mon Sep 17 00:00:00 2001 From: Bryce Harrington Date: Thu, 14 Jul 2016 18:28:03 -0700 Subject: config-parser: Add weston_config_section_get_color Previously weston_config_section_get_uint was serving dual purpose for parsing both unsigned decimal integer values (ids, counts, seconds, etc.) and hexadecimal values (colors), by relying on strtoul's auto-detection mechanism. However, this usage is unable to catch certain kinds of error conditions, such as specifying a negative number where an unsigned should be used. And for colors in particular, it would misparse hex values if the leading 0x was omitted. E.g. "background-color=99999999" would render a near-black background (effectively 0x05f5e0ff) instead of medium grey, and "background-color=ffffffff" would be treated as an error rather than white. "background-color=0x01234567", "background-color=01234567", and "background-color=1234567" each resulted in the value being parsed as hexadecimal, octal, and decimal respectively, resulting in colors 0x01234567, 0x00053977, and 0x0012d687 being displayed. This new routine forces hexadecimal to be used in all cases when parsing color values, so "0x01234567" and "01234567" result in the same color value, "99999999" is grey, and "ffffffff" is white. It also requires exactly 8 or 10 digits (other lengths likely indicate typos), or the value "0" (black). Signed-off-by: Bryce Harrington Reviewed-by: Eric Engestrom --- shared/config-parser.c | 39 +++++++++++++++++++++++++++++++++++++++ shared/config-parser.h | 4 ++++ 2 files changed, 43 insertions(+) (limited to 'shared') diff --git a/shared/config-parser.c b/shared/config-parser.c index 1e08759e..33d5e361 100644 --- a/shared/config-parser.c +++ b/shared/config-parser.c @@ -207,6 +207,45 @@ weston_config_section_get_uint(struct weston_config_section *section, return 0; } +WL_EXPORT +int +weston_config_section_get_color(struct weston_config_section *section, + const char *key, + uint32_t *color, uint32_t default_color) +{ + struct weston_config_entry *entry; + int len; + char *end; + + entry = config_section_get_entry(section, key); + if (entry == NULL) { + *color = default_color; + errno = ENOENT; + return -1; + } + + len = strlen(entry->value); + if (len == 1 && entry->value[0] == '0') { + *color = 0; + return 0; + } else if (len != 8 && len != 10) { + fprintf(stderr, "string '%s' is length %d\n", entry->value, len); + *color = default_color; + errno = EINVAL; + return -1; + } + + errno = 0; + *color = strtoul(entry->value, &end, 16); + if (errno != 0 || end == entry->value || *end != '\0') { + *color = default_color; + errno = EINVAL; + return -1; + } + + return 0; +} + WL_EXPORT int weston_config_section_get_double(struct weston_config_section *section, diff --git a/shared/config-parser.h b/shared/config-parser.h index b8462a72..17ef5d73 100644 --- a/shared/config-parser.h +++ b/shared/config-parser.h @@ -85,6 +85,10 @@ weston_config_section_get_uint(struct weston_config_section *section, const char *key, uint32_t *value, uint32_t default_value); int +weston_config_section_get_color(struct weston_config_section *section, + const char *key, + uint32_t *color, uint32_t default_color); +int weston_config_section_get_double(struct weston_config_section *section, const char *key, double *value, double default_value); -- cgit v1.2.3