Skip to content

Commit ef02e11

Browse files
authored
[Sema] Fix memory leak in DeductionFailureInfo (#143129)
Move allocation of deduction failure info from the ASTContext to the heap. Implement corresponding type-safe delete calls in `DeductionFailureInfo::Destroy()` to prevent memory leaks.
1 parent bfbf5d5 commit ef02e11

File tree

1 file changed

+23
-13
lines changed

1 file changed

+23
-13
lines changed

clang/lib/Sema/SemaOverload.cpp

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -756,8 +756,8 @@ clang::MakeDeductionFailureInfo(ASTContext &Context,
756756

757757
case TemplateDeductionResult::DeducedMismatch:
758758
case TemplateDeductionResult::DeducedMismatchNested: {
759-
// FIXME: Should allocate from normal heap so that we can free this later.
760-
auto *Saved = new (Context) DFIDeducedMismatchArgs;
759+
// Allocate from normal heap so that we can free this later.
760+
auto *Saved = new DFIDeducedMismatchArgs;
761761
Saved->FirstArg = Info.FirstArg;
762762
Saved->SecondArg = Info.SecondArg;
763763
Saved->TemplateArgs = Info.takeSugared();
@@ -767,8 +767,8 @@ clang::MakeDeductionFailureInfo(ASTContext &Context,
767767
}
768768

769769
case TemplateDeductionResult::NonDeducedMismatch: {
770-
// FIXME: Should allocate from normal heap so that we can free this later.
771-
DFIArguments *Saved = new (Context) DFIArguments;
770+
// Allocate from normal heap so that we can free this later.
771+
DFIArguments *Saved = new DFIArguments;
772772
Saved->FirstArg = Info.FirstArg;
773773
Saved->SecondArg = Info.SecondArg;
774774
Result.Data = Saved;
@@ -779,8 +779,8 @@ clang::MakeDeductionFailureInfo(ASTContext &Context,
779779
// FIXME: It's slightly wasteful to allocate two TemplateArguments for this.
780780
case TemplateDeductionResult::Inconsistent:
781781
case TemplateDeductionResult::Underqualified: {
782-
// FIXME: Should allocate from normal heap so that we can free this later.
783-
DFIParamWithArguments *Saved = new (Context) DFIParamWithArguments;
782+
// Allocate from normal heap so that we can free this later.
783+
DFIParamWithArguments *Saved = new DFIParamWithArguments;
784784
Saved->Param = Info.Param;
785785
Saved->FirstArg = Info.FirstArg;
786786
Saved->SecondArg = Info.SecondArg;
@@ -799,7 +799,8 @@ clang::MakeDeductionFailureInfo(ASTContext &Context,
799799
break;
800800

801801
case TemplateDeductionResult::ConstraintsNotSatisfied: {
802-
CNSInfo *Saved = new (Context) CNSInfo;
802+
// Allocate from normal heap so that we can free this later.
803+
CNSInfo *Saved = new CNSInfo;
803804
Saved->TemplateArgs = Info.takeSugared();
804805
Saved->Satisfaction = Info.AssociatedConstraintsSatisfaction;
805806
Result.Data = Saved;
@@ -831,31 +832,40 @@ void DeductionFailureInfo::Destroy() {
831832
case TemplateDeductionResult::IncompletePack:
832833
case TemplateDeductionResult::Inconsistent:
833834
case TemplateDeductionResult::Underqualified:
835+
delete static_cast<DFIParamWithArguments*>(Data);
836+
Data = nullptr;
837+
break;
834838
case TemplateDeductionResult::DeducedMismatch:
835839
case TemplateDeductionResult::DeducedMismatchNested:
840+
delete static_cast<DFIDeducedMismatchArgs*>(Data);
841+
Data = nullptr;
842+
break;
836843
case TemplateDeductionResult::NonDeducedMismatch:
837-
// FIXME: Destroy the data?
844+
// Destroy the data
845+
delete static_cast<DFIArguments*>(Data);
838846
Data = nullptr;
839847
break;
840848

841-
case TemplateDeductionResult::SubstitutionFailure:
849+
case TemplateDeductionResult::SubstitutionFailure:{
842850
// FIXME: Destroy the template argument list?
843851
Data = nullptr;
844852
if (PartialDiagnosticAt *Diag = getSFINAEDiagnostic()) {
845853
Diag->~PartialDiagnosticAt();
846854
HasDiagnostic = false;
847855
}
848856
break;
849-
850-
case TemplateDeductionResult::ConstraintsNotSatisfied:
851-
// FIXME: Destroy the template argument list?
857+
}
858+
case TemplateDeductionResult::ConstraintsNotSatisfied:{
859+
// Destroy the data
860+
CNSInfo *Ptr = static_cast<CNSInfo*>(Data);
861+
delete Ptr;
852862
Data = nullptr;
853863
if (PartialDiagnosticAt *Diag = getSFINAEDiagnostic()) {
854864
Diag->~PartialDiagnosticAt();
855865
HasDiagnostic = false;
856866
}
857867
break;
858-
868+
}
859869
// Unhandled
860870
case TemplateDeductionResult::MiscellaneousDeductionFailure:
861871
case TemplateDeductionResult::AlreadyDiagnosed:

0 commit comments

Comments
 (0)