r239521 - Fix crash in clang-format.

Manuel Klimek klimek at google.com
Thu Jun 11 03:14:14 PDT 2015


Author: klimek
Date: Thu Jun 11 05:14:13 2015
New Revision: 239521

URL: http://llvm.org/viewvc/llvm-project?rev=239521&view=rev
Log:
Fix crash in clang-format.

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.

Modified:
    cfe/trunk/lib/Format/UnwrappedLineFormatter.cpp
    cfe/trunk/unittests/Format/FormatTest.cpp

Modified: cfe/trunk/lib/Format/UnwrappedLineFormatter.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/UnwrappedLineFormatter.cpp?rev=239521&r1=239520&r2=239521&view=diff
==============================================================================
--- cfe/trunk/lib/Format/UnwrappedLineFormatter.cpp (original)
+++ cfe/trunk/lib/Format/UnwrappedLineFormatter.cpp Thu Jun 11 05:14:13 2015
@@ -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);
     }

Modified: cfe/trunk/unittests/Format/FormatTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTest.cpp?rev=239521&r1=239520&r2=239521&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTest.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTest.cpp Thu Jun 11 05:14:13 2015
@@ -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





More information about the cfe-commits mailing list