[clang] [clang-format] Fix crash in TokenAnnotator (PR #82349)

2024-02-22 Thread Emilia Kond via cfe-commits

https://github.com/rymiel closed https://github.com/llvm/llvm-project/pull/82349
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-format] Fix crash in TokenAnnotator (PR #82349)

2024-02-22 Thread Owen Pan via cfe-commits

https://github.com/owenca approved this pull request.


https://github.com/llvm/llvm-project/pull/82349
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-format] Fix crash in TokenAnnotator (PR #82349)

2024-02-22 Thread Emilia Kond via cfe-commits

https://github.com/rymiel updated 
https://github.com/llvm/llvm-project/pull/82349

>From 25ca98534f0d5dbd96ff74c802528171787ced45 Mon Sep 17 00:00:00 2001
From: Emilia Kond 
Date: Tue, 20 Feb 2024 14:11:33 +0200
Subject: [PATCH 1/4] [clang-format] Fix crash in TokenAnnotator

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 https://github.com/llvm/llvm-project/issues/82328
---
 clang/lib/Format/TokenAnnotator.cpp   | 2 +-
 clang/unittests/Format/FormatTest.cpp | 3 +++
 2 files changed, 4 insertions(+), 1 deletion(-)

diff --git a/clang/lib/Format/TokenAnnotator.cpp 
b/clang/lib/Format/TokenAnnotator.cpp
index ec7b7f4dbe3470..a38ba8762f8505 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -3817,7 +3817,7 @@ void 
TokenAnnotator::calculateFormattingInformation(AnnotatedLine ) const {
 do {
   Tok = Tok->Next;
 } while (Tok && Tok->isNot(TT_OverloadedOperatorLParen));
-if (!Tok)
+if (!Tok || !Tok->Next)
   break;
 const auto *LeftParen = Tok;
 for (Tok = Tok->Next; Tok && Tok != LeftParen->MatchingParen;
diff --git a/clang/unittests/Format/FormatTest.cpp 
b/clang/unittests/Format/FormatTest.cpp
index 24f62af8ddcb87..3a3c88f609af7a 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -13503,6 +13503,9 @@ TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) {
   verifyFormat("{");
   verifyFormat("#})");
   verifyNoCrash("(/**/[:!] ?[).");
+  verifyNoCrash("struct X{"
+"  operator iunt("
+"};");
 }
 
 TEST_F(FormatTest, IncorrectUnbalancedBracesInMacrosWithUnicode) {

>From e5f5e64a5765d23c5b88e05d4f9b6d46bbbe5abd Mon Sep 17 00:00:00 2001
From: Emilia Kond 
Date: Thu, 22 Feb 2024 14:55:16 +0200
Subject: [PATCH 2/4] Apply suggestions from code review

Co-authored-by: Owen Pan 
---
 clang/lib/Format/TokenAnnotator.cpp   | 2 +-
 clang/unittests/Format/FormatTest.cpp | 4 ++--
 2 files changed, 3 insertions(+), 3 deletions(-)

diff --git a/clang/lib/Format/TokenAnnotator.cpp 
b/clang/lib/Format/TokenAnnotator.cpp
index a38ba8762f8505..a60d6ae197a24e 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -3817,7 +3817,7 @@ void 
TokenAnnotator::calculateFormattingInformation(AnnotatedLine ) const {
 do {
   Tok = Tok->Next;
 } while (Tok && Tok->isNot(TT_OverloadedOperatorLParen));
-if (!Tok || !Tok->Next)
+if (!Tok || !Tok->MatchingParen)
   break;
 const auto *LeftParen = Tok;
 for (Tok = Tok->Next; Tok && Tok != LeftParen->MatchingParen;
diff --git a/clang/unittests/Format/FormatTest.cpp 
b/clang/unittests/Format/FormatTest.cpp
index 3a3c88f609af7a..863d9c80d7ee6c 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -13503,8 +13503,8 @@ TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) {
   verifyFormat("{");
   verifyFormat("#})");
   verifyNoCrash("(/**/[:!] ?[).");
-  verifyNoCrash("struct X{"
-"  operator iunt("
+  verifyNoCrash("struct X {\n"
+"  operator iunt(\n"
 "};");
 }
 

>From 24eb4e02db50acfd2990ac8bce0aed36093319d7 Mon Sep 17 00:00:00 2001
From: Emilia Kond 
Date: Thu, 22 Feb 2024 14:56:00 +0200
Subject: [PATCH 3/4] Additional test

---
 clang/unittests/Format/FormatTest.cpp | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/clang/unittests/Format/FormatTest.cpp 
b/clang/unittests/Format/FormatTest.cpp
index 863d9c80d7ee6c..ee6eb6ff5cc66c 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -13506,6 +13506,9 @@ TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) {
   verifyNoCrash("struct X {\n"
 "  operator iunt(\n"
 "};");
+  verifyNoCrash("struct Foo {\n"
+"operator foo(bar\n"
+"};");
 }
 
 TEST_F(FormatTest, IncorrectUnbalancedBracesInMacrosWithUnicode) {

>From 0d542c133362768e9231de577f91738ce362cce5 Mon Sep 17 00:00:00 2001
From: Emilia Kond 
Date: Thu, 22 Feb 2024 14:57:23 +0200
Subject: [PATCH 4/4] spacing

---
 clang/unittests/Format/FormatTest.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/clang/unittests/Format/FormatTest.cpp 
b/clang/unittests/Format/FormatTest.cpp
index ee6eb6ff5cc66c..70859ec4b0141d 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ 

[clang] [clang-format] Fix crash in TokenAnnotator (PR #82349)

2024-02-22 Thread Emilia Kond via cfe-commits

https://github.com/rymiel updated 
https://github.com/llvm/llvm-project/pull/82349

>From 25ca98534f0d5dbd96ff74c802528171787ced45 Mon Sep 17 00:00:00 2001
From: Emilia Kond 
Date: Tue, 20 Feb 2024 14:11:33 +0200
Subject: [PATCH 1/3] [clang-format] Fix crash in TokenAnnotator

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 https://github.com/llvm/llvm-project/issues/82328
---
 clang/lib/Format/TokenAnnotator.cpp   | 2 +-
 clang/unittests/Format/FormatTest.cpp | 3 +++
 2 files changed, 4 insertions(+), 1 deletion(-)

diff --git a/clang/lib/Format/TokenAnnotator.cpp 
b/clang/lib/Format/TokenAnnotator.cpp
index ec7b7f4dbe3470..a38ba8762f8505 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -3817,7 +3817,7 @@ void 
TokenAnnotator::calculateFormattingInformation(AnnotatedLine ) const {
 do {
   Tok = Tok->Next;
 } while (Tok && Tok->isNot(TT_OverloadedOperatorLParen));
-if (!Tok)
+if (!Tok || !Tok->Next)
   break;
 const auto *LeftParen = Tok;
 for (Tok = Tok->Next; Tok && Tok != LeftParen->MatchingParen;
diff --git a/clang/unittests/Format/FormatTest.cpp 
b/clang/unittests/Format/FormatTest.cpp
index 24f62af8ddcb87..3a3c88f609af7a 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -13503,6 +13503,9 @@ TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) {
   verifyFormat("{");
   verifyFormat("#})");
   verifyNoCrash("(/**/[:!] ?[).");
+  verifyNoCrash("struct X{"
+"  operator iunt("
+"};");
 }
 
 TEST_F(FormatTest, IncorrectUnbalancedBracesInMacrosWithUnicode) {

>From e5f5e64a5765d23c5b88e05d4f9b6d46bbbe5abd Mon Sep 17 00:00:00 2001
From: Emilia Kond 
Date: Thu, 22 Feb 2024 14:55:16 +0200
Subject: [PATCH 2/3] Apply suggestions from code review

Co-authored-by: Owen Pan 
---
 clang/lib/Format/TokenAnnotator.cpp   | 2 +-
 clang/unittests/Format/FormatTest.cpp | 4 ++--
 2 files changed, 3 insertions(+), 3 deletions(-)

diff --git a/clang/lib/Format/TokenAnnotator.cpp 
b/clang/lib/Format/TokenAnnotator.cpp
index a38ba8762f8505..a60d6ae197a24e 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -3817,7 +3817,7 @@ void 
TokenAnnotator::calculateFormattingInformation(AnnotatedLine ) const {
 do {
   Tok = Tok->Next;
 } while (Tok && Tok->isNot(TT_OverloadedOperatorLParen));
-if (!Tok || !Tok->Next)
+if (!Tok || !Tok->MatchingParen)
   break;
 const auto *LeftParen = Tok;
 for (Tok = Tok->Next; Tok && Tok != LeftParen->MatchingParen;
diff --git a/clang/unittests/Format/FormatTest.cpp 
b/clang/unittests/Format/FormatTest.cpp
index 3a3c88f609af7a..863d9c80d7ee6c 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -13503,8 +13503,8 @@ TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) {
   verifyFormat("{");
   verifyFormat("#})");
   verifyNoCrash("(/**/[:!] ?[).");
-  verifyNoCrash("struct X{"
-"  operator iunt("
+  verifyNoCrash("struct X {\n"
+"  operator iunt(\n"
 "};");
 }
 

>From 24eb4e02db50acfd2990ac8bce0aed36093319d7 Mon Sep 17 00:00:00 2001
From: Emilia Kond 
Date: Thu, 22 Feb 2024 14:56:00 +0200
Subject: [PATCH 3/3] Additional test

---
 clang/unittests/Format/FormatTest.cpp | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/clang/unittests/Format/FormatTest.cpp 
b/clang/unittests/Format/FormatTest.cpp
index 863d9c80d7ee6c..ee6eb6ff5cc66c 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -13506,6 +13506,9 @@ TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) {
   verifyNoCrash("struct X {\n"
 "  operator iunt(\n"
 "};");
+  verifyNoCrash("struct Foo {\n"
+"operator foo(bar\n"
+"};");
 }
 
 TEST_F(FormatTest, IncorrectUnbalancedBracesInMacrosWithUnicode) {

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-format] Fix crash in TokenAnnotator (PR #82349)

2024-02-22 Thread Emilia Kond via cfe-commits

https://github.com/rymiel updated 
https://github.com/llvm/llvm-project/pull/82349

>From 25ca98534f0d5dbd96ff74c802528171787ced45 Mon Sep 17 00:00:00 2001
From: Emilia Kond 
Date: Tue, 20 Feb 2024 14:11:33 +0200
Subject: [PATCH 1/2] [clang-format] Fix crash in TokenAnnotator

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 https://github.com/llvm/llvm-project/issues/82328
---
 clang/lib/Format/TokenAnnotator.cpp   | 2 +-
 clang/unittests/Format/FormatTest.cpp | 3 +++
 2 files changed, 4 insertions(+), 1 deletion(-)

diff --git a/clang/lib/Format/TokenAnnotator.cpp 
b/clang/lib/Format/TokenAnnotator.cpp
index ec7b7f4dbe3470..a38ba8762f8505 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -3817,7 +3817,7 @@ void 
TokenAnnotator::calculateFormattingInformation(AnnotatedLine ) const {
 do {
   Tok = Tok->Next;
 } while (Tok && Tok->isNot(TT_OverloadedOperatorLParen));
-if (!Tok)
+if (!Tok || !Tok->Next)
   break;
 const auto *LeftParen = Tok;
 for (Tok = Tok->Next; Tok && Tok != LeftParen->MatchingParen;
diff --git a/clang/unittests/Format/FormatTest.cpp 
b/clang/unittests/Format/FormatTest.cpp
index 24f62af8ddcb87..3a3c88f609af7a 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -13503,6 +13503,9 @@ TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) {
   verifyFormat("{");
   verifyFormat("#})");
   verifyNoCrash("(/**/[:!] ?[).");
+  verifyNoCrash("struct X{"
+"  operator iunt("
+"};");
 }
 
 TEST_F(FormatTest, IncorrectUnbalancedBracesInMacrosWithUnicode) {

>From e5f5e64a5765d23c5b88e05d4f9b6d46bbbe5abd Mon Sep 17 00:00:00 2001
From: Emilia Kond 
Date: Thu, 22 Feb 2024 14:55:16 +0200
Subject: [PATCH 2/2] Apply suggestions from code review

Co-authored-by: Owen Pan 
---
 clang/lib/Format/TokenAnnotator.cpp   | 2 +-
 clang/unittests/Format/FormatTest.cpp | 4 ++--
 2 files changed, 3 insertions(+), 3 deletions(-)

diff --git a/clang/lib/Format/TokenAnnotator.cpp 
b/clang/lib/Format/TokenAnnotator.cpp
index a38ba8762f8505..a60d6ae197a24e 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -3817,7 +3817,7 @@ void 
TokenAnnotator::calculateFormattingInformation(AnnotatedLine ) const {
 do {
   Tok = Tok->Next;
 } while (Tok && Tok->isNot(TT_OverloadedOperatorLParen));
-if (!Tok || !Tok->Next)
+if (!Tok || !Tok->MatchingParen)
   break;
 const auto *LeftParen = Tok;
 for (Tok = Tok->Next; Tok && Tok != LeftParen->MatchingParen;
diff --git a/clang/unittests/Format/FormatTest.cpp 
b/clang/unittests/Format/FormatTest.cpp
index 3a3c88f609af7a..863d9c80d7ee6c 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -13503,8 +13503,8 @@ TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) {
   verifyFormat("{");
   verifyFormat("#})");
   verifyNoCrash("(/**/[:!] ?[).");
-  verifyNoCrash("struct X{"
-"  operator iunt("
+  verifyNoCrash("struct X {\n"
+"  operator iunt(\n"
 "};");
 }
 

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-format] Fix crash in TokenAnnotator (PR #82349)

2024-02-20 Thread Owen Pan via cfe-commits


@@ -13503,6 +13503,9 @@ TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) {
   verifyFormat("{");
   verifyFormat("#})");
   verifyNoCrash("(/**/[:!] ?[).");
+  verifyNoCrash("struct X{"
+"  operator iunt("

owenca wrote:

```suggestion
  verifyNoCrash("struct X {\n"
"  operator iunt(\n"
```

https://github.com/llvm/llvm-project/pull/82349
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-format] Fix crash in TokenAnnotator (PR #82349)

2024-02-20 Thread Owen Pan via cfe-commits


@@ -3817,7 +3817,7 @@ void 
TokenAnnotator::calculateFormattingInformation(AnnotatedLine ) const {
 do {
   Tok = Tok->Next;
 } while (Tok && Tok->isNot(TT_OverloadedOperatorLParen));
-if (!Tok)
+if (!Tok || !Tok->Next)

owenca wrote:

```suggestion
if (!Tok || !Tok->MatchingParen)
```
Otherwise, it would still crash if the unmatched `l_paren` is not the last 
token on the line, e.g.:
```
struct Foo {
  operator foo(bar
};

https://github.com/llvm/llvm-project/pull/82349
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-format] Fix crash in TokenAnnotator (PR #82349)

2024-02-20 Thread via cfe-commits

https://github.com/mydeveloperday approved this pull request.

LGTM

https://github.com/llvm/llvm-project/pull/82349
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-format] Fix crash in TokenAnnotator (PR #82349)

2024-02-20 Thread via cfe-commits

llvmbot wrote:




@llvm/pr-subscribers-clang-format

Author: Emilia Kond (rymiel)


Changes

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 https://github.com/llvm/llvm-project/issues/82328

---
Full diff: https://github.com/llvm/llvm-project/pull/82349.diff


2 Files Affected:

- (modified) clang/lib/Format/TokenAnnotator.cpp (+1-1) 
- (modified) clang/unittests/Format/FormatTest.cpp (+3) 


``diff
diff --git a/clang/lib/Format/TokenAnnotator.cpp 
b/clang/lib/Format/TokenAnnotator.cpp
index ec7b7f4dbe3470..a38ba8762f8505 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -3817,7 +3817,7 @@ void 
TokenAnnotator::calculateFormattingInformation(AnnotatedLine ) const {
 do {
   Tok = Tok->Next;
 } while (Tok && Tok->isNot(TT_OverloadedOperatorLParen));
-if (!Tok)
+if (!Tok || !Tok->Next)
   break;
 const auto *LeftParen = Tok;
 for (Tok = Tok->Next; Tok && Tok != LeftParen->MatchingParen;
diff --git a/clang/unittests/Format/FormatTest.cpp 
b/clang/unittests/Format/FormatTest.cpp
index 24f62af8ddcb87..3a3c88f609af7a 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -13503,6 +13503,9 @@ TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) {
   verifyFormat("{");
   verifyFormat("#})");
   verifyNoCrash("(/**/[:!] ?[).");
+  verifyNoCrash("struct X{"
+"  operator iunt("
+"};");
 }
 
 TEST_F(FormatTest, IncorrectUnbalancedBracesInMacrosWithUnicode) {

``




https://github.com/llvm/llvm-project/pull/82349
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-format] Fix crash in TokenAnnotator (PR #82349)

2024-02-20 Thread Emilia Kond via cfe-commits

https://github.com/rymiel created 
https://github.com/llvm/llvm-project/pull/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 https://github.com/llvm/llvm-project/issues/82328

>From 25ca98534f0d5dbd96ff74c802528171787ced45 Mon Sep 17 00:00:00 2001
From: Emilia Kond 
Date: Tue, 20 Feb 2024 14:11:33 +0200
Subject: [PATCH] [clang-format] Fix crash in TokenAnnotator

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 https://github.com/llvm/llvm-project/issues/82328
---
 clang/lib/Format/TokenAnnotator.cpp   | 2 +-
 clang/unittests/Format/FormatTest.cpp | 3 +++
 2 files changed, 4 insertions(+), 1 deletion(-)

diff --git a/clang/lib/Format/TokenAnnotator.cpp 
b/clang/lib/Format/TokenAnnotator.cpp
index ec7b7f4dbe3470..a38ba8762f8505 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -3817,7 +3817,7 @@ void 
TokenAnnotator::calculateFormattingInformation(AnnotatedLine ) const {
 do {
   Tok = Tok->Next;
 } while (Tok && Tok->isNot(TT_OverloadedOperatorLParen));
-if (!Tok)
+if (!Tok || !Tok->Next)
   break;
 const auto *LeftParen = Tok;
 for (Tok = Tok->Next; Tok && Tok != LeftParen->MatchingParen;
diff --git a/clang/unittests/Format/FormatTest.cpp 
b/clang/unittests/Format/FormatTest.cpp
index 24f62af8ddcb87..3a3c88f609af7a 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -13503,6 +13503,9 @@ TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) {
   verifyFormat("{");
   verifyFormat("#})");
   verifyNoCrash("(/**/[:!] ?[).");
+  verifyNoCrash("struct X{"
+"  operator iunt("
+"};");
 }
 
 TEST_F(FormatTest, IncorrectUnbalancedBracesInMacrosWithUnicode) {

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits