Skip to content

[clang-format] Fix crash in TokenAnnotator #82349

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Feb 22, 2024
Merged

Conversation

rymiel
Copy link
Member

@rymiel rymiel commented Feb 20, 2024

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

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 llvm#82328
@llvmbot
Copy link
Member

llvmbot commented Feb 20, 2024

@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 #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 --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 &Line) 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) {

Copy link
Contributor

@mydeveloperday mydeveloperday left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@rymiel rymiel merged commit 2e7cacf into llvm:main Feb 22, 2024
@rymiel rymiel deleted the fix/crash-annotator branch February 22, 2024 18:22
arBmind pushed a commit to hicknhack-software/Qt-llvm-project that referenced this pull request Mar 9, 2024
Squash commit from llvm/llvm-project#82349

Change-Id: Ie2a8d501486f2132e5ab9db57159a01f1b631fec
Reviewed-by: Marcus Tillmanns <[email protected]>
arBmind pushed a commit to hicknhack-software/Qt-llvm-project that referenced this pull request Mar 9, 2024
Squash commit from llvm/llvm-project#82349

Change-Id: Ie2a8d501486f2132e5ab9db57159a01f1b631fec
arBmind pushed a commit to hicknhack-software/Qt-llvm-project that referenced this pull request Mar 9, 2024
Squash commit from llvm/llvm-project#82349

Change-Id: Ie2a8d501486f2132e5ab9db57159a01f1b631fec
Reviewed-by: Marcus Tillmanns <[email protected]>
arBmind pushed a commit to hicknhack-software/Qt-llvm-project that referenced this pull request May 18, 2024
Squash commit from llvm/llvm-project#82349

Change-Id: Ie2a8d501486f2132e5ab9db57159a01f1b631fec
arBmind pushed a commit to hicknhack-software/Qt-llvm-project that referenced this pull request Jun 14, 2024
Squash commit from llvm/llvm-project#82349

Change-Id: Ie2a8d501486f2132e5ab9db57159a01f1b631fec
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Crash when clang-format is used for incomplete file
4 participants