[Lldb-commits] [PATCH] D49307: Fix some crashes and deadlocks in FormatAnsiTerminalCodes

Raphael Isemann via Phabricator via lldb-commits lldb-commits at lists.llvm.org
Mon Jul 16 09:42:59 PDT 2018


teemperor updated this revision to Diff 155706.
teemperor added a comment.

- Removed temp string variables.


https://reviews.llvm.org/D49307

Files:
  include/lldb/Utility/AnsiTerminal.h
  unittests/Utility/AnsiTerminalTest.cpp
  unittests/Utility/CMakeLists.txt


Index: unittests/Utility/CMakeLists.txt
===================================================================
--- unittests/Utility/CMakeLists.txt
+++ unittests/Utility/CMakeLists.txt
@@ -1,4 +1,5 @@
 add_lldb_unittest(UtilityTests
+  AnsiTerminalTest.cpp
   ArgsTest.cpp
   OptionsWithRawTest.cpp
   ArchSpecTest.cpp
Index: unittests/Utility/AnsiTerminalTest.cpp
===================================================================
--- /dev/null
+++ unittests/Utility/AnsiTerminalTest.cpp
@@ -0,0 +1,55 @@
+//===-- AnsiTerminalTest.cpp ------------------------------------*- C++ -*-===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#include "gtest/gtest.h"
+
+#include "lldb/Utility/AnsiTerminal.h"
+
+using namespace lldb_utility;
+
+TEST(AnsiTerminal, Empty) { EXPECT_EQ("", ansi::FormatAnsiTerminalCodes("")); }
+
+TEST(AnsiTerminal, WhiteSpace) {
+  EXPECT_EQ(" ", ansi::FormatAnsiTerminalCodes(" "));
+}
+
+TEST(AnsiTerminal, AtEnd) {
+  EXPECT_EQ("abc\x1B[30m",
+            ansi::FormatAnsiTerminalCodes("abc${ansi.fg.black}"));
+}
+
+TEST(AnsiTerminal, AtStart) {
+  EXPECT_EQ("\x1B[30mabc",
+            ansi::FormatAnsiTerminalCodes("${ansi.fg.black}abc"));
+}
+
+TEST(AnsiTerminal, KnownPrefix) {
+  EXPECT_EQ("${ansi.fg.redish}abc",
+            ansi::FormatAnsiTerminalCodes("${ansi.fg.redish}abc"));
+}
+
+TEST(AnsiTerminal, Unknown) {
+  EXPECT_EQ("${ansi.fg.foo}abc",
+            ansi::FormatAnsiTerminalCodes("${ansi.fg.foo}abc"));
+}
+
+TEST(AnsiTerminal, Incomplete) {
+  EXPECT_EQ("abc${ansi.", ansi::FormatAnsiTerminalCodes("abc${ansi."));
+}
+
+TEST(AnsiTerminal, Twice) {
+  EXPECT_EQ("\x1B[30m\x1B[31mabc",
+            ansi::FormatAnsiTerminalCodes("${ansi.fg.black}${ansi.fg.red}abc"));
+}
+
+TEST(AnsiTerminal, Basic) {
+  EXPECT_EQ(
+      "abc\x1B[31mabc\x1B[0mabc",
+      ansi::FormatAnsiTerminalCodes("abc${ansi.fg.red}abc${ansi.normal}abc"));
+}
Index: include/lldb/Utility/AnsiTerminal.h
===================================================================
--- include/lldb/Utility/AnsiTerminal.h
+++ include/lldb/Utility/AnsiTerminal.h
@@ -119,17 +119,21 @@
       break;
     }
 
+    bool found_code = false;
     for (const auto &code : codes) {
       if (!right.consume_front(code.name))
         continue;
 
       if (do_color)
         fmt.append(code.value);
-      format = right;
+      found_code = true;
       break;
     }
-
-    format = format.drop_front();
+    format = right;
+    // If we haven't found a valid replacement value, we just copy the string
+    // to the result without any modifications.
+    if (!found_code)
+      fmt.append(tok_hdr);
   }
   return fmt;
 }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D49307.155706.patch
Type: text/x-patch
Size: 2856 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/lldb-commits/attachments/20180716/9a597966/attachment-0001.bin>


More information about the lldb-commits mailing list