]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
ansi-color: fix stack overflow with debug level and invalid SYSTEMD_COLORS env var
authorLuca Boccassi <luca.boccassi@gmail.com>
Wed, 10 Sep 2025 12:25:30 +0000 (13:25 +0100)
committerLuca Boccassi <luca.boccassi@gmail.com>
Fri, 12 Sep 2025 09:58:26 +0000 (10:58 +0100)
When SYSTEMD_COLORS is invalid, parse_systemd_colors() logs about it.
Logging helpers then call into parse_systemd_colors() to pretty-print
the log message, which then fails, so it logs about the failure,
rinse and repeat until segfault.

Follow-up for c8210d98a4b64af6fadb1cb765c0451758af1303

src/basic/ansi-color.c

index 9dce2f44188f46a93896ae0a801174c5fcff6962..f750b6f58b73b3fe3b16e233fa950e9001077932 100644 (file)
@@ -36,18 +36,20 @@ void reset_ansi_feature_caches(void) {
 ColorMode parse_systemd_colors(void) {
         const char *e;
 
+        /* Note: do not log in this function, to avoid infinite recursion issues, as the log functions call
+         * this when deciding whether to color the output. */
+
         e = getenv("SYSTEMD_COLORS");
         if (!e)
                 return _COLOR_MODE_INVALID;
 
-        ColorMode m = color_mode_from_string(e);
-        if (m < 0)
-                return log_debug_errno(m, "Failed to parse $SYSTEMD_COLORS value '%s', ignoring: %m", e);
-
-        return m;
+        return color_mode_from_string(e);
 }
 
 static ColorMode get_color_mode_impl(void) {
+        /* Note: do not log in this function, to avoid infinite recursion issues, as the log functions call
+         * this when deciding whether to color the output. */
+
         /* Returns the mode used to choose output colors. The possible modes are COLOR_OFF for no colors,
          * COLOR_16 for only the base 16 ANSI colors, COLOR_256 for more colors, and COLOR_24BIT for
          * unrestricted color output. */