Skip to content

Commit 5679f55

Browse files
authored
[clang-format] Fix crashes in AlignArrayOfStructures (#72520)
Fixed #54815. Fixed #55269. Fixed #55493. Fixed #68431.
1 parent 4323da9 commit 5679f55

File tree

3 files changed

+36
-2
lines changed

3 files changed

+36
-2
lines changed

clang/lib/Format/WhitespaceManager.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1316,6 +1316,8 @@ void WhitespaceManager::alignArrayInitializersRightJustified(
13161316
auto Offset = std::distance(Cells.begin(), CellIter);
13171317
for (const auto *Next = CellIter->NextColumnElement; Next;
13181318
Next = Next->NextColumnElement) {
1319+
if (RowCount >= CellDescs.CellCounts.size())
1320+
break;
13191321
auto *Start = (Cells.begin() + RowCount * CellDescs.CellCounts[0]);
13201322
auto *End = Start + Offset;
13211323
ThisNetWidth = getNetWidth(Start, End, CellDescs.InitialSpaces);
@@ -1379,7 +1381,7 @@ void WhitespaceManager::alignArrayInitializersLeftJustified(
13791381
auto Offset = std::distance(Cells.begin(), CellIter);
13801382
for (const auto *Next = CellIter->NextColumnElement; Next;
13811383
Next = Next->NextColumnElement) {
1382-
if (RowCount > CellDescs.CellCounts.size())
1384+
if (RowCount >= CellDescs.CellCounts.size())
13831385
break;
13841386
auto *Start = (Cells.begin() + RowCount * CellDescs.CellCounts[0]);
13851387
auto *End = Start + Offset;

clang/lib/Format/WhitespaceManager.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ class WhitespaceManager {
317317
auto Offset = std::distance(CellStart, CellStop);
318318
for (const auto *Next = CellStop->NextColumnElement; Next;
319319
Next = Next->NextColumnElement) {
320-
if (RowCount > MaxRowCount)
320+
if (RowCount >= MaxRowCount)
321321
break;
322322
auto Start = (CellStart + RowCount * CellCount);
323323
auto End = Start + Offset;

clang/unittests/Format/FormatTest.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20709,6 +20709,11 @@ TEST_F(FormatTest, CatchExceptionReferenceBinding) {
2070920709
TEST_F(FormatTest, CatchAlignArrayOfStructuresRightAlignment) {
2071020710
auto Style = getLLVMStyle();
2071120711
Style.AlignArrayOfStructures = FormatStyle::AIAS_Right;
20712+
verifyNoCrash("f({\n"
20713+
"table({}, table({{\"\", false}}, {}))\n"
20714+
"});",
20715+
Style);
20716+
2071220717
Style.AlignConsecutiveAssignments.Enabled = true;
2071320718
Style.AlignConsecutiveDeclarations.Enabled = true;
2071420719
verifyFormat("struct test demo[] = {\n"
@@ -21140,6 +21145,33 @@ TEST_F(FormatTest, CatchAlignArrayOfStructuresLeftAlignment) {
2114021145
"that really, in any just world, ought to be split over multiple "
2114121146
"lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};",
2114221147
Style);
21148+
21149+
Style.ColumnLimit = 25;
21150+
verifyNoCrash("Type foo{\n"
21151+
" {\n"
21152+
" 1, // A\n"
21153+
" 2, // B\n"
21154+
" 3, // C\n"
21155+
" },\n"
21156+
" \"hello\",\n"
21157+
"};",
21158+
Style);
21159+
verifyNoCrash("Type object[X][Y] = {\n"
21160+
" {{val}, {val}, {val}},\n"
21161+
" {{val}, {val}, // some comment\n"
21162+
" {val}}\n"
21163+
"};",
21164+
Style);
21165+
21166+
Style.ColumnLimit = 120;
21167+
verifyNoCrash(
21168+
"T v[] {\n"
21169+
" { AAAAAAAAAAAAAAAAAAAAAAAAA::aaaaaaaaaaaaaaaaaaa, "
21170+
"AAAAAAAAAAAAAAAAAAAAAAAAA::aaaaaaaaaaaaaaaaaaaaaaaa, 1, 0.000000000f, "
21171+
"\"00000000000000000000000000000000000000000000000000000000"
21172+
"00000000000000000000000000000000000000000000000000000000\" },\n"
21173+
"};",
21174+
Style);
2114321175
}
2114421176

2114521177
TEST_F(FormatTest, UnderstandsPragmas) {

0 commit comments

Comments
 (0)