Skip to content

[clang-format] Fix crash involving array designators #77045

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Jan 11, 2024
Merged

Conversation

XDeme1
Copy link
Contributor

@XDeme1 XDeme1 commented Jan 5, 2024

Fixes #76716
Fixes parsing of [0]{}. Before this patch it was begin parsed as a lambda, now it is correctly parsed as a designator initializer.

@llvmbot
Copy link
Member

llvmbot commented Jan 5, 2024

@llvm/pr-subscribers-clang-format

Author: None (XDeme)

Changes

Fixes llvm/llvm-project#76716
Added a check to prevent null deferencing


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

2 Files Affected:

  • (modified) clang/lib/Format/WhitespaceManager.cpp (+2-1)
  • (modified) clang/unittests/Format/FormatTest.cpp (+6)
diff --git a/clang/lib/Format/WhitespaceManager.cpp b/clang/lib/Format/WhitespaceManager.cpp
index 3bc6915b8df0a7..95693f4588c631 100644
--- a/clang/lib/Format/WhitespaceManager.cpp
+++ b/clang/lib/Format/WhitespaceManager.cpp
@@ -1444,7 +1444,8 @@ 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
+        const FormatToken *Next = C.Tok->getNextNonComment();
+        if (Next && Next->isNot(tok::r_brace)) // dangling comma
           ++Cell;
       }
     } else if (Depth == 1) {
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index 881993ede17c3d..c9f91953c13f52 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -21084,6 +21084,12 @@ TEST_F(FormatTest, CatchAlignArrayOfStructuresLeftAlignment) {
       "};",
       Style);
 
+  verifyNoCrash("Foo f[] = {\n"
+                "    [0] = { 1, },\n"
+                "    [1] { 1, },\n"
+                "};",
+                Style);
+
   verifyFormat("return GradForUnaryCwise(g, {\n"
                "                                {{\"sign\"}, \"Sign\", {\"x\", "
                "\"dy\"}   },\n"

@XDeme1
Copy link
Contributor Author

XDeme1 commented Jan 5, 2024

While I was trying to find a minimal reproducer to the bug, I accidentally found that this patch fix another crash, and doesn't fix the linked issue.

@XDeme1
Copy link
Contributor Author

XDeme1 commented Jan 5, 2024

I think with this the issue might be fixed.

@XDeme1 XDeme1 changed the title [clang-format] Fix crash involving array designators and dangling comma [clang-format] Fix crash involving array designators Jan 9, 2024
@HazardyKnusperkeks HazardyKnusperkeks self-requested a review January 9, 2024 20:09
@owenca
Copy link
Contributor

owenca commented Jan 10, 2024

Please add an assertion in WhitespaceManager.h:

--- a/clang/lib/Format/WhitespaceManager.h
+++ b/clang/lib/Format/WhitespaceManager.h
@@ -282,6 +282,7 @@ private:
     for (auto PrevIter = Start; PrevIter != End; ++PrevIter) {
       // If we broke the line the initial spaces are already
       // accounted for.
+      assert(PrevIter->Index) < Changes.size();
       if (Changes[PrevIter->Index].NewlinesBefore > 0)
         NetWidth = 0;
       NetWidth +=

@owenca
Copy link
Contributor

owenca commented Jan 11, 2024

While I was trying to find a minimal reproducer to the bug, I accidentally found that this patch fix another crash, and doesn't fix the linked issue.

Can you open another pull request to fix the other crash?

@owenca owenca merged commit 093e6bd into llvm:main Jan 11, 2024
XDeme1 added a commit to XDeme1/llvm-project that referenced this pull request Jan 11, 2024
@XDeme1 XDeme1 deleted the 76716 branch January 11, 2024 04:09
@XDeme1
Copy link
Contributor Author

XDeme1 commented Jan 11, 2024

While I was trying to find a minimal reproducer to the bug, I accidentally found that this patch fix another crash, and doesn't fix the linked issue.

Can you open another pull request to fix the other crash?

@owenca Just created #77723

owenca pushed a commit that referenced this pull request Jan 12, 2024
Done as requested in #77045

I have changed the test a bit, because since the root problem was fixed,
the original test would possibly never crash.
justinfargnoli pushed a commit to justinfargnoli/llvm-project that referenced this pull request Jan 28, 2024
Fixes llvm#76716
Fixes parsing of `[0]{}`. Before this patch it was begin parsed as a
lambda, now it is correctly parsed as a designator initializer.
justinfargnoli pushed a commit to justinfargnoli/llvm-project that referenced this pull request Jan 28, 2024
Done as requested in llvm#77045

I have changed the test a bit, because since the root problem was fixed,
the original test would possibly never crash.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

clang-format fails with assertion "`idx < size()' failed"
5 participants