aboutsummaryrefslogtreecommitdiff
path: root/config.c
diff options
context:
space:
mode:
authorDaniel Eklöf <daniel@ekloef.se>2023-10-26 14:22:41 +0000
committerDaniel Eklöf <daniel@ekloef.se>2023-10-26 14:25:57 +0000
commit02fff24b4fa27c500a964148d54f793ddc1df1cb (patch)
treedb4099744b665abd3e00a9e36bc8f3c6f2f46f23 /config.c
parentadd CCACHE_DISABLE=1 to pgo.sh to avoid errors when ccache is enabled (diff)
config: improve validation of color values, default alpha to 0xff
Reject color values that aren't in either RGB, or ARGB format. That is, color values that aren't hexadecimal numbers with either 6 or 8 digits. Also, if a color value is allowed to have an alpha component, and the user left it out, default to 0xff (opaque) rather than 0x00 (fully transparent). Closes #1526
Diffstat (limited to 'config.c')
-rw-r--r--config.c24
1 files changed, 18 insertions, 6 deletions
diff --git a/config.c b/config.c
index 35c847e9..98d2918b 100644
--- a/config.c
+++ b/config.c
@@ -618,18 +618,30 @@ value_to_enum(struct context *ctx, const char **value_map, int *res)
}
static bool NOINLINE
-value_to_color(struct context *ctx, uint32_t *color, bool allow_alpha)
+value_to_color(struct context *ctx, uint32_t *result, bool allow_alpha)
{
- if (!str_to_uint32(ctx->value, 16, color)) {
- LOG_CONTEXTUAL_ERR("not a valid color value");
+ uint32_t color;
+ const size_t len = strlen(ctx->value);
+ const size_t component_count = len / 2;
+
+ if (!(len == 6 || (allow_alpha && len == 8)) ||
+ !str_to_uint32(ctx->value, 16, &color))
+ {
+ if (allow_alpha) {
+ LOG_CONTEXTUAL_ERR("color must be in either RGB or ARGB format");
+ } else {
+ LOG_CONTEXTUAL_ERR("color must be in RGB format");
+ }
+
return false;
}
- if (!allow_alpha && (*color & 0xff000000) != 0) {
- LOG_CONTEXTUAL_ERR("color value must not have an alpha component");
- return false;
+ if (allow_alpha && component_count == 3) {
+ /* If user left out the alpha component, assume non-transparency */
+ color |= 0xff000000;
}
+ *result = color;
return true;
}