[clang] [clang][Sema] Fix crash when diagnosing candidates with parameter packs (PR #93079)

kadir çetinkaya via cfe-commits cfe-commits at lists.llvm.org
Fri May 24 01:30:32 PDT 2024


https://github.com/kadircet updated https://github.com/llvm/llvm-project/pull/93079

>From 6a057ff4b539045ce81e55b63702892496d18a97 Mon Sep 17 00:00:00 2001
From: Kadir Cetinkaya <kadircet at google.com>
Date: Wed, 22 May 2024 19:37:18 +0200
Subject: [PATCH] [clang][Sema] Fix crash when diagnosing candidates with
 parameter packs

Prevent OOB access.

Fixes https://github.com/llvm/llvm-project/issues/93076.
Fixes https://github.com/llvm/llvm-project/issues/76354.
Fixes https://github.com/llvm/llvm-project/issues/70191.
---
 clang/lib/Sema/SemaOverload.cpp          | 11 +++++++++--
 clang/test/SemaCXX/overload-template.cpp |  3 +++
 2 files changed, 12 insertions(+), 2 deletions(-)

diff --git a/clang/lib/Sema/SemaOverload.cpp b/clang/lib/Sema/SemaOverload.cpp
index 0c89fca8d38eb..86e869c7c72ff 100644
--- a/clang/lib/Sema/SemaOverload.cpp
+++ b/clang/lib/Sema/SemaOverload.cpp
@@ -13,6 +13,7 @@
 #include "clang/AST/ASTContext.h"
 #include "clang/AST/ASTLambda.h"
 #include "clang/AST/CXXInheritance.h"
+#include "clang/AST/Decl.h"
 #include "clang/AST/DeclCXX.h"
 #include "clang/AST/DeclObjC.h"
 #include "clang/AST/DependenceFlags.h"
@@ -11301,8 +11302,14 @@ static void DiagnoseBadConversion(Sema &S, OverloadCandidate *Cand,
   Expr *FromExpr = Conv.Bad.FromExpr;
   QualType FromTy = Conv.Bad.getFromType();
   QualType ToTy = Conv.Bad.getToType();
-  SourceRange ToParamRange =
-      !isObjectArgument ? Fn->getParamDecl(I)->getSourceRange() : SourceRange();
+  SourceRange ToParamRange;
+
+  // FIXME: In presence of parameter packs we can't determine parameter range
+  // reliably, as we don't have access to instantiation.
+  bool HasParamPack = llvm::any_of(Fn->parameters().take_front(I),
+               [](const ParmVarDecl *Parm) { return Parm->isParameterPack(); });
+  if (!isObjectArgument && !HasParamPack)
+    ToParamRange = Fn->getParamDecl(I)->getSourceRange();
 
   if (FromTy == S.Context.OverloadTy) {
     assert(FromExpr && "overload set argument came from implicit argument?");
diff --git a/clang/test/SemaCXX/overload-template.cpp b/clang/test/SemaCXX/overload-template.cpp
index 0fe13c479cce2..01cfe87a05831 100644
--- a/clang/test/SemaCXX/overload-template.cpp
+++ b/clang/test/SemaCXX/overload-template.cpp
@@ -58,3 +58,6 @@ namespace overloadCheck{
   }
 }
 #endif
+
+template <typename ...a> int b(a...); // expected-note {{candidate function template not viable: no known conversion from 'int ()' to 'int' for 2nd argument}}
+int d() { return b<int, int>(0, d); } // expected-error {{no matching function for call to 'b'}}



More information about the cfe-commits mailing list