[clang] 85eb725 - [clang] Fix use-after-scope when diagnosting __attribute__((format_matches))

Benjamin Kramer via cfe-commits cfe-commits at lists.llvm.org
Tue Feb 25 07:43:47 PST 2025


Author: Benjamin Kramer
Date: 2025-02-25T16:41:52+01:00
New Revision: 85eb7259d9e1ab57e9fac248096d73505a60c072

URL: https://github.com/llvm/llvm-project/commit/85eb7259d9e1ab57e9fac248096d73505a60c072
DIFF: https://github.com/llvm/llvm-project/commit/85eb7259d9e1ab57e9fac248096d73505a60c072.diff

LOG: [clang] Fix use-after-scope when diagnosting __attribute__((format_matches))

I don't think this will ever crash, but asan complains about it.

SUMMARY: AddressSanitizer: stack-use-after-scope clang/lib/Sema/SemaChecking.cpp:6925:43 in void (anonymous namespace)::CheckFormatHandler::EmitFormatDiagnostic<clang::CharSourceRange>(clang::PartialDiagnostic, clang::SourceLocation, bool, clang::CharSourceRange, llvm::ArrayRef<clang::FixItHint>)

While there switch to stable_sort to not give a flipped error message
half of the time.

Added: 
    

Modified: 
    clang/lib/Sema/SemaChecking.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp
index 81209f2242f59..f9926c6b4adab 100644
--- a/clang/lib/Sema/SemaChecking.cpp
+++ b/clang/lib/Sema/SemaChecking.cpp
@@ -7390,10 +7390,11 @@ bool DecomposePrintfHandler::GetSpecifiers(
   const char *Str = Data.data();
   llvm::SmallBitVector BV;
   UncoveredArgHandler UA;
+  const Expr *PrintfArgs[] = {FSL->getFormatString()};
   DecomposePrintfHandler H(S, FSL, FSL->getFormatString(), Type, 0, 0, IsObjC,
-                           Str, Sema::FAPK_Elsewhere, {FSL->getFormatString()},
-                           0, InFunctionCall, Sema::VariadicDoesNotApply, BV,
-                           UA, Args);
+                           Str, Sema::FAPK_Elsewhere, PrintfArgs, 0,
+                           InFunctionCall, Sema::VariadicDoesNotApply, BV, UA,
+                           Args);
 
   if (!analyze_format_string::ParsePrintfString(
           H, Str, Str + Data.size(), S.getLangOpts(), S.Context.getTargetInfo(),
@@ -7402,7 +7403,7 @@ bool DecomposePrintfHandler::GetSpecifiers(
   if (H.HadError)
     return false;
 
-  std::sort(
+  std::stable_sort(
       Args.begin(), Args.end(),
       [](const EquatableFormatArgument &A, const EquatableFormatArgument &B) {
         return A.getPosition() < B.getPosition();


        


More information about the cfe-commits mailing list