[clang-tools-extra] [clangd] Fix use-after-free issues in TidyProvider.cpp (PR #114808)

Haojian Wu via cfe-commits cfe-commits at lists.llvm.org
Mon Nov 4 07:05:08 PST 2024


https://github.com/hokein created https://github.com/llvm/llvm-project/pull/114808

None

>From 38c41b134b7092d880ba349bcca1a7bb6887d1dc Mon Sep 17 00:00:00 2001
From: Haojian Wu <hokein.wu at gmail.com>
Date: Mon, 4 Nov 2024 16:03:27 +0100
Subject: [PATCH] [clangd] Fix use-after-free issues in TidyProvider.cpp

---
 clang-tools-extra/clangd/TidyProvider.cpp | 19 ++++++++++++-------
 1 file changed, 12 insertions(+), 7 deletions(-)

diff --git a/clang-tools-extra/clangd/TidyProvider.cpp b/clang-tools-extra/clangd/TidyProvider.cpp
index a87238e0c0938c..f35cf9f0ab999d 100644
--- a/clang-tools-extra/clangd/TidyProvider.cpp
+++ b/clang-tools-extra/clangd/TidyProvider.cpp
@@ -46,7 +46,7 @@ class DotClangTidyCache : private FileCache {
         [this](std::optional<llvm::StringRef> Data) {
           Value.reset();
           if (Data && !Data->empty()) {
-            tidy::DiagCallback Diagnostics = [](const llvm::SMDiagnostic &D) {
+            auto Diagnostics = [](const llvm::SMDiagnostic &D) {
               switch (D.getKind()) {
               case llvm::SourceMgr::DK_Error:
                 elog("tidy-config error at {0}:{1}:{2}: {3}", D.getFilename(),
@@ -159,12 +159,15 @@ TidyProviderRef provideEnvironment() {
     return Ret;
   }();
 
-  if (User)
-    return
-        [](tidy::ClangTidyOptions &Opts, llvm::StringRef) { Opts.User = User; };
+  if (User) {
+    static const auto Provider = [](tidy::ClangTidyOptions &Opts, llvm::StringRef) { Opts.User = User; };
+    return Provider;
+  }
   // FIXME: Once function_ref and unique_function operator= operators handle
   // null values, this can return null.
-  return [](tidy::ClangTidyOptions &, llvm::StringRef) {};
+  static const auto EmptyProvider = [](tidy::ClangTidyOptions &,
+                                       llvm::StringRef) {};
+  return EmptyProvider;
 }
 
 TidyProviderRef provideDefaultChecks() {
@@ -178,10 +181,11 @@ TidyProviderRef provideDefaultChecks() {
       "bugprone-suspicious-missing-comma", "bugprone-unused-raii",
       "bugprone-unused-return-value", "misc-unused-using-decls",
       "misc-unused-alias-decls", "misc-definitions-in-headers");
-  return [](tidy::ClangTidyOptions &Opts, llvm::StringRef) {
+  static auto Provider =  [](tidy::ClangTidyOptions &Opts, llvm::StringRef) {
     if (!Opts.Checks || Opts.Checks->empty())
       Opts.Checks = DefaultChecks;
   };
+  return Provider;
 }
 
 TidyProvider addTidyChecks(llvm::StringRef Checks,
@@ -252,7 +256,7 @@ TidyProvider disableUnusableChecks(llvm::ArrayRef<std::string> ExtraBadChecks) {
 }
 
 TidyProviderRef provideClangdConfig() {
-  return [](tidy::ClangTidyOptions &Opts, llvm::StringRef) {
+  static const auto Provider = [](tidy::ClangTidyOptions &Opts, llvm::StringRef) {
     const auto &CurTidyConfig = Config::current().Diagnostics.ClangTidy;
     if (!CurTidyConfig.Checks.empty())
       mergeCheckList(Opts.Checks, CurTidyConfig.Checks);
@@ -262,6 +266,7 @@ TidyProviderRef provideClangdConfig() {
                                          tidy::ClangTidyOptions::ClangTidyValue(
                                              CheckOption.getValue(), 10000U));
   };
+  return Provider;
 }
 
 TidyProvider provideClangTidyFiles(ThreadsafeFS &TFS) {



More information about the cfe-commits mailing list