[clang] [clang][Sema] Fix crash when diagnosing candidates with parameter packs (PR #93079)
https://github.com/kadircet closed https://github.com/llvm/llvm-project/pull/93079 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang][Sema] Fix crash when diagnosing candidates with parameter packs (PR #93079)
https://github.com/kadircet updated https://github.com/llvm/llvm-project/pull/93079 From 98ae27a0d303252a23891b204df18112a846f661 Mon Sep 17 00:00:00 2001 From: Kadir Cetinkaya Date: Wed, 22 May 2024 19:37:18 +0200 Subject: [PATCH] [clang][Sema] Fix crash when diagnosing candidates with parameter packs Prevent OOB access by not printing target parameter range when there's a pack in the function parameters. 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/docs/ReleaseNotes.rst | 3 ++- clang/lib/Sema/SemaOverload.cpp | 13 +++-- clang/test/SemaCXX/overload-template.cpp | 10 ++ 3 files changed, 23 insertions(+), 3 deletions(-) diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst index 825e91876ffce..81b8d42aaa84e 100644 --- a/clang/docs/ReleaseNotes.rst +++ b/clang/docs/ReleaseNotes.rst @@ -734,7 +734,6 @@ Bug Fixes to C++ Support from being explicitly specialized for a given implicit instantiation of the class template. - Fixed a crash when ``this`` is used in a dependent class scope function template specialization that instantiates to a static member function. - - Fix crash when inheriting from a cv-qualified type. Fixes #GH35603 - Fix a crash when the using enum declaration uses an anonymous enumeration. Fixes (#GH86790). - Handled an edge case in ``getFullyPackExpandedSize`` so that we now avoid a false-positive diagnostic. (#GH84220) @@ -796,6 +795,8 @@ Bug Fixes to C++ Support Fixes (#GH91308). - Fix a crash caused by a regression in the handling of ``source_location`` in dependent contexts. Fixes (#GH92680). +- Fixed a crash when diagnosing failed conversions involving template parameter + packs. (#GH93076) Bug Fixes to AST Handling ^ diff --git a/clang/lib/Sema/SemaOverload.cpp b/clang/lib/Sema/SemaOverload.cpp index 0c89fca8d38eb..61d3c1633a2b7 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,16 @@ static void DiagnoseBadConversion(Sema , 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..3277a17e5e450 100644 --- a/clang/test/SemaCXX/overload-template.cpp +++ b/clang/test/SemaCXX/overload-template.cpp @@ -58,3 +58,13 @@ namespace overloadCheck{ } } #endif + +namespace GH93076 { +template int b(a..., int); // expected-note-re 3 {{candidate function template not viable: no known conversion from 'int ()' to 'int' for {{.*}} argument}} +int d() { + (void)b(0, 0, d); // expected-error {{no matching function for call to 'b'}} + (void)b(0, d, 0); // expected-error {{no matching function for call to 'b'}} + (void)b(d, 0, 0); // expected-error {{no matching function for call to 'b'}} + return 0; + } +} ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang][Sema] Fix crash when diagnosing candidates with parameter packs (PR #93079)
https://github.com/kadircet updated https://github.com/llvm/llvm-project/pull/93079 From d133b1bf63ab8c5408497ef4c2d2d629ebcff8eb Mon Sep 17 00:00:00 2001 From: Kadir Cetinkaya Date: Wed, 22 May 2024 19:37:18 +0200 Subject: [PATCH] [clang][Sema] Fix crash when diagnosing candidates with parameter packs Prevent OOB access by not printing target parameter range when there's a pack in the function parameters. 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/docs/ReleaseNotes.rst | 3 ++- clang/lib/Sema/SemaOverload.cpp | 11 +-- clang/test/SemaCXX/overload-template.cpp | 10 ++ 3 files changed, 21 insertions(+), 3 deletions(-) diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst index 825e91876ffce..81b8d42aaa84e 100644 --- a/clang/docs/ReleaseNotes.rst +++ b/clang/docs/ReleaseNotes.rst @@ -734,7 +734,6 @@ Bug Fixes to C++ Support from being explicitly specialized for a given implicit instantiation of the class template. - Fixed a crash when ``this`` is used in a dependent class scope function template specialization that instantiates to a static member function. - - Fix crash when inheriting from a cv-qualified type. Fixes #GH35603 - Fix a crash when the using enum declaration uses an anonymous enumeration. Fixes (#GH86790). - Handled an edge case in ``getFullyPackExpandedSize`` so that we now avoid a false-positive diagnostic. (#GH84220) @@ -796,6 +795,8 @@ Bug Fixes to C++ Support Fixes (#GH91308). - Fix a crash caused by a regression in the handling of ``source_location`` in dependent contexts. Fixes (#GH92680). +- Fixed a crash when diagnosing failed conversions involving template parameter + packs. (#GH93076) Bug Fixes to AST Handling ^ 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 , 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..3277a17e5e450 100644 --- a/clang/test/SemaCXX/overload-template.cpp +++ b/clang/test/SemaCXX/overload-template.cpp @@ -58,3 +58,13 @@ namespace overloadCheck{ } } #endif + +namespace GH93076 { +template int b(a..., int); // expected-note-re 3 {{candidate function template not viable: no known conversion from 'int ()' to 'int' for {{.*}} argument}} +int d() { + (void)b(0, 0, d); // expected-error {{no matching function for call to 'b'}} + (void)b(0, d, 0); // expected-error {{no matching function for call to 'b'}} + (void)b(d, 0, 0); // expected-error {{no matching function for call to 'b'}} + return 0; + } +} ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang][Sema] Fix crash when diagnosing candidates with parameter packs (PR #93079)
https://github.com/mizvekov approved this pull request. 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. https://github.com/llvm/llvm-project/pull/93079 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang][Sema] Fix crash when diagnosing candidates with parameter packs (PR #93079)
https://github.com/mizvekov edited https://github.com/llvm/llvm-project/pull/93079 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang][Sema] Fix crash when diagnosing candidates with parameter packs (PR #93079)
@@ -11298,8 +11298,9 @@ static void DiagnoseBadConversion(Sema , 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(); mizvekov wrote: Okay. Should'nt be a problem to pipe in the specialization, but as I said, just leaving the FIXME is fine. https://github.com/llvm/llvm-project/pull/93079 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang][Sema] Fix crash when diagnosing candidates with parameter packs (PR #93079)
https://github.com/kadircet updated https://github.com/llvm/llvm-project/pull/93079 From 9c691ab41ba500c1962bf9d63de86b65f184f047 Mon Sep 17 00:00:00 2001 From: Kadir Cetinkaya Date: Wed, 22 May 2024 19:37:18 +0200 Subject: [PATCH] [clang][Sema] Fix crash when diagnosing candidates with parameter packs Prevent OOB access by not printing target parameter range when there's a pack in the function parameters. 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/docs/ReleaseNotes.rst | 3 ++- clang/lib/Sema/SemaOverload.cpp | 11 +-- clang/test/SemaCXX/overload-template.cpp | 3 +++ 3 files changed, 14 insertions(+), 3 deletions(-) diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst index 45676a02b760b..023af70572306 100644 --- a/clang/docs/ReleaseNotes.rst +++ b/clang/docs/ReleaseNotes.rst @@ -730,7 +730,6 @@ Bug Fixes to C++ Support from being explicitly specialized for a given implicit instantiation of the class template. - Fixed a crash when ``this`` is used in a dependent class scope function template specialization that instantiates to a static member function. - - Fix crash when inheriting from a cv-qualified type. Fixes #GH35603 - Fix a crash when the using enum declaration uses an anonymous enumeration. Fixes (#GH86790). - Handled an edge case in ``getFullyPackExpandedSize`` so that we now avoid a false-positive diagnostic. (#GH84220) @@ -790,6 +789,8 @@ Bug Fixes to C++ Support Fixes (#GH87210), (GH89541). - Clang no longer tries to check if an expression is immediate-escalating in an unevaluated context. Fixes (#GH91308). +- Fixed a crash when diagnosing failed conversions involving template parameter + packs. (#GH93076) Bug Fixes to AST Handling ^ 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 , 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 int b(a...); // expected-note {{candidate function template not viable: no known conversion from 'int ()' to 'int' for 2nd argument}} +int d() { return b(0, d); } // expected-error {{no matching function for call to 'b'}} ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang][Sema] Fix crash when diagnosing candidates with parameter packs (PR #93079)
kadircet wrote: @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 https://github.com/llvm/llvm-project/pull/70280 ? If so I am happy to discard this PR in favor of yours. https://github.com/llvm/llvm-project/pull/93079 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang][Sema] Fix crash when diagnosing candidates with parameter packs (PR #93079)
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 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 , 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 int b(a...); // expected-note {{candidate function template not viable: no known conversion from 'int ()' to 'int' for 2nd argument}} +int d() { return b(0, d); } // expected-error {{no matching function for call to 'b'}} ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang][Sema] Fix crash when diagnosing candidates with parameter packs (PR #93079)
github-actions[bot] wrote: :warning: C/C++ code formatter, clang-format found issues in your code. :warning: You can test this locally with the following command: ``bash git-clang-format --diff a79a0c52885c3a60d6afdda3b125866b8ed75fce 7c5716a726fe0c4a2a3e0ddfe8f992491bd0299d -- clang/lib/Sema/SemaOverload.cpp clang/test/SemaCXX/overload-template.cpp `` View the diff from clang-format here. ``diff diff --git a/clang/lib/Sema/SemaOverload.cpp b/clang/lib/Sema/SemaOverload.cpp index 86e869c7c7..61d3c1633a 100644 --- a/clang/lib/Sema/SemaOverload.cpp +++ b/clang/lib/Sema/SemaOverload.cpp @@ -11306,8 +11306,10 @@ static void DiagnoseBadConversion(Sema , OverloadCandidate *Cand, // 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(); }); + bool HasParamPack = + llvm::any_of(Fn->parameters().take_front(I), [](const ParmVarDecl *Parm) { +return Parm->isParameterPack(); + }); if (!isObjectArgument && !HasParamPack) ToParamRange = Fn->getParamDecl(I)->getSourceRange(); `` https://github.com/llvm/llvm-project/pull/93079 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang][Sema] Fix crash when diagnosing candidates with parameter packs (PR #93079)
https://github.com/kadircet updated https://github.com/llvm/llvm-project/pull/93079 From f7bdd39714e21ff31b3c5aa6a3a18967cb6fef2c Mon Sep 17 00:00:00 2001 From: Kadir Cetinkaya Date: Wed, 22 May 2024 19:37:18 +0200 Subject: [PATCH 1/2] [clang][Sema] Fix crash when diagnosing candidates with parameter packs Prevent OOB access. Fixes https://github.com/llvm/llvm-project/issues/93076 --- clang/lib/Sema/SemaOverload.cpp | 5 +++-- clang/test/SemaCXX/overload-template.cpp | 3 +++ 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/clang/lib/Sema/SemaOverload.cpp b/clang/lib/Sema/SemaOverload.cpp index 0c89fca8d38eb..7465d6d96c20f 100644 --- a/clang/lib/Sema/SemaOverload.cpp +++ b/clang/lib/Sema/SemaOverload.cpp @@ -11301,8 +11301,9 @@ static void DiagnoseBadConversion(Sema , 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 int b(a...); // expected-note {{candidate function template not viable: no known conversion from 'int ()' to 'int' for 2nd argument}} +int d() { return b(0, d); } // expected-error {{no matching function for call to 'b'}} From 7c5716a726fe0c4a2a3e0ddfe8f992491bd0299d Mon Sep 17 00:00:00 2001 From: Kadir Cetinkaya Date: Fri, 24 May 2024 10:25:25 +0200 Subject: [PATCH 2/2] add fixme and improve range handling --- clang/lib/Sema/SemaOverload.cpp | 8 +++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/clang/lib/Sema/SemaOverload.cpp b/clang/lib/Sema/SemaOverload.cpp index 7465d6d96c20f..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" @@ -11302,7 +11303,12 @@ static void DiagnoseBadConversion(Sema , OverloadCandidate *Cand, QualType FromTy = Conv.Bad.getFromType(); QualType ToTy = Conv.Bad.getToType(); SourceRange ToParamRange; - if (!isObjectArgument && I < Fn->getNumParams()) + + // 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) { ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang][Sema] Fix crash when diagnosing candidates with parameter packs (PR #93079)
@@ -11298,8 +11298,9 @@ static void DiagnoseBadConversion(Sema , 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(); kadircet wrote: > Though I don't think solving this is complicated sorry I guess I didn't convey full context in my previous message when I just said "it requires propagating extra information in the bad conversion about the parameter location". `Fn` in this context is the template decl itself, hence the packs in the parameters are dependent. hence we can't simply ask how many expansions are there. https://github.com/llvm/llvm-project/pull/93079 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang][Sema] Fix crash when diagnosing candidates with parameter packs (PR #93079)
https://github.com/mizvekov edited https://github.com/llvm/llvm-project/pull/93079 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang][Sema] Fix crash when diagnosing candidates with parameter packs (PR #93079)
@@ -11298,8 +11298,9 @@ static void DiagnoseBadConversion(Sema , 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(); mizvekov wrote: Sure, fixing the crash is important, but in that case I would leave a FIXME and keep the bug open, with updated information. It's your call. Though I don't think solving this is complicated: Just a linear scan on the parameter list: * A parameter which is a pack consumes `cast(Fn->getParamDecl(I)->getType())->getNumExpansions()` arguments, which can possibly be zero arguments. It should not be possible to find an unexpanded pack in this diagnostic. * A parameter which is not a pack consumes one argument. https://github.com/llvm/llvm-project/pull/93079 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang][Sema] Fix crash when diagnosing candidates with parameter packs (PR #93079)
https://github.com/mizvekov requested changes to this pull request. https://github.com/llvm/llvm-project/pull/93079 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang][Sema] Fix crash when diagnosing candidates with parameter packs (PR #93079)
@@ -11298,8 +11298,9 @@ static void DiagnoseBadConversion(Sema , 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(); kadircet wrote: that's a fair point, I was mostly operating with the assumption of "parameter pack is always the last parameter", but that isn't necessarily true. as you pointed out this patch prevents the OOB access, and won't necessarily do the "right" thing in terms of pointing at the failed conversion. but this patch is still an improvement to today's case of just crashing even before printing the candidate. I think adjusting the parameter range to be accurate in presence of parameter packs is more of a feature request at this point. since (AFAICT) it requires propagating extra information in the bad conversion about the parameter location. so I'd rather move forward with this patch, while filing a feature request for the "ideal" state. WDYT? https://github.com/llvm/llvm-project/pull/93079 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang][Sema] Fix crash when diagnosing candidates with parameter packs (PR #93079)
@@ -11298,8 +11298,9 @@ static void DiagnoseBadConversion(Sema , 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(); mizvekov wrote: I mean, I would expect the patch would make it so it doesn't crash, but does the diagnostic point to the correct parameter? https://github.com/llvm/llvm-project/pull/93079 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang][Sema] Fix crash when diagnosing candidates with parameter packs (PR #93079)
@@ -11298,8 +11298,9 @@ static void DiagnoseBadConversion(Sema , 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(); mizvekov wrote: It seems like the problem here may be that `I` is an index into an argument-as-written list, which doesn't take into account that mapping into the parameter list needs to consider packs. I think this could do the wrong thing if there are other parameters after the pack. How does your patch handle the following example? ```C++ template int b(a..., int); int d() { return b(0, 0, d); } ``` https://github.com/llvm/llvm-project/pull/93079 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang][Sema] Fix crash when diagnosing candidates with parameter packs (PR #93079)
shafik wrote: Can you also confirm this fixes: https://github.com/llvm/llvm-project/issues/70191 https://github.com/llvm/llvm-project/pull/93079 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang][Sema] Fix crash when diagnosing candidates with parameter packs (PR #93079)
https://github.com/shafik approved this pull request. 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: https://github.com/llvm/llvm-project/issues/76354 https://github.com/llvm/llvm-project/pull/93079 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang][Sema] Fix crash when diagnosing candidates with parameter packs (PR #93079)
llvmbot wrote: @llvm/pr-subscribers-clang Author: kadir çetinkaya (kadircet) Changes Prevent OOB access. Fixes https://github.com/llvm/llvm-project/issues/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 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 , 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 int b(a...); // expected-note {{candidate function template not viable: no known conversion from 'int ()' to 'int' for 2nd argument}} +int d() { return b(0, d); } // expected-error {{no matching function for call to 'b'}} `` https://github.com/llvm/llvm-project/pull/93079 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang][Sema] Fix crash when diagnosing candidates with parameter packs (PR #93079)
https://github.com/kadircet created https://github.com/llvm/llvm-project/pull/93079 Prevent OOB access. Fixes https://github.com/llvm/llvm-project/issues/93076 From 7840ea2b16863ee7057f3b1239c59b6be06cbd42 Mon Sep 17 00:00:00 2001 From: Kadir Cetinkaya 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 --- clang/lib/Sema/SemaOverload.cpp | 5 +++-- clang/test/SemaCXX/overload-template.cpp | 3 +++ 2 files changed, 6 insertions(+), 2 deletions(-) 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 , 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 int b(a...); // expected-note {{candidate function template not viable: no known conversion from 'int ()' to 'int' for 2nd argument}} +int d() { return b(0, d); } // expected-error {{no matching function for call to 'b'}} ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits