r358134 - Check i < FD->getNumParams() before querying

Dmitri Gribenko via cfe-commits cfe-commits at lists.llvm.org
Wed Apr 10 13:25:07 PDT 2019


Author: gribozavr
Date: Wed Apr 10 13:25:07 2019
New Revision: 358134

URL: http://llvm.org/viewvc/llvm-project?rev=358134&view=rev
Log:
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.

Added:
    cfe/trunk/test/SemaCXX/PR41139.cpp
Modified:
    cfe/trunk/lib/Sema/SemaTemplateInstantiate.cpp
    cfe/trunk/test/SemaCXX/cxx1y-generic-lambdas.cpp

Modified: cfe/trunk/lib/Sema/SemaTemplateInstantiate.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaTemplateInstantiate.cpp?rev=358134&r1=358133&r2=358134&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaTemplateInstantiate.cpp (original)
+++ cfe/trunk/lib/Sema/SemaTemplateInstantiate.cpp Wed Apr 10 13:25:07 2019
@@ -2892,7 +2892,7 @@ static const Decl *getCanonicalParmVarDe
       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);
     }
   }

Added: cfe/trunk/test/SemaCXX/PR41139.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/PR41139.cpp?rev=358134&view=auto
==============================================================================
--- cfe/trunk/test/SemaCXX/PR41139.cpp (added)
+++ cfe/trunk/test/SemaCXX/PR41139.cpp Wed Apr 10 13:25:07 2019
@@ -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 );
+}

Modified: cfe/trunk/test/SemaCXX/cxx1y-generic-lambdas.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/cxx1y-generic-lambdas.cpp?rev=358134&r1=358133&r2=358134&view=diff
==============================================================================
--- cfe/trunk/test/SemaCXX/cxx1y-generic-lambdas.cpp (original)
+++ cfe/trunk/test/SemaCXX/cxx1y-generic-lambdas.cpp Wed Apr 10 13:25:07 2019
@@ -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) {




More information about the cfe-commits mailing list