From 426fa82f8fb687bff856971b208ea2f870a01fbf Mon Sep 17 00:00:00 2001 From: Mahmoud Al-Qudsi Date: Tue, 17 Aug 2021 19:04:10 -0500 Subject: [PATCH] Fix recently broken escape_code_length() result `escape_code_length()` was converted from returning a `size_t` to returning a `maybe_t` but that subtly broke all existing call sites by forcing all input to go through the slow path of assuming a zero-length escape sequence was found. This is because all callers predicated their next action on what amounts to `if (escape_code_length(...))` which would correctly skip the slow path when `escape_code_length` returned zero, but after the conversion to `maybe_t` contained not `maybe_t::none()` but rather `maybe_t::some(0)` due to coercion of the result from the `size_t` local `esc_seq_len` to the `maybe_t` return value - which, when coerced to a boolean returns *true* for `maybe_t::some(0)` rather than false. The regression was introduced in 7ad855a844a271770ad659622a10f9fe273566b4 and did not ship in any released versions so no harm, no foul. --- src/screen.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/screen.cpp b/src/screen.cpp index 88a49ef24..84693eee8 100644 --- a/src/screen.cpp +++ b/src/screen.cpp @@ -249,7 +249,7 @@ static bool is_visual_escape_seq(const wchar_t *code, size_t *resulting_length) /// the escape sequence based on querying terminfo and other heuristics. maybe_t escape_code_length(const wchar_t *code) { assert(code != nullptr); - if (*code != L'\x1B') return 0; + if (*code != L'\x1B') return none(); size_t esc_seq_len = 0; bool found = is_color_escape_seq(code, &esc_seq_len); @@ -259,7 +259,8 @@ maybe_t escape_code_length(const wchar_t *code) { if (!found) found = is_three_byte_escape_seq(code, &esc_seq_len); if (!found) found = is_csi_style_escape_seq(code, &esc_seq_len); if (!found) found = is_two_byte_escape_seq(code, &esc_seq_len); - return esc_seq_len; + + return found ? maybe_t{esc_seq_len} : none(); } size_t layout_cache_t::escape_code_length(const wchar_t *code) {