From: Luca Boccassi Date: Wed, 10 Sep 2025 12:25:30 +0000 (+0100) Subject: ansi-color: fix stack overflow with debug level and invalid SYSTEMD_COLORS env var X-Git-Tag: v258~19 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=f82d80da067aa6c73c19ea84c0261db4961368c4;p=thirdparty%2Fsystemd.git 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 --- 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. */