[Lldb-commits] [lldb] r337189 - Fix some crashes and deadlocks in FormatAnsiTerminalCodes
Raphael Isemann via lldb-commits
lldb-commits at lists.llvm.org
Mon Jul 16 09:38:30 PDT 2018
Author: teemperor
Date: Mon Jul 16 09:38:30 2018
New Revision: 337189
URL: http://llvm.org/viewvc/llvm-project?rev=337189&view=rev
Log:
Fix some crashes and deadlocks in FormatAnsiTerminalCodes
Summary:
This patch fixes a few problems with the FormatAnsiTerminalCodes function:
* It does an infinite loop on an unknown color value.
* It crashes when the color value is at the end of the string.
* It deletes the first character behind the color token.
Also added a few tests that reproduce those problems (and test some other corner cases).
Reviewers: davide, labath
Reviewed By: labath
Subscribers: labath, lldb-commits, mgorny
Differential Revision: https://reviews.llvm.org/D49307
Added:
lldb/trunk/unittests/Utility/AnsiTerminalTest.cpp
Modified:
lldb/trunk/include/lldb/Utility/AnsiTerminal.h
lldb/trunk/unittests/Utility/CMakeLists.txt
Modified: lldb/trunk/include/lldb/Utility/AnsiTerminal.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/Utility/AnsiTerminal.h?rev=337189&r1=337188&r2=337189&view=diff
==============================================================================
--- lldb/trunk/include/lldb/Utility/AnsiTerminal.h (original)
+++ lldb/trunk/include/lldb/Utility/AnsiTerminal.h Mon Jul 16 09:38:30 2018
@@ -119,17 +119,21 @@ inline std::string FormatAnsiTerminalCod
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;
}
Added: lldb/trunk/unittests/Utility/AnsiTerminalTest.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/unittests/Utility/AnsiTerminalTest.cpp?rev=337189&view=auto
==============================================================================
--- lldb/trunk/unittests/Utility/AnsiTerminalTest.cpp (added)
+++ lldb/trunk/unittests/Utility/AnsiTerminalTest.cpp Mon Jul 16 09:38:30 2018
@@ -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"));
+}
Modified: lldb/trunk/unittests/Utility/CMakeLists.txt
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/unittests/Utility/CMakeLists.txt?rev=337189&r1=337188&r2=337189&view=diff
==============================================================================
--- lldb/trunk/unittests/Utility/CMakeLists.txt (original)
+++ lldb/trunk/unittests/Utility/CMakeLists.txt Mon Jul 16 09:38:30 2018
@@ -1,4 +1,5 @@
add_lldb_unittest(UtilityTests
+ AnsiTerminalTest.cpp
ArgsTest.cpp
OptionsWithRawTest.cpp
ArchSpecTest.cpp
More information about the lldb-commits
mailing list