Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Clang] Fix an integer overflow issue in computing CTAD's parameter depth #128704

Merged
merged 2 commits into from
Feb 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion clang/lib/Sema/SemaTemplateDeductionGuide.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -377,8 +377,15 @@ struct ConvertConstructorToDeductionGuideTransform {
if (NestedPattern)
Args.addOuterRetainedLevels(NestedPattern->getTemplateDepth());
auto [Depth, Index] = getDepthAndIndex(Param);
// Depth can still be 0 if FTD belongs to an explicit class template
// specialization with an empty template parameter list. In that case,
// we don't want the NewDepth to overflow, and it should remain 0.
assert(Depth ||
cast<ClassTemplateSpecializationDecl>(FTD->getDeclContext())
->isExplicitSpecialization());
NamedDecl *NewParam = transformTemplateParameter(
SemaRef, DC, Param, Args, Index + Depth1IndexAdjustment, Depth - 1);
SemaRef, DC, Param, Args, Index + Depth1IndexAdjustment,
Depth ? Depth - 1 : 0);
if (!NewParam)
return nullptr;
// Constraints require that we substitute depth-1 arguments
Expand Down
32 changes: 32 additions & 0 deletions clang/test/SemaTemplate/deduction-guide.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -691,3 +691,35 @@ Test test(42);
// CHECK-NEXT: | `-ParmVarDecl {{.*}} 'auto:1'

} // namespace GH122134

namespace GH128691 {

template <typename = void>
class NewDeleteAllocator;

template <>
struct NewDeleteAllocator<> {
template <typename T>
NewDeleteAllocator(T); // expected-note {{candidate template ignored}} \
// expected-note {{implicit deduction guide declared as}}
};

template <typename>
struct NewDeleteAllocator : NewDeleteAllocator<> { // expected-note {{candidate template ignored}} \
// expected-note {{implicit deduction guide declared as}}
using NewDeleteAllocator<>::NewDeleteAllocator;
};

void test() { NewDeleteAllocator abc(42); } // expected-error {{no viable constructor or deduction guide}}

// CHECK-LABEL: Dumping GH128691::<deduction guide for NewDeleteAllocator>:
// CHECK-NEXT: FunctionTemplateDecl {{.+}} <deduction guide for NewDeleteAllocator>
// CHECK-NEXT: |-TemplateTypeParmDecl {{.+}} typename depth 0 index 0
// CHECK-NEXT: | `-TemplateArgument type 'void'
// CHECK-NEXT: | |-inherited from TemplateTypeParm {{.+}} depth 0 index 0
// CHECK-NEXT: | `-BuiltinType {{.+}} 'void'
// CHECK-NEXT: |-TemplateTypeParmDecl {{.+}} typename depth 0 index 1 T
// CHECK-NEXT: `-CXXDeductionGuideDecl {{.+}} <deduction guide for NewDeleteAllocator> 'auto (T) -> NewDeleteAllocator<type-parameter-0-0>'
// CHECK-NEXT: `-ParmVarDecl {{.+}} 'T'

} // namespace GH128691
Loading