ansi-color: assert on final result of get_color_mode() being settled

This commit is contained in:
Mike Yuan
2026-01-27 19:17:42 +01:00
parent cab4b9defd
commit 97be547c6f
2 changed files with 11 additions and 5 deletions
+4 -3
View File
@@ -55,7 +55,7 @@ static ColorMode get_color_mode_impl(void) {
/* First, we check $SYSTEMD_COLORS, which is the explicit way to change the mode. */
ColorMode m = parse_systemd_colors();
if (IN_SET(m, COLOR_OFF, COLOR_16, COLOR_256, COLOR_24BIT))
if (m >= 0 && m < _COLOR_MODE_FIXED_MAX)
return m;
/* Next, check for the presence of $NO_COLOR; value is ignored. */
@@ -92,13 +92,14 @@ static ColorMode get_color_mode_impl(void) {
}
ColorMode get_color_mode(void) {
if (cached_color_mode < 0)
if (cached_color_mode < 0) {
cached_color_mode = get_color_mode_impl();
assert(cached_color_mode >= 0 && cached_color_mode < _COLOR_MODE_FIXED_MAX);
}
return cached_color_mode;
}
static const char* const color_mode_table[_COLOR_MODE_MAX] = {
[COLOR_OFF] = "off",
[COLOR_16] = "16",
+7 -2
View File
@@ -9,9 +9,14 @@ typedef enum ColorMode {
COLOR_16, /* Only the base 16 colors. */
COLOR_256, /* Only 256 colors. */
COLOR_24BIT, /* For truecolor or 24bit color support, no restriction. */
COLOR_AUTO_16, /* The "AUTO" modes are as the above, but subject to suitable settings for */
COLOR_AUTO_256, /* the environment variables TERM and NO_COLOR. */
_COLOR_MODE_FIXED_MAX,
/* The "AUTO" modes are as the above, but subject to suitable settings for the environment variables
* TERM and NO_COLOR. */
COLOR_AUTO_16 = _COLOR_MODE_FIXED_MAX,
COLOR_AUTO_256,
COLOR_AUTO_24BIT,
_COLOR_MODE_MAX,
_COLOR_MODE_INVALID = -EINVAL,
} ColorMode;