Skip to content

Commit a99c978

Browse files
committed
[Clang] Avoid dereferencing an invalid iterator
Fix msan builds after 8c5a307 https://lab.llvm.org/buildbot/#/builders/94/builds/6321
1 parent d149631 commit a99c978

File tree

2 files changed

+13
-6
lines changed

2 files changed

+13
-6
lines changed

clang/include/clang/Sema/Overload.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1350,6 +1350,9 @@ class Sema;
13501350
iterator end() { return Candidates.end(); }
13511351

13521352
size_t size() const { return Candidates.size() + DeferredCandidatesCount; }
1353+
1354+
size_t nonDeferredCandidatesCount() const { return Candidates.size(); }
1355+
13531356
bool empty() const {
13541357
return Candidates.empty() && DeferredCandidatesCount == 0;
13551358
}

clang/lib/Sema/SemaOverload.cpp

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1108,7 +1108,7 @@ bool OverloadCandidateSet::OperatorRewriteInfo::shouldAddReversed(
11081108
}
11091109

11101110
void OverloadCandidateSet::destroyCandidates() {
1111-
for (iterator i = begin(), e = end(); i != e; ++i) {
1111+
for (iterator i = Candidates.begin(), e = Candidates.end(); i != e; ++i) {
11121112
for (auto &C : i->Conversions)
11131113
C.~ImplicitConversionSequence();
11141114
if (!i->Viable && i->FailureKind == ovl_fail_bad_deduction)
@@ -11237,7 +11237,7 @@ void OverloadCandidateSet::PerfectViableFunction(
1123711237
Sema &S, SourceLocation Loc, OverloadCandidateSet::iterator &Best) {
1123811238

1123911239
Best = end();
11240-
for (auto It = begin(); It != end(); ++It) {
11240+
for (auto It = Candidates.begin(); It != Candidates.end(); ++It) {
1124111241

1124211242
if (!It->isPerfectMatch(S.getASTContext()))
1124311243
continue;
@@ -11277,7 +11277,8 @@ OverloadingResult OverloadCandidateSet::BestViableFunctionImpl(
1127711277

1127811278
llvm::SmallVector<OverloadCandidate *, 16> Candidates;
1127911279
Candidates.reserve(this->Candidates.size());
11280-
std::transform(begin(), end(), std::back_inserter(Candidates),
11280+
std::transform(this->Candidates.begin(), this->Candidates.end(),
11281+
std::back_inserter(Candidates),
1128111282
[](OverloadCandidate &Cand) { return &Cand; });
1128211283

1128311284
if (S.getLangOpts().CUDA)
@@ -13050,7 +13051,8 @@ SmallVector<OverloadCandidate *, 32> OverloadCandidateSet::CompleteCandidates(
1305013051
// be prohibitive, so we make a set of pointers and sort those.
1305113052
SmallVector<OverloadCandidate*, 32> Cands;
1305213053
if (OCD == OCD_AllCandidates) Cands.reserve(size());
13053-
for (iterator Cand = begin(), LastCand = end(); Cand != LastCand; ++Cand) {
13054+
for (iterator Cand = Candidates.begin(), LastCand = Candidates.end();
13055+
Cand != LastCand; ++Cand) {
1305413056
if (!Filter(*Cand))
1305513057
continue;
1305613058
switch (OCD) {
@@ -13127,7 +13129,8 @@ void OverloadCandidateSet::NoteCandidates(
1312713129
NoteCandidates(S, Args, Cands, Opc, OpLoc);
1312813130

1312913131
if (OCD == OCD_AmbiguousCandidates)
13130-
MaybeDiagnoseAmbiguousConstraints(S, {begin(), end()});
13132+
MaybeDiagnoseAmbiguousConstraints(S,
13133+
{Candidates.begin(), Candidates.end()});
1313113134
}
1313213135

1313313136
void OverloadCandidateSet::NoteCandidates(Sema &S, ArrayRef<Expr *> Args,
@@ -16255,7 +16258,8 @@ Sema::BuildCallToObjectOfClassType(Scope *S, Expr *Obj,
1625516258
// we filter them out to produce better error diagnostics, ie to avoid
1625616259
// showing 2 failed overloads instead of one.
1625716260
bool IgnoreSurrogateFunctions = false;
16258-
if (CandidateSet.size() == 1 && Record->getAsCXXRecordDecl()->isLambda()) {
16261+
if (CandidateSet.nonDeferredCandidatesCount() == 1 &&
16262+
Record->getAsCXXRecordDecl()->isLambda()) {
1625916263
const OverloadCandidate &Candidate = *CandidateSet.begin();
1626016264
if (!Candidate.Viable &&
1626116265
Candidate.FailureKind == ovl_fail_constraints_not_satisfied)

0 commit comments

Comments
 (0)