[clang] 8c3fc44 - [clang-format] Fix crash on malformed operator input (#199098)
via cfe-commits
cfe-commits at lists.llvm.org
Thu Jun 18 12:46:05 PDT 2026
Author: mygitljf
Date: 2026-06-18T21:46:00+02:00
New Revision: 8c3fc449d742055dc3a596b0cce0988bd199b1d9
URL: https://github.com/llvm/llvm-project/commit/8c3fc449d742055dc3a596b0cce0988bd199b1d9
DIFF: https://github.com/llvm/llvm-project/commit/8c3fc449d742055dc3a596b0cce0988bd199b1d9.diff
LOG: [clang-format] Fix crash on malformed operator input (#199098)
fixes the remaining clang-format crash case after #199100 landed.
The problematic input is:
```cpp
{ operator } a
```
When annotating operator, clang-format should stop scanning at } instead
of consuming it and disturbing brace scope tracking. And adds a no-crash
regression test for it.
Added:
Modified:
clang/lib/Format/TokenAnnotator.cpp
clang/unittests/Format/FormatTest.cpp
Removed:
################################################################################
diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp
index 208c79082d83a..634143d843c3b 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -1691,7 +1691,8 @@ class AnnotatingParser {
}
}
while (CurrentToken &&
- CurrentToken->isNoneOf(tok::l_paren, tok::semi, tok::r_paren)) {
+ CurrentToken->isNoneOf(tok::l_paren, tok::semi, tok::r_paren,
+ tok::r_brace)) {
if (CurrentToken->isOneOf(tok::star, tok::amp))
CurrentToken->setType(TT_PointerOrReference);
auto Next = CurrentToken->getNextNonComment();
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index 50aef6b02babc..f85422dbf4f42 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -14394,6 +14394,7 @@ TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) {
verifyNoCrash("struct Foo {\n"
" operator foo(bar\n"
"};");
+ verifyNoCrash("{ operator } a");
verifyNoCrash("decltype( {\n"
" {");
}
More information about the cfe-commits
mailing list