]> granicus.if.org Git - clang/commitdiff
[clang-format] fix crash involving invalid preprocessor line
authorKrasimir Georgiev <krasimir@google.com>
Thu, 8 Aug 2019 11:56:18 +0000 (11:56 +0000)
committerKrasimir Georgiev <krasimir@google.com>
Thu, 8 Aug 2019 11:56:18 +0000 (11:56 +0000)
Summary:
This (invalid) fragment is crashing clang-format:
```
#if 1
int x;
#elif
int y;
#endif
```

The reason being that the parser expects a token after `#elif`, and the
subsequent parsing of the next line does not check if `CurrentToken` is null.

Reviewers: gribozavr

Reviewed By: gribozavr

Subscribers: cfe-commits

Tags: #clang

Differential Revision: https://reviews.llvm.org/D65940

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@368280 91177308-0d34-0410-b5e6-96231b3b80d8

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

index 6e0369f27e7ecbc9b648eb459a2e6b899ad15a11..cc0a954dbfb24c4735ef281013801fc4cc93e95d 100644 (file)
@@ -1099,6 +1099,8 @@ private:
 
 public:
   LineType parseLine() {
+    if (!CurrentToken)
+      return LT_Invalid;
     NonTemplateLess.clear();
     if (CurrentToken->is(tok::hash))
       return parsePreprocessorDirective();
index 528ad4b78c06255f09024587a5235263a1214ffa..4e1b6f22bc3159fd806f5c5164aa47560c1e0da0 100644 (file)
@@ -3087,6 +3087,15 @@ TEST_F(FormatTest, IndentPreprocessorDirectives) {
                "#endif\n"
                "#endif\n",
                Style);
+  // Don't crash if there is an #elif directive without a condition.
+  verifyFormat("#if 1\n"
+               "int x;\n"
+               "#elif\n"
+               "int y;\n"
+               "#else\n"
+               "int z;\n"
+               "#endif",
+               Style);
   // FIXME: This doesn't handle the case where there's code between the
   // #ifndef and #define but all other conditions hold. This is because when
   // the #define line is parsed, UnwrappedLineParser::Lines doesn't hold the