[clang] [Sema] Fix an out-of-bounds crash when diagnosing bad conversion for a function with a parameter pack. (PR #92721)
Haojian Wu via cfe-commits
cfe-commits at lists.llvm.org
Mon May 20 00:13:28 PDT 2024
https://github.com/hokein created https://github.com/llvm/llvm-project/pull/92721
None
>From 2aa2caf15282d28e9fda99d51776d842d3d57e95 Mon Sep 17 00:00:00 2001
From: Haojian Wu <hokein.wu at gmail.com>
Date: Sun, 19 May 2024 22:47:14 +0200
Subject: [PATCH] [Sema] Fix an out-of-bounds crash when diagnosing bad
conversion for a function with a parameter pack.
---
clang/docs/ReleaseNotes.rst | 2 ++
clang/lib/Sema/SemaOverload.cpp | 10 ++++++++--
clang/test/Misc/diag-overload-cand-ranges.cpp | 8 ++++++++
3 files changed, 18 insertions(+), 2 deletions(-)
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index be4cded276321..de3d258ae578a 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -573,6 +573,8 @@ Bug Fixes in This Version
- Clang now correctly disallows VLA type compound literals, e.g. ``(int[size]){}``,
as the C standard mandates. (#GH89835)
+- Fix an out-of-bounds crash when diagnosing bad conversion for a function with a parameter pack.
+
Bug Fixes to Compiler Builtins
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
diff --git a/clang/lib/Sema/SemaOverload.cpp b/clang/lib/Sema/SemaOverload.cpp
index 2eb25237a0de6..73a175f724402 100644
--- a/clang/lib/Sema/SemaOverload.cpp
+++ b/clang/lib/Sema/SemaOverload.cpp
@@ -11298,8 +11298,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;
+ if (!isObjectArgument) {
+ if (I < Fn->getNumParams())
+ ToParamRange = Fn->getParamDecl(I)->getSourceRange();
+ else
+ // parameter pack case.
+ ToParamRange = Fn->parameters().back()->getSourceRange();
+ }
if (FromTy == S.Context.OverloadTy) {
assert(FromExpr && "overload set argument came from implicit argument?");
diff --git a/clang/test/Misc/diag-overload-cand-ranges.cpp b/clang/test/Misc/diag-overload-cand-ranges.cpp
index 080ca484d4b74..06d638d9b719c 100644
--- a/clang/test/Misc/diag-overload-cand-ranges.cpp
+++ b/clang/test/Misc/diag-overload-cand-ranges.cpp
@@ -70,3 +70,11 @@ template <short T> class Type1 {};
template <short T> void Function1(int zz, Type1<T> &x, int ww) {}
void Function() { Function1(33, Type1<-42>(), 66); }
+
+// CHECK: error: no matching function for call to 'b'
+// CHECK: :{[[@LINE+1]]:41-[[@LINE+1]]:45}: note: {{.*}} no known conversion from 'int' to 'ForwardClass' for 3rd argument
+template <class T, class...U> void b(T, U...);
+class ForwardClass;
+void NoCrash() {
+ b<int, int, ForwardClass>(1, 1, 0);
+}
More information about the cfe-commits
mailing list