[PATCH] D110481: fixes bug #51926 where dangling comma caused overrun

Fred Grim via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Sat Sep 25 09:14:35 PDT 2021


feg208 created this revision.
feg208 added reviewers: HazardyKnusperkeks, MyDeveloperDay, curdeius, klimek, djasper, tinloaf.
feg208 requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

bug 51926 identified an issue where a dangling comma caused the cell count to be to off by one


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D110481

Files:
  clang/lib/Format/WhitespaceManager.cpp
  clang/unittests/Format/FormatTest.cpp


Index: clang/unittests/Format/FormatTest.cpp
===================================================================
--- clang/unittests/Format/FormatTest.cpp
+++ clang/unittests/Format/FormatTest.cpp
@@ -17800,6 +17800,12 @@
 TEST_F(FormatTest, CatchAlignArrayOfStructuresLeftAlignment) {
   auto Style = getLLVMStyle();
   Style.AlignArrayOfStructures = FormatStyle::AIAS_Left;
+  verifyFormat("auto foo = Items{\n"
+               "    Section{\n"
+               "            0, bar(),\n"
+               "            }\n"
+               "};\n",
+               Style);
   verifyFormat("struct test demo[] = {\n"
                "    {56, 23,    \"hello\"},\n"
                "    {-1, 93463, \"world\"},\n"
Index: clang/lib/Format/WhitespaceManager.cpp
===================================================================
--- clang/lib/Format/WhitespaceManager.cpp
+++ clang/lib/Format/WhitespaceManager.cpp
@@ -1146,14 +1146,15 @@
       } else if (C.Tok->is(tok::comma)) {
         if (!Cells.empty())
           Cells.back().EndIndex = i;
-        Cell++;
+        if (C.Tok->Next->isNot(tok::r_brace))
+          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});
-        CellCount = Cell + 1;
+        CellCount = C.Tok->Previous->isNot(tok::comma) ? Cell + 1 : Cell;
         // Go to the next non-comment and ensure there is a break in front
         const auto *NextNonComment = C.Tok->getNextNonComment();
         while (NextNonComment->is(tok::comma))


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D110481.375042.patch
Type: text/x-patch
Size: 1643 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20210925/fd0ae947/attachment-0001.bin>


More information about the cfe-commits mailing list