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 c8210d98a4
This commit is contained in:
Luca Boccassi
2025-09-10 13:25:30 +01:00
parent 6becea2859
commit f82d80da06

View 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. */