Skip to content

Commit f6c231c

Browse files
owencatru
authored andcommitted
[clang-format] Fix crashes in AlignArrayOfStructures (#72520)
Fixed #54815. Fixed #55269. Fixed #55493. Fixed #68431.
1 parent f74f3e6 commit f6c231c

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
@@ -1263,6 +1263,8 @@ void WhitespaceManager::alignArrayInitializersRightJustified(
12631263
auto Offset = std::distance(Cells.begin(), CellIter);
12641264
for (const auto *Next = CellIter->NextColumnElement; Next;
12651265
Next = Next->NextColumnElement) {
1266+
if (RowCount >= CellDescs.CellCounts.size())
1267+
break;
12661268
auto *Start = (Cells.begin() + RowCount * CellDescs.CellCounts[0]);
12671269
auto *End = Start + Offset;
12681270
ThisNetWidth = getNetWidth(Start, End, CellDescs.InitialSpaces);
@@ -1324,7 +1326,7 @@ void WhitespaceManager::alignArrayInitializersLeftJustified(
13241326
auto Offset = std::distance(Cells.begin(), CellIter);
13251327
for (const auto *Next = CellIter->NextColumnElement; Next;
13261328
Next = Next->NextColumnElement) {
1327-
if (RowCount > CellDescs.CellCounts.size())
1329+
if (RowCount >= CellDescs.CellCounts.size())
13281330
break;
13291331
auto *Start = (Cells.begin() + RowCount * CellDescs.CellCounts[0]);
13301332
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
@@ -20734,6 +20734,11 @@ TEST_F(FormatTest, CatchExceptionReferenceBinding) {
2073420734
TEST_F(FormatTest, CatchAlignArrayOfStructuresRightAlignment) {
2073520735
auto Style = getLLVMStyle();
2073620736
Style.AlignArrayOfStructures = FormatStyle::AIAS_Right;
20737+
verifyNoCrash("f({\n"
20738+
"table({}, table({{\"\", false}}, {}))\n"
20739+
"});",
20740+
Style);
20741+
2073720742
Style.AlignConsecutiveAssignments.Enabled = true;
2073820743
Style.AlignConsecutiveDeclarations.Enabled = true;
2073920744
verifyFormat("struct test demo[] = {\n"
@@ -21142,6 +21147,33 @@ TEST_F(FormatTest, CatchAlignArrayOfStructuresLeftAlignment) {
2114221147
"that really, in any just world, ought to be split over multiple "
2114321148
"lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};",
2114421149
Style));
21150+
21151+
Style.ColumnLimit = 25;
21152+
verifyNoCrash("Type foo{\n"
21153+
" {\n"
21154+
" 1, // A\n"
21155+
" 2, // B\n"
21156+
" 3, // C\n"
21157+
" },\n"
21158+
" \"hello\",\n"
21159+
"};",
21160+
Style);
21161+
verifyNoCrash("Type object[X][Y] = {\n"
21162+
" {{val}, {val}, {val}},\n"
21163+
" {{val}, {val}, // some comment\n"
21164+
" {val}}\n"
21165+
"};",
21166+
Style);
21167+
21168+
Style.ColumnLimit = 120;
21169+
verifyNoCrash(
21170+
"T v[] {\n"
21171+
" { AAAAAAAAAAAAAAAAAAAAAAAAA::aaaaaaaaaaaaaaaaaaa, "
21172+
"AAAAAAAAAAAAAAAAAAAAAAAAA::aaaaaaaaaaaaaaaaaaaaaaaa, 1, 0.000000000f, "
21173+
"\"00000000000000000000000000000000000000000000000000000000"
21174+
"00000000000000000000000000000000000000000000000000000000\" },\n"
21175+
"};",
21176+
Style);
2114521177
}
2114621178

2114721179
TEST_F(FormatTest, UnderstandsPragmas) {

0 commit comments

Comments
 (0)