]> granicus.if.org Git - clang/commitdiff
clang-format: Prevent assertion discovered by fuzzer.
authorDaniel Jasper <djasper@google.com>
Wed, 6 May 2015 08:58:57 +0000 (08:58 +0000)
committerDaniel Jasper <djasper@google.com>
Wed, 6 May 2015 08:58:57 +0000 (08:58 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@236578 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Format/TokenAnnotator.cpp
unittests/Format/FormatTest.cpp

index 39ac66b27fab7461c5f01ebfe3f31696c8ac059f..fa12d4dd7ab6012548233709dcd1f3d58cb92bab 100644 (file)
@@ -895,8 +895,16 @@ private:
                (!Current.Previous || Current.Previous->isNot(tok::l_square))) {
       Current.Type = TT_BinaryOperator;
     } else if (Current.is(tok::comment)) {
-      Current.Type =
-          Current.TokenText.startswith("/*") ? TT_BlockComment : TT_LineComment;
+      if (Current.TokenText.startswith("/*")) {
+        if (Current.TokenText.endswith("*/"))
+          Current.Type = TT_BlockComment;
+        else
+          // The lexer has for some reason determined a comment here. But we
+          // cannot really handle it, if it isn't properly terminated.
+          Current.Tok.setKind(tok::unknown);
+      } else {
+        Current.Type = TT_LineComment;
+      }
     } else if (Current.is(tok::r_paren)) {
       if (rParenEndsCast(Current))
         Current.Type = TT_CastRParen;
index 762f92c042cd9d21f72848f3a4e8c04cb199ed68..31befad678337efde802548ee935f792280e408e 100644 (file)
@@ -1056,6 +1056,8 @@ TEST_F(FormatTest, UnderstandsSingleLineComments) {
 
   verifyNoCrash("/\\\n/");
   verifyNoCrash("/\\\n* */");
+  // The 0-character somehow makes the lexer return a proper comment.
+  verifyNoCrash(StringRef("/*\\\0\n/", 6));
 }
 
 TEST_F(FormatTest, KeepsParameterWithTrailingCommentsOnTheirOwnLine) {