Skip to content

Commit eeae197

Browse files
committed
[Clang] Fix an integer overflow issue in computing CTAD's parameter depth
1 parent 4cc7d60 commit eeae197

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

clang/lib/Sema/SemaTemplateDeductionGuide.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -377,8 +377,12 @@ struct ConvertConstructorToDeductionGuideTransform {
377377
if (NestedPattern)
378378
Args.addOuterRetainedLevels(NestedPattern->getTemplateDepth());
379379
auto [Depth, Index] = getDepthAndIndex(Param);
380+
assert(Depth ||
381+
cast<ClassTemplateSpecializationDecl>(FTD->getDeclContext())
382+
->isExplicitSpecialization());
380383
NamedDecl *NewParam = transformTemplateParameter(
381-
SemaRef, DC, Param, Args, Index + Depth1IndexAdjustment, Depth - 1);
384+
SemaRef, DC, Param, Args, Index + Depth1IndexAdjustment,
385+
Depth ? Depth - 1 : 0);
382386
if (!NewParam)
383387
return nullptr;
384388
// Constraints require that we substitute depth-1 arguments

clang/test/SemaTemplate/deduction-guide.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -691,3 +691,35 @@ Test test(42);
691691
// CHECK-NEXT: | `-ParmVarDecl {{.*}} 'auto:1'
692692

693693
} // namespace GH122134
694+
695+
namespace GH128691 {
696+
697+
template <typename = void>
698+
class NewDeleteAllocator;
699+
700+
template <>
701+
struct NewDeleteAllocator<> {
702+
template <typename T>
703+
NewDeleteAllocator(T); // expected-note {{candidate template ignored}} \
704+
// expected-note {{implicit deduction guide declared as}}
705+
};
706+
707+
template <typename>
708+
struct NewDeleteAllocator : NewDeleteAllocator<> { // expected-note {{candidate template ignored}} \
709+
// expected-note {{implicit deduction guide declared as}}
710+
using NewDeleteAllocator<>::NewDeleteAllocator;
711+
};
712+
713+
void test() { NewDeleteAllocator abc(42); } // expected-error {{no viable constructor or deduction guide}}
714+
715+
// CHECK-LABEL: Dumping GH128691::<deduction guide for NewDeleteAllocator>:
716+
// CHECK-NEXT: FunctionTemplateDecl {{.+}} <deduction guide for NewDeleteAllocator>
717+
// CHECK-NEXT: |-TemplateTypeParmDecl {{.+}} typename depth 0 index 0
718+
// CHECK-NEXT: | `-TemplateArgument type 'void'
719+
// CHECK-NEXT: | |-inherited from TemplateTypeParm {{.+}} depth 0 index 0
720+
// CHECK-NEXT: | `-BuiltinType {{.+}} 'void'
721+
// CHECK-NEXT: |-TemplateTypeParmDecl {{.+}} typename depth 0 index 1 T
722+
// CHECK-NEXT: `-CXXDeductionGuideDecl {{.+}} <deduction guide for NewDeleteAllocator> 'auto (T) -> NewDeleteAllocator<type-parameter-0-0>'
723+
// CHECK-NEXT: `-ParmVarDecl {{.+}} 'T'
724+
725+
} // namespace GH128691

0 commit comments

Comments
 (0)