Skip to content

Commit d8293dd

Browse files
committed
[clang-format] Handle possible crash getCells
Done as requested in llvm#77045
1 parent 093e6bd commit d8293dd

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

clang/lib/Format/WhitespaceManager.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1451,8 +1451,10 @@ WhitespaceManager::CellDescriptions WhitespaceManager::getCells(unsigned Start,
14511451
} else if (C.Tok->is(tok::comma)) {
14521452
if (!Cells.empty())
14531453
Cells.back().EndIndex = i;
1454-
if (C.Tok->getNextNonComment()->isNot(tok::r_brace)) // dangling comma
1454+
if (const auto *Next = C.Tok->getNextNonComment();
1455+
Next && Next->isNot(tok::r_brace)) { // dangling comma
14551456
++Cell;
1457+
}
14561458
}
14571459
} else if (Depth == 1) {
14581460
if (C.Tok == MatchingParen) {

clang/unittests/Format/FormatTest.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20931,6 +20931,11 @@ TEST_F(FormatTest, CatchAlignArrayOfStructuresRightAlignment) {
2093120931
"};",
2093220932
Style);
2093320933

20934+
verifyNoCrash("Foo f[] = {\n"
20935+
" [0] = { 1, },\n"
20936+
" [1] { 1, },\n"
20937+
"};",
20938+
Style);
2093420939
verifyNoCrash("Foo foo[] = {\n"
2093520940
" [0] = {1, 1},\n"
2093620941
" [1] { 1, 1, },\n"
@@ -21179,6 +21184,11 @@ TEST_F(FormatTest, CatchAlignArrayOfStructuresLeftAlignment) {
2117921184
"};",
2118021185
Style);
2118121186

21187+
verifyNoCrash("Foo f[] = {\n"
21188+
" [0] = { 1, },\n"
21189+
" [1] { 1, },\n"
21190+
"};",
21191+
Style);
2118221192
verifyNoCrash("Foo foo[] = {\n"
2118321193
" [0] = {1, 1},\n"
2118421194
" [1] { 1, 1, },\n"

0 commit comments

Comments
 (0)