Check i < FD->getNumParams() before querying

Summary:
As was already stated in a previous comment, the parameter isn't
necessarily referring to one of the DeclContext's parameter. We
should check the index is within the range to avoid out-of-boundary
access.

Reviewers: gribozavr, rsmith, lebedev.ri

Reviewed By: gribozavr, rsmith

Subscribers: lebedev.ri, cfe-commits

Tags: #clang

Differential Revision: https://reviews.llvm.org/D60055

Patch by Violet.

llvm-svn: 358134
This commit is contained in:
Dmitri Gribenko 2019-04-10 20:25:07 +00:00
parent 6644d014dd
commit 66b6bb1766
3 changed files with 25 additions and 1 deletions

View File

@ -2892,7 +2892,7 @@ static const Decl *getCanonicalParmVarDecl(const Decl *D) {
unsigned i = PV->getFunctionScopeIndex();
// This parameter might be from a freestanding function type within the
// function and isn't necessarily referring to one of FD's parameters.
if (FD->getParamDecl(i) == PV)
if (i < FD->getNumParams() && FD->getParamDecl(i) == PV)
return FD->getCanonicalDecl()->getParamDecl(i);
}
}

View File

@ -0,0 +1,15 @@
// RUN: %clang_cc1 -fsyntax-only -std=c++17 -verify %s
// expected-no-diagnostics
// This test should not crash.
int f1( unsigned ) { return 0; }
template <class R, class... Args>
struct S1 {
S1( R(*f)(Args...) ) {}
};
int main() {
S1 s1( f1 );
}

View File

@ -944,6 +944,15 @@ namespace PR22117 {
}(0)(0);
}
namespace PR41139 {
int y = [](auto outer) {
return [](auto inner) {
using T = int(decltype(outer), decltype(inner));
return 0;
};
}(0)(0);
}
namespace PR23716 {
template<typename T>
auto f(T x) {