[NFC] Fix a mem-sanitizer found issue in AutoType
authorErich Keane <erich.keane@intel.com>
Fri, 28 Apr 2023 16:54:28 +0000 (09:54 -0700)
committerErich Keane <erich.keane@intel.com>
Fri, 28 Apr 2023 17:09:26 +0000 (10:09 -0700)
We only need the list of constriant template arguments when we have a
valid constraint.  We give up on merging the auto-type constraints if
the template arguments don't match, but neglected to clear the
collection of template arguments.  The result was we had an AutoType
where we initialized the number of template arguments, but never
initialized the template arguments themselves.

This patch adds an assert to catch this in the future, plus ensures we
clear out the vector so we don't try to create the AutoType incorrectly.

clang/lib/AST/ASTContext.cpp
clang/lib/AST/Type.cpp

index e331df8..6bc202e 100644 (file)
@@ -12982,8 +12982,10 @@ static QualType getCommonSugarTypeNode(ASTContext &Ctx, const Type *X,
     SmallVector<TemplateArgument, 8> As;
     if (CD &&
         getCommonTemplateArguments(Ctx, As, AX->getTypeConstraintArguments(),
-                                   AY->getTypeConstraintArguments()))
+                                   AY->getTypeConstraintArguments())) {
       CD = nullptr; // The arguments differ, so make it unconstrained.
+      As.clear();
+    }
 
     // Both auto types can't be dependent, otherwise they wouldn't have been
     // sugar. This implies they can't contain unexpanded packs either.
index e1686a7..c0d2250 100644 (file)
@@ -4630,6 +4630,7 @@ AutoType::AutoType(QualType DeducedAsType, AutoTypeKeyword Keyword,
   AutoTypeBits.Keyword = (unsigned)Keyword;
   AutoTypeBits.NumArgs = TypeConstraintArgs.size();
   this->TypeConstraintConcept = TypeConstraintConcept;
+  assert(TypeConstraintConcept || AutoTypeBits.NumArgs == 0);
   if (TypeConstraintConcept) {
     auto *ArgBuffer =
         const_cast<TemplateArgument *>(getTypeConstraintArguments().data());