]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
ansi-color: assert on final result of get_color_mode() being settled
authorMike Yuan <me@yhndnzj.com>
Fri, 23 Jan 2026 14:03:57 +0000 (15:03 +0100)
committerMike Yuan <me@yhndnzj.com>
Tue, 27 Jan 2026 18:17:42 +0000 (19:17 +0100)
src/basic/ansi-color.c
src/basic/ansi-color.h

index 5b04199318aaf4c52a1435e14be5bf6c2a6fd787..da91cf6d7e2a43f46c1c71a6786c017bf08cdbcc 100644 (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",
index fb6b2bba0c25808e66c0aa467fbc2bc32cb97119..04b265dd16c0caac8b9f3d83b7a449ad7fba8e29 100644 (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;