Skip to content

Commit c17df0a

Browse files
authored
[webkit.UncountedLambdaCapturesChecker] Fix a crash in declProtectsThis (#127309)
Add a missing nullptr check to declProtectsThis.
1 parent e60de25 commit c17df0a

File tree

2 files changed

+43
-2
lines changed

2 files changed

+43
-2
lines changed

clang/lib/StaticAnalyzer/Checkers/WebKit/UncountedLambdaCapturesChecker.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,8 @@ class UncountedLambdaCapturesChecker
267267
auto OpCode = OpCE->getOperator();
268268
if (OpCode == OO_Star || OpCode == OO_Amp) {
269269
auto *Callee = OpCE->getDirectCallee();
270+
if (!Callee)
271+
return false;
270272
auto clsName = safeGetName(Callee->getParent());
271273
if (!isRefType(clsName) || !OpCE->getNumArgs())
272274
return false;
@@ -276,9 +278,10 @@ class UncountedLambdaCapturesChecker
276278
}
277279
if (auto *UO = dyn_cast<UnaryOperator>(Arg)) {
278280
auto OpCode = UO->getOpcode();
279-
if (OpCode == UO_Deref || OpCode == UO_AddrOf)
281+
if (OpCode == UO_Deref || OpCode == UO_AddrOf) {
280282
Arg = UO->getSubExpr()->IgnoreParenCasts();
281-
continue;
283+
continue;
284+
}
282285
}
283286
break;
284287
} while (Arg);
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// RUN: %clang_analyze_cc1 -analyzer-checker=webkit.UncountedLambdaCapturesChecker -verify %s
2+
3+
struct Foo {
4+
int x;
5+
int y;
6+
Foo(int x, int y) : x(x) , y(y) { }
7+
};
8+
9+
template <typename T>
10+
struct Baz {
11+
void ref() const;
12+
void deref() const;
13+
Foo operator*();
14+
bool operator!();
15+
};
16+
17+
inline Foo operator*(const Foo& a, const Foo& b);
18+
19+
Baz<Foo> someFunction();
20+
template <typename CallbackType> void bar(CallbackType callback) {
21+
auto baz = someFunction();
22+
callback(baz);
23+
}
24+
25+
struct Obj {
26+
void ref() const;
27+
void deref() const;
28+
29+
void foo(Foo foo) {
30+
bar([this](auto baz) {
31+
// expected-warning@-1{{Captured raw-pointer 'this' to ref-counted type or CheckedPtr-capable type is unsafe [webkit.UncountedLambdaCapturesChecker]}}
32+
bar([this, foo = *baz, foo2 = !baz](auto&&) {
33+
// expected-warning@-1{{Captured raw-pointer 'this' to ref-counted type or CheckedPtr-capable type is unsafe [webkit.UncountedLambdaCapturesChecker]}}
34+
someFunction();
35+
});
36+
});
37+
}
38+
};

0 commit comments

Comments
 (0)