From a7df92e1874dc347c9f82457e7ab921e24f5bb8d Mon Sep 17 00:00:00 2001 From: Fabian Homborg Date: Tue, 9 Mar 2021 13:46:08 +0100 Subject: [PATCH] Fix crash with `set_color --print-colors --background normal` Found in conjunction with #7805. --- src/builtin_set_color.cpp | 8 +++++++- tests/pexpects/set_color.py | 21 +++++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/src/builtin_set_color.cpp b/src/builtin_set_color.cpp index 07d2bf87a..f8f54f4de 100644 --- a/src/builtin_set_color.cpp +++ b/src/builtin_set_color.cpp @@ -188,13 +188,19 @@ maybe_t builtin_set_color(parser_t &parser, io_streams_t &streams, wchar_t } } - const rgb_color_t bg = rgb_color_t(bgcolor ? bgcolor : L""); + rgb_color_t bg = rgb_color_t(bgcolor ? bgcolor : L""); if (bgcolor && bg.is_none()) { streams.err.append_format(_(L"%ls: Unknown color '%ls'\n"), argv[0], bgcolor); return STATUS_INVALID_ARGS; } if (print) { + // Hack: Explicitly setting a background of "normal" crashes + // for --print-colors. Because it's not interesting in terms of display, + // just skip it. + if (bgcolor && bg.is_special()) { + bg = rgb_color_t(L""); + } print_colors(streams, bold, underline, italics, dim, reverse, bg); return STATUS_CMD_OK; } diff --git a/tests/pexpects/set_color.py b/tests/pexpects/set_color.py index b645f9090..a1e9676cc 100644 --- a/tests/pexpects/set_color.py +++ b/tests/pexpects/set_color.py @@ -61,3 +61,24 @@ expect_str("white") expect_str("yellow") expect_str("normal") expect_prompt() + +# This used to crash +sendline("set_color --print-colors --background normal") +expect_str("black") +expect_str("blue") +expect_str("brblack") +expect_str("brblue") +expect_str("brcyan") +expect_str("brgreen") +expect_str("brmagenta") +expect_str("brred") +expect_str("brwhite") +expect_str("bryellow") +expect_str("cyan") +expect_str("green") +expect_str("magenta") +expect_str("\n\x1b[31mred") +expect_str("\n\x1b[37mwhite") +expect_str("\n\x1b[33myellow") +expect_str("normal") +expect_prompt()