[clang] [clang-format] Fix crash involving array designators and dangling comma (PR #77045)

via cfe-commits cfe-commits at lists.llvm.org
Mon Jan 8 20:38:30 PST 2024


================
@@ -1444,16 +1444,26 @@ WhitespaceManager::CellDescriptions WhitespaceManager::getCells(unsigned Start,
       } else if (C.Tok->is(tok::comma)) {
         if (!Cells.empty())
           Cells.back().EndIndex = i;
-        if (C.Tok->getNextNonComment()->isNot(tok::r_brace)) // dangling comma
+
+        if (const auto *Next = C.Tok->getNextNonComment();
+            Next && Next->isNot(tok::r_brace)) { // Dangling comma.
           ++Cell;
+        }
       }
     } else if (Depth == 1) {
       if (C.Tok == MatchingParen) {
         if (!Cells.empty())
           Cells.back().EndIndex = i;
         Cells.push_back(CellDescription{i, ++Cell, i + 1, false, nullptr});
-        CellCounts.push_back(C.Tok->Previous->isNot(tok::comma) ? Cell + 1
-                                                                : Cell);
+        CellCounts.push_back(
+            C.Tok->Previous->isNot(tok::comma) &&
+                    // When dealing with C array designators. There is a
+                    // possibility of some nested array not having an `=`.
+                    // When this happens we make the cells non rectangular,
+                    // avoiding an access out of bound later on.
+                    MatchingParen->MatchingParen->Previous->isNot(tok::equal)
----------------
XDeme wrote:

I've investigated a bit more, and I found that we were actually parsing
`[0] {};` as a lambda, instead of a designated initializer

https://github.com/llvm/llvm-project/pull/77045


More information about the cfe-commits mailing list