[clang] [clang-format] Fix crash involving array designators and dangling comma (PR #77045)
via cfe-commits
cfe-commits at lists.llvm.org
Thu Jan 4 20:30:34 PST 2024
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang-format
Author: None (XDeme)
<details>
<summary>Changes</summary>
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
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"
``````````
</details>
https://github.com/llvm/llvm-project/pull/77045
More information about the cfe-commits
mailing list