Skip to content

Commit d462aa5

Browse files
committed
[clang] Fix a nullptr dereference bug on invalid code
When working with invalid code, we would try to dereference a nullptr while deducing template arguments in some dependend code operating on a lambda with invalid return type. Differential Revision: https://reviews.llvm.org/D95145
1 parent 68eee55 commit d462aa5

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

clang/lib/Sema/SemaTemplateInstantiateDecl.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4189,6 +4189,9 @@ TemplateDeclInstantiator::SubstFunctionType(FunctionDecl *D,
41894189
for (unsigned OldIdx = 0, NumOldParams = OldProtoLoc.getNumParams();
41904190
OldIdx != NumOldParams; ++OldIdx) {
41914191
ParmVarDecl *OldParam = OldProtoLoc.getParam(OldIdx);
4192+
if (!OldParam)
4193+
return nullptr;
4194+
41924195
LocalInstantiationScope *Scope = SemaRef.CurrentInstantiationScope;
41934196

41944197
Optional<unsigned> NumArgumentsInExpansion;
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// RUN: %clang -fsyntax-only -std=c++17 %s -Xclang -verify
2+
3+
// The important part is that we do not crash.
4+
5+
template<typename T> T declval();
6+
7+
template <typename T>
8+
auto Call(T x) -> decltype(declval<T>()(0)) {} // expected-note{{candidate template ignored}}
9+
10+
class Status {};
11+
12+
void fun() {
13+
// The Status() (instead of Status) here used to cause a crash.
14+
Call([](auto x) -> Status() {}); // expected-error{{function cannot return function type 'Status ()}}
15+
// expected-error@-1{{no matching function for call to 'Call'}}
16+
}

0 commit comments

Comments
 (0)