Skip to content

Commit 71478ec

Browse files
authored
[WebKit Checkers] Treat const Objective-C ivar as a safe origin (#126353)
Like const C++ member variables, treat const Ref, RefPtr, CheckedRef, CheckedPtr Objective-C ivars as a safe pointer origin in WebKit checkers.
1 parent 918848d commit 71478ec

File tree

2 files changed

+16
-6
lines changed

2 files changed

+16
-6
lines changed

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -163,10 +163,11 @@ bool isConstOwnerPtrMemberExpr(const clang::Expr *E) {
163163
if (OCE->getOperator() == OO_Star && OCE->getNumArgs() == 1)
164164
E = OCE->getArg(0);
165165
}
166-
auto *ME = dyn_cast<MemberExpr>(E);
167-
if (!ME)
168-
return false;
169-
auto *D = ME->getMemberDecl();
166+
const ValueDecl *D = nullptr;
167+
if (auto *ME = dyn_cast<MemberExpr>(E))
168+
D = ME->getMemberDecl();
169+
else if (auto *IVR = dyn_cast<ObjCIvarRefExpr>(E))
170+
D = IVR->getDecl();
170171
if (!D)
171172
return false;
172173
auto T = D->getType();

clang/test/Analysis/Checkers/WebKit/uncounted-obj-arg.mm

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
// RUN: %clang_analyze_cc1 -analyzer-checker=alpha.webkit.UncountedCallArgsChecker -verify %s
2-
// expected-no-diagnostics
32

43
#import "mock-types.h"
54
#import "mock-system-header.h"
65
#import "../../Inputs/system-header-simulator-for-objc-dealloc.h"
76

8-
@interface Foo : NSObject
7+
@interface Foo : NSObject {
8+
const Ref<RefCountable> _obj1;
9+
const RefPtr<RefCountable> _obj2;
10+
Ref<RefCountable> _obj3;
11+
}
912

1013
@property (nonatomic, readonly) RefPtr<RefCountable> countable;
1114

@@ -17,6 +20,11 @@ @implementation Foo
1720

1821
- (void)execute {
1922
self._protectedRefCountable->method();
23+
_obj1->method();
24+
_obj1.get().method();
25+
(*_obj2).method();
26+
_obj3->method();
27+
// expected-warning@-1{{Call argument for 'this' parameter is uncounted and unsafe}}
2028
}
2129

2230
- (RefPtr<RefCountable>)_protectedRefCountable {
@@ -30,6 +38,7 @@ - (void)execute {
3038
void ref() const;
3139
void deref() const;
3240
Ref<RefCountedObject> copy() const;
41+
void method();
3342
};
3443

3544
@interface WrapperObj : NSObject

0 commit comments

Comments
 (0)