[Lldb-commits] [lldb] [lldb] Avoid UB in ansi::TrimAtWordBoundary() (PR #229324)

Igor Kudrin via lldb-commits lldb-commits at lists.llvm.org
Wed Oct 7 15:46:07 PDT 2026


https://github.com/igorkudrin updated https://github.com/llvm/llvm-project/pull/229324

>From f15bab462c3ad0b3748440c0bf16db61e62a8f9c Mon Sep 17 00:00:00 2001
From: Igor Kudrin <ikudrin at accesssoftek.com>
Date: Tue, 6 Oct 2026 00:21:08 -0700
Subject: [PATCH] [lldb] Avoid UB in ansi::TrimAtWordBoundary()
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

UtilityTest fails on Windows in the debug configuration when
AnsiTerminal.TrimAtWordBoundary() calls ansi::TrimAtWordBoundary() with
Unicode strings containing bytes with the high bit set, which leads to
std::isspace() being called with a negative value. C11 ยง7.4 paragraph 1
requires the argument to be representable as unsigned char or equal to
EOF. Passing any other value is UB.

See also: https://en.cppreference.com/cpp/string/byte/isspace
---
 lldb/include/lldb/Utility/AnsiTerminal.h | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)

diff --git a/lldb/include/lldb/Utility/AnsiTerminal.h b/lldb/include/lldb/Utility/AnsiTerminal.h
index 68054454dc2025..ee581abc74e09e 100644
--- a/lldb/include/lldb/Utility/AnsiTerminal.h
+++ b/lldb/include/lldb/Utility/AnsiTerminal.h
@@ -303,7 +303,8 @@ inline std::string TrimAtWordBoundary(llvm::StringRef str,
     if (result_visible_length + column_width <= visible_length) {
       result.append(left).append(escape);
       result_visible_length += column_width;
-      at_word_boundary = right.empty() || std::isspace(right[0]);
+      at_word_boundary =
+          right.empty() || std::isspace(static_cast<unsigned char>(right[0]));
 
       continue;
     }
@@ -332,8 +333,10 @@ inline std::string TrimAtWordBoundary(llvm::StringRef str,
            visible_length)) {
         result.append(trimmed);
         result_visible_length += trimmed_width;
-        at_word_boundary = std::isspace(trimmed.back()) ||
-                           (following_char && std::isspace(*following_char));
+        at_word_boundary =
+            std::isspace(static_cast<unsigned char>(trimmed.back())) ||
+            (following_char &&
+             std::isspace(static_cast<unsigned char>(*following_char)));
 
         break;
       }



More information about the lldb-commits mailing list