-
Notifications
You must be signed in to change notification settings - Fork 14.2k
[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
Conversation
Fixes llvm#76716 Added a check to prevent null deferencing
@llvm/pr-subscribers-clang-format Author: None (XDeme) ChangesFixes llvm/llvm-project#76716 Full diff: https://github.com/llvm/llvm-project/pull/77045.diff 2 Files Affected:
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"
|
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. |
I think with this the issue might be fixed. |
Please add an assertion in
|
Can you open another pull request to fix the other crash? |
Done as requested in llvm#77045
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.
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.
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.