From f82d80da067aa6c73c19ea84c0261db4961368c4 Mon Sep 17 00:00:00 2001 From: Luca Boccassi Date: Wed, 10 Sep 2025 13:25:30 +0100 Subject: [PATCH] ansi-color: fix stack overflow with debug level and invalid SYSTEMD_COLORS env var 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 | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/basic/ansi-color.c b/src/basic/ansi-color.c index 9dce2f44188..f750b6f58b7 100644 --- a/src/basic/ansi-color.c +++ b/src/basic/ansi-color.c @@ -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. */ -- 2.47.3