mirror of
https://codeberg.org/dnkl/foot.git
synced 2026-03-13 05:33:51 -04:00
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
This commit is contained in:
parent
85a4e4ccc1
commit
02fff24b4f
3 changed files with 34 additions and 6 deletions
12
CHANGELOG.md
12
CHANGELOG.md
|
|
@ -52,9 +52,21 @@
|
||||||
## Unreleased
|
## Unreleased
|
||||||
### Added
|
### Added
|
||||||
### Changed
|
### Changed
|
||||||
|
|
||||||
|
* config: ARGB color values now default to opaque, rather than
|
||||||
|
transparent, when the alpha component has been left out
|
||||||
|
([#1526][1526]).
|
||||||
|
|
||||||
|
[1526]: https://codeberg.org/dnkl/foot/issues/1526
|
||||||
|
|
||||||
|
|
||||||
### Deprecated
|
### Deprecated
|
||||||
### Removed
|
### Removed
|
||||||
### Fixed
|
### Fixed
|
||||||
|
|
||||||
|
* config: improved validation of color values.
|
||||||
|
|
||||||
|
|
||||||
### Security
|
### Security
|
||||||
### Contributors
|
### Contributors
|
||||||
|
|
||||||
|
|
|
||||||
24
config.c
24
config.c
|
|
@ -618,18 +618,30 @@ value_to_enum(struct context *ctx, const char **value_map, int *res)
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool NOINLINE
|
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)) {
|
uint32_t color;
|
||||||
LOG_CONTEXTUAL_ERR("not a valid color value");
|
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;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!allow_alpha && (*color & 0xff000000) != 0) {
|
if (allow_alpha && component_count == 3) {
|
||||||
LOG_CONTEXTUAL_ERR("color value must not have an alpha component");
|
/* If user left out the alpha component, assume non-transparency */
|
||||||
return false;
|
color |= 0xff000000;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
*result = color;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -426,6 +426,10 @@ test_color(struct context *ctx, bool (*parse_fun)(struct context *ctx),
|
||||||
{"ffffff", 0xffffff},
|
{"ffffff", 0xffffff},
|
||||||
{"ffffffff", 0xffffffff, !alpha_allowed},
|
{"ffffffff", 0xffffffff, !alpha_allowed},
|
||||||
{"aabbccdd", 0xaabbccdd, !alpha_allowed},
|
{"aabbccdd", 0xaabbccdd, !alpha_allowed},
|
||||||
|
{"00", 0, true},
|
||||||
|
{"0000", 0, true},
|
||||||
|
{"00000", 0, true},
|
||||||
|
{"000000000", 0, true},
|
||||||
{"unittest-invalid-color", 0, true},
|
{"unittest-invalid-color", 0, true},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue