Skip to content

Commit d9cbbe4

Browse files
committed
[clang-format] Fix crash involving array designators and dangling comma
Fixes #76716 Added a check to prevent null deferencing
1 parent 0414cf0 commit d9cbbe4

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

clang/lib/Format/WhitespaceManager.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1444,7 +1444,8 @@ WhitespaceManager::CellDescriptions WhitespaceManager::getCells(unsigned Start,
14441444
} else if (C.Tok->is(tok::comma)) {
14451445
if (!Cells.empty())
14461446
Cells.back().EndIndex = i;
1447-
if (C.Tok->getNextNonComment()->isNot(tok::r_brace)) // dangling comma
1447+
const FormatToken *Next = C.Tok->getNextNonComment();
1448+
if (Next && Next->isNot(tok::r_brace)) // dangling comma
14481449
++Cell;
14491450
}
14501451
} else if (Depth == 1) {

clang/unittests/Format/FormatTest.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21084,6 +21084,12 @@ TEST_F(FormatTest, CatchAlignArrayOfStructuresLeftAlignment) {
2108421084
"};",
2108521085
Style);
2108621086

21087+
verifyNoCrash("Foo f[] = {\n"
21088+
" [0] = { 1, },\n"
21089+
" [1] { 1, },\n"
21090+
"};",
21091+
Style);
21092+
2108721093
verifyFormat("return GradForUnaryCwise(g, {\n"
2108821094
" {{\"sign\"}, \"Sign\", {\"x\", "
2108921095
"\"dy\"} },\n"

0 commit comments

Comments
 (0)