Skip to content

Commit 5d7e8ac

Browse files
authored
[webkit.UncountedLambdaCapturesChecker] Treat a copy capture of a CheckedPtr object as safe (#138068)
Allow copy capture of a reference to a CheckedPtr capable object since such a capture will copy the said object instead of keeping a dangling reference to the object.
1 parent 90d8e4d commit 5d7e8ac

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,9 @@ class RawPtrRefLambdaCapturesChecker
381381
}
382382
QualType CapturedVarQualType = CapturedVar->getType();
383383
auto IsUncountedPtr = isUnsafePtr(CapturedVar->getType());
384+
if (C.getCaptureKind() == LCK_ByCopy &&
385+
CapturedVarQualType->isReferenceType())
386+
continue;
384387
if (IsUncountedPtr && *IsUncountedPtr)
385388
reportBug(C, CapturedVar, CapturedVarQualType, L);
386389
} else if (C.capturesThis() && shouldCheckThis) {

clang/test/Analysis/Checkers/WebKit/uncounted-lambda-captures.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,13 +137,11 @@ void references() {
137137
RefCountable automatic;
138138
RefCountable& ref_countable_ref = automatic;
139139
auto foo1 = [ref_countable_ref](){ ref_countable_ref.constMethod(); };
140-
// expected-warning@-1{{Captured reference 'ref_countable_ref' to uncounted type is unsafe [webkit.UncountedLambdaCapturesChecker]}}
141140
auto foo2 = [&ref_countable_ref](){ ref_countable_ref.method(); };
142141
// expected-warning@-1{{Captured reference 'ref_countable_ref' to uncounted type is unsafe [webkit.UncountedLambdaCapturesChecker]}}
143142
auto foo3 = [&](){ ref_countable_ref.method(); };
144143
// expected-warning@-1{{Implicitly captured reference 'ref_countable_ref' to uncounted type is unsafe [webkit.UncountedLambdaCapturesChecker]}}
145144
auto foo4 = [=](){ ref_countable_ref.constMethod(); };
146-
// expected-warning@-1{{Implicitly captured reference 'ref_countable_ref' to uncounted type is unsafe [webkit.UncountedLambdaCapturesChecker]}}
147145

148146
call(foo1);
149147
call(foo2);
@@ -407,3 +405,14 @@ void lambda_converted_to_function(RefCountable* obj)
407405
// expected-warning@-1{{Implicitly captured raw-pointer 'obj' to uncounted type is unsafe [webkit.UncountedLambdaCapturesChecker]}}
408406
});
409407
}
408+
409+
void capture_copy_in_lambda(CheckedObj& checked) {
410+
callFunctionOpaque([checked]() mutable {
411+
checked.method();
412+
});
413+
auto* ptr = &checked;
414+
callFunctionOpaque([ptr]() mutable {
415+
// expected-warning@-1{{Captured raw-pointer 'ptr' to uncounted type is unsafe [webkit.UncountedLambdaCapturesChecker]}}
416+
ptr->method();
417+
});
418+
}

0 commit comments

Comments
 (0)