Skip to content

Commit

Permalink
[clang-format] Handle possible crash in getCells (llvm#77723)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
XDeme1 authored and justinfargnoli committed Jan 28, 2024
1 parent 19aca43 commit b7b90d9
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
4 changes: 3 additions & 1 deletion clang/lib/Format/WhitespaceManager.cpp
Expand Up @@ -1451,8 +1451,10 @@ 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
if (const auto *Next = C.Tok->getNextNonComment();
Next && Next->isNot(tok::r_brace)) { // dangling comma
++Cell;
}
}
} else if (Depth == 1) {
if (C.Tok == MatchingParen) {
Expand Down
10 changes: 10 additions & 0 deletions clang/unittests/Format/FormatTest.cpp
Expand Up @@ -20931,6 +20931,11 @@ TEST_F(FormatTest, CatchAlignArrayOfStructuresRightAlignment) {
"};",
Style);

verifyNoCrash("Foo f[] = {\n"
" [0] = { 1, },\n"
" [i] { 1, },\n"
"};",
Style);
verifyNoCrash("Foo foo[] = {\n"
" [0] = {1, 1},\n"
" [1] { 1, 1, },\n"
Expand Down Expand Up @@ -21179,6 +21184,11 @@ TEST_F(FormatTest, CatchAlignArrayOfStructuresLeftAlignment) {
"};",
Style);

verifyNoCrash("Foo f[] = {\n"
" [0] = { 1, },\n"
" [i] { 1, },\n"
"};",
Style);
verifyNoCrash("Foo foo[] = {\n"
" [0] = {1, 1},\n"
" [1] { 1, 1, },\n"
Expand Down

0 comments on commit b7b90d9

Please sign in to comment.