Skip to content

Commit 2e7cacf

Browse files
rymielowenca
andauthored
[clang-format] Fix crash in TokenAnnotator (#82349)
The while loop on line 3814 can cause a segmentation fault getting the Next field on a nullptr. This is because further down, on line 3823, there is another for loop, which assigns Tok to Tok->Next in its initializer. This for loop has a condition to check if the result of that isn't null. If it is, the loop is skipped and we drop back out to the outer loop, except, now Tok is null, and we try to dereference it without checking first. This patch adds a defensive check that returns if Tok->Next is null before we make it to the second for loop. Fixes #82328 --------- Co-authored-by: Owen Pan <[email protected]>
1 parent 54a6cf1 commit 2e7cacf

File tree

2 files changed

+7
-1
lines changed

2 files changed

+7
-1
lines changed

clang/lib/Format/TokenAnnotator.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3817,7 +3817,7 @@ void TokenAnnotator::calculateFormattingInformation(AnnotatedLine &Line) const {
38173817
do {
38183818
Tok = Tok->Next;
38193819
} while (Tok && Tok->isNot(TT_OverloadedOperatorLParen));
3820-
if (!Tok)
3820+
if (!Tok || !Tok->MatchingParen)
38213821
break;
38223822
const auto *LeftParen = Tok;
38233823
for (Tok = Tok->Next; Tok && Tok != LeftParen->MatchingParen;

clang/unittests/Format/FormatTest.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13503,6 +13503,12 @@ TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) {
1350313503
verifyFormat("{");
1350413504
verifyFormat("#})");
1350513505
verifyNoCrash("(/**/[:!] ?[).");
13506+
verifyNoCrash("struct X {\n"
13507+
" operator iunt(\n"
13508+
"};");
13509+
verifyNoCrash("struct Foo {\n"
13510+
" operator foo(bar\n"
13511+
"};");
1350613512
}
1350713513

1350813514
TEST_F(FormatTest, IncorrectUnbalancedBracesInMacrosWithUnicode) {

0 commit comments

Comments
 (0)