Fix crash in clang-format.
authorManuel Klimek <klimek@google.com>
Thu, 11 Jun 2015 10:14:13 +0000 (10:14 +0000)
committerManuel Klimek <klimek@google.com>
Thu, 11 Jun 2015 10:14:13 +0000 (10:14 +0000)
The following example used to crash clang-format.
 #define a\
  /**/}

Adjusting the indentation level cache for the line starting with the
comment would lead to an out-of-bounds array read.

llvm-svn: 239521

clang/lib/Format/UnwrappedLineFormatter.cpp
clang/unittests/Format/FormatTest.cpp

index cbf8c6c..ee81b50 100644 (file)
@@ -51,11 +51,13 @@ public:
   /// next.
   void nextLine(const AnnotatedLine &Line) {
     Offset = getIndentOffset(*Line.First);
+    // Update the indent level cache size so that we can rely on it
+    // having the right size in adjustToUnmodifiedline.
+    while (IndentForLevel.size() <= Line.Level)
+      IndentForLevel.push_back(-1);
     if (Line.InPPDirective) {
       Indent = Line.Level * Style.IndentWidth + AdditionalIndent;
     } else {
-      while (IndentForLevel.size() <= Line.Level)
-        IndentForLevel.push_back(-1);
       IndentForLevel.resize(Line.Level + 1);
       Indent = getIndent(IndentForLevel, Line.Level);
     }
index 5b3980b..46be32b 100644 (file)
@@ -10576,7 +10576,10 @@ TEST_F(FormatTest, DisableRegions) {
                    "   int   k;"));
 }
 
-TEST_F(FormatTest, DoNotCrashOnInvalidInput) { format("? ) ="); }
+TEST_F(FormatTest, DoNotCrashOnInvalidInput) {
+  format("? ) =");
+  verifyNoCrash("#define a\\\n /**/}");
+}
 
 } // end namespace tooling
 } // end namespace clang