Skip to content

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

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

Merged
merged 1 commit into from
May 27, 2024

Conversation

kadircet
Copy link
Member

Prevent OOB access.

Fixes #93076

@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels May 22, 2024
@llvmbot
Copy link
Member

llvmbot commented May 22, 2024

@llvm/pr-subscribers-clang

Author: kadir çetinkaya (kadircet)

Changes

Prevent OOB access.

Fixes #93076


Full diff: https://github.com/llvm/llvm-project/pull/93079.diff

2 Files Affected:

  • (modified) clang/lib/Sema/SemaOverload.cpp (+3-2)
  • (modified) clang/test/SemaCXX/overload-template.cpp (+3)
diff --git a/clang/lib/Sema/SemaOverload.cpp b/clang/lib/Sema/SemaOverload.cpp
index 2eb25237a0de6..c4f85df1ef697 100644
--- a/clang/lib/Sema/SemaOverload.cpp
+++ b/clang/lib/Sema/SemaOverload.cpp
@@ -11298,8 +11298,9 @@ 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 && I < Fn->getNumParams())
+    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'}}

@kadircet kadircet requested a review from cor3ntin May 22, 2024 17:41
Copy link
Collaborator

@shafik shafik left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the fix. Can you please a little more details to your summary so that folks reading git log have more context.

This also needs a release note.

Please also add that this also fixes: #76354

@shafik
Copy link
Collaborator

shafik commented May 22, 2024

Can you also confirm this fixes: #70191

@kadircet kadircet requested a review from mizvekov May 23, 2024 14:02
Copy link

github-actions bot commented May 24, 2024

✅ With the latest revision this PR passed the C/C++ code formatter.

@kadircet
Copy link
Member Author

@shafik thx for the pointers, i didn't know this came up before and even there were attempted fixes. I can verify that both of those are also fixed with this patch.

@knightXun do you plan to proceed with #70280 ? If so I am happy to discard this PR in favor of yours.

Copy link
Contributor

@mizvekov mizvekov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you also add a test based on my example?

With also a variant on that in which the bad conversion happens on the last element of the pack, instead of the last parameter.

Lastly, please namespace the tests with the name of, or add a comment naming, the GitHub issue.

Otherwise, LGTM.

Prevent OOB access by not printing target parameter range when there's a
pack in the function parameters.

Fixes llvm#93076.
Fixes llvm#76354.
Fixes llvm#70191.
@kadircet kadircet merged commit 7a28a5b into llvm:main May 27, 2024
5 of 9 checks passed
@kadircet kadircet deleted the fix_93076 branch May 27, 2024 10:18
qiaojbao pushed a commit to GPUOpen-Drivers/llvm-project that referenced this pull request Jun 26, 2024
…4034d613e

Local branch amd-gfx 6434034 Merged main:7429950d840b8fec3d9a48d00e612a3240c2be83 into amd-gfx:49c66abb8fcb
Remote branch main 7a28a5b [clang][Sema] Fix crash when diagnosing candidates with parameter packs (llvm#93079)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

clang crash while diagnosing bad overload canidate
4 participants