Author: Corentin Jabot
Date: 2025-04-18T10:03:06+02:00
New Revision: a99c978d1b35e30d9a0fe9db68b91e9f2815c8e9

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

LOG: [Clang] Avoid dereferencing an invalid iterator

Fix msan builds after 8c5a307bd8
https://lab.llvm.org/buildbot/#/builders/94/builds/6321

Added: 
    

Modified: 
    clang/include/clang/Sema/Overload.h
    clang/lib/Sema/SemaOverload.cpp

Removed: 
    


################################################################################
diff  --git a/clang/include/clang/Sema/Overload.h 
b/clang/include/clang/Sema/Overload.h
index 55d714c1222a1..8182ce9c39685 100644
--- a/clang/include/clang/Sema/Overload.h
+++ b/clang/include/clang/Sema/Overload.h
@@ -1350,6 +1350,9 @@ class Sema;
     iterator end() { return Candidates.end(); }
 
     size_t size() const { return Candidates.size() + DeferredCandidatesCount; }
+
+    size_t nonDeferredCandidatesCount() const { return Candidates.size(); }
+
     bool empty() const {
       return Candidates.empty() && DeferredCandidatesCount == 0;
     }

diff  --git a/clang/lib/Sema/SemaOverload.cpp b/clang/lib/Sema/SemaOverload.cpp
index b7a981e08ead9..e4ff8c5489df3 100644
--- a/clang/lib/Sema/SemaOverload.cpp
+++ b/clang/lib/Sema/SemaOverload.cpp
@@ -1108,7 +1108,7 @@ bool 
OverloadCandidateSet::OperatorRewriteInfo::shouldAddReversed(
 }
 
 void OverloadCandidateSet::destroyCandidates() {
-  for (iterator i = begin(), e = end(); i != e; ++i) {
+  for (iterator i = Candidates.begin(), e = Candidates.end(); i != e; ++i) {
     for (auto &C : i->Conversions)
       C.~ImplicitConversionSequence();
     if (!i->Viable && i->FailureKind == ovl_fail_bad_deduction)
@@ -11237,7 +11237,7 @@ void OverloadCandidateSet::PerfectViableFunction(
     Sema &S, SourceLocation Loc, OverloadCandidateSet::iterator &Best) {
 
   Best = end();
-  for (auto It = begin(); It != end(); ++It) {
+  for (auto It = Candidates.begin(); It != Candidates.end(); ++It) {
 
     if (!It->isPerfectMatch(S.getASTContext()))
       continue;
@@ -11277,7 +11277,8 @@ OverloadingResult 
OverloadCandidateSet::BestViableFunctionImpl(
 
   llvm::SmallVector<OverloadCandidate *, 16> Candidates;
   Candidates.reserve(this->Candidates.size());
-  std::transform(begin(), end(), std::back_inserter(Candidates),
+  std::transform(this->Candidates.begin(), this->Candidates.end(),
+                 std::back_inserter(Candidates),
                  [](OverloadCandidate &Cand) { return &Cand; });
 
   if (S.getLangOpts().CUDA)
@@ -13050,7 +13051,8 @@ SmallVector<OverloadCandidate *, 32> 
OverloadCandidateSet::CompleteCandidates(
   // be prohibitive, so we make a set of pointers and sort those.
   SmallVector<OverloadCandidate*, 32> Cands;
   if (OCD == OCD_AllCandidates) Cands.reserve(size());
-  for (iterator Cand = begin(), LastCand = end(); Cand != LastCand; ++Cand) {
+  for (iterator Cand = Candidates.begin(), LastCand = Candidates.end();
+       Cand != LastCand; ++Cand) {
     if (!Filter(*Cand))
       continue;
     switch (OCD) {
@@ -13127,7 +13129,8 @@ void OverloadCandidateSet::NoteCandidates(
     NoteCandidates(S, Args, Cands, Opc, OpLoc);
 
   if (OCD == OCD_AmbiguousCandidates)
-    MaybeDiagnoseAmbiguousConstraints(S, {begin(), end()});
+    MaybeDiagnoseAmbiguousConstraints(S,
+                                      {Candidates.begin(), Candidates.end()});
 }
 
 void OverloadCandidateSet::NoteCandidates(Sema &S, ArrayRef<Expr *> Args,
@@ -16255,7 +16258,8 @@ Sema::BuildCallToObjectOfClassType(Scope *S, Expr *Obj,
   // we filter them out to produce better error diagnostics, ie to avoid
   // showing 2 failed overloads instead of one.
   bool IgnoreSurrogateFunctions = false;
-  if (CandidateSet.size() == 1 && Record->getAsCXXRecordDecl()->isLambda()) {
+  if (CandidateSet.nonDeferredCandidatesCount() == 1 &&
+      Record->getAsCXXRecordDecl()->isLambda()) {
     const OverloadCandidate &Candidate = *CandidateSet.begin();
     if (!Candidate.Viable &&
         Candidate.FailureKind == ovl_fail_constraints_not_satisfied)


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

Reply via email to