[clang] 10b5b5d - [clang] Fix a crash when referencing the result if the overload fails (#77288)

via cfe-commits cfe-commits at lists.llvm.org
Mon Jan 8 02:49:40 PST 2024


Author: Haojian Wu
Date: 2024-01-08T11:49:36+01:00
New Revision: 10b5b5d6e2df25dab86fe89a78c5df6f507f6e50

URL: https://github.com/llvm/llvm-project/commit/10b5b5d6e2df25dab86fe89a78c5df6f507f6e50
DIFF: https://github.com/llvm/llvm-project/commit/10b5b5d6e2df25dab86fe89a78c5df6f507f6e50.diff

LOG: [clang] Fix a crash when referencing the result if the overload fails (#77288)

after 20a05677f9394d4bc9467fe7bc93a4ebd3aeda61

If the overload fails, the `Best` might point to the `end()`,
referencing it leads to asan crashes.

Added: 
    

Modified: 
    clang/lib/Sema/SemaOverload.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/Sema/SemaOverload.cpp b/clang/lib/Sema/SemaOverload.cpp
index 9fb767101e1eb7..8e3a2d1288079b 100644
--- a/clang/lib/Sema/SemaOverload.cpp
+++ b/clang/lib/Sema/SemaOverload.cpp
@@ -13994,21 +13994,22 @@ ExprResult Sema::BuildOverloadedCallExpr(Scope *S, Expr *Fn,
   OverloadCandidateSet::iterator Best;
   OverloadingResult OverloadResult =
       CandidateSet.BestViableFunction(*this, Fn->getBeginLoc(), Best);
-  FunctionDecl *FDecl = Best->Function;
 
   // Model the case with a call to a templated function whose definition
   // encloses the call and whose return type contains a placeholder type as if
   // the UnresolvedLookupExpr was type-dependent.
-  if (OverloadResult == OR_Success && FDecl &&
-      FDecl->isTemplateInstantiation() &&
-      FDecl->getReturnType()->isUndeducedType()) {
-    if (auto TP = FDecl->getTemplateInstantiationPattern(false)) {
-      if (TP->willHaveBody()) {
-        CallExpr *CE =
-            CallExpr::Create(Context, Fn, Args, Context.DependentTy, VK_PRValue,
-                             RParenLoc, CurFPFeatureOverrides());
-        result = CE;
-        return result;
+  if (OverloadResult == OR_Success) {
+    FunctionDecl *FDecl = Best->Function;
+    if (FDecl && FDecl->isTemplateInstantiation() &&
+        FDecl->getReturnType()->isUndeducedType()) {
+      if (auto TP = FDecl->getTemplateInstantiationPattern(false)) {
+        if (TP->willHaveBody()) {
+          CallExpr *CE =
+              CallExpr::Create(Context, Fn, Args, Context.DependentTy,
+                               VK_PRValue, RParenLoc, CurFPFeatureOverrides());
+          result = CE;
+          return result;
+        }
       }
     }
   }


        


More information about the cfe-commits mailing list