[clang] 97a9dbb - [clang-format] Handle possible crash in `getCells` (#77723)
via cfe-commits
cfe-commits at lists.llvm.org
Fri Jan 12 10:20:49 PST 2024
Author: XDeme
Date: 2024-01-12T10:20:44-08:00
New Revision: 97a9dbb64919fe71379cda5f043633cbeb6438e5
URL: https://github.com/llvm/llvm-project/commit/97a9dbb64919fe71379cda5f043633cbeb6438e5
DIFF: https://github.com/llvm/llvm-project/commit/97a9dbb64919fe71379cda5f043633cbeb6438e5.diff
LOG: [clang-format] Handle possible crash in `getCells` (#77723)
Done as requested in llvm/llvm-project#77045
I have changed the test a bit, because since the root problem was fixed,
the original test would possibly never crash.
Added:
Modified:
clang/lib/Format/WhitespaceManager.cpp
clang/unittests/Format/FormatTest.cpp
Removed:
################################################################################
diff --git a/clang/lib/Format/WhitespaceManager.cpp b/clang/lib/Format/WhitespaceManager.cpp
index f1d176f182ffa4..8415c8d360d640 100644
--- a/clang/lib/Format/WhitespaceManager.cpp
+++ b/clang/lib/Format/WhitespaceManager.cpp
@@ -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) {
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index 8f115fb8cbf0fb..340ae39cb22b03 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -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"
@@ -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"
More information about the cfe-commits
mailing list