Skip to content

Commit

Permalink
Check i < FD->getNumParams() before querying
Browse files Browse the repository at this point in the history
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
  • Loading branch information
gribozavr committed Apr 10, 2019
1 parent 6644d01 commit 66b6bb1
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
2 changes: 1 addition & 1 deletion clang/lib/Sema/SemaTemplateInstantiate.cpp
Expand Up @@ -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);
}
}
Expand Down
15 changes: 15 additions & 0 deletions clang/test/SemaCXX/PR41139.cpp
@@ -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 );
}
9 changes: 9 additions & 0 deletions clang/test/SemaCXX/cxx1y-generic-lambdas.cpp
Expand Up @@ -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) {
Expand Down

0 comments on commit 66b6bb1

Please sign in to comment.