Skip to content

Commit e31e59b

Browse files
committed
Treat invocations of copy & isEqual selectors as safe.
1 parent 21dd8e6 commit e31e59b

File tree

5 files changed

+21
-1
lines changed

5 files changed

+21
-1
lines changed

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,6 @@ class RawPtrRefCallArgsChecker
190190
if (auto *InnerMsg =
191191
dyn_cast<ObjCMessageExpr>(Receiver->IgnoreParenCasts())) {
192192
auto InnerSelector = InnerMsg->getSelector();
193-
auto SelName = Selector.getNameForSlot(0);
194193
if (InnerSelector.getNameForSlot(0).starts_with("alloc") &&
195194
(SelName.starts_with("init") || SelName.starts_with("_init")))
196195
return;

clang/test/Analysis/Checkers/WebKit/objc-mock-types.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,8 @@ __attribute__((objc_root_class))
160160
- ( const char *)UTF8String;
161161
- (id)initWithUTF8String:(const char *)nullTerminatedCString;
162162
- (NSString *)copy;
163+
- (NSString *)mutableCopy;
164+
- (BOOL)isEqualToString:(NSString *)aString;
163165
+ (id)stringWithUTF8String:(const char *)nullTerminatedCString;
164166
@end
165167

@@ -198,6 +200,7 @@ extern NSApplication * NSApp;
198200
+ (SomeObj *)sharedInstance;
199201
- (instancetype)_init;
200202
- (SomeObj *)mutableCopy;
203+
- (BOOL)isEqual:(SomeObj *)other;
201204
- (SomeObj *)copyWithValue:(int)value;
202205
- (void)doWork;
203206
- (SomeObj *)other;

clang/test/Analysis/Checkers/WebKit/retain-ptr-ctor-adopt-use-arc.mm

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ @implementation SomeObj {
4545
SomeObj *_other;
4646
}
4747

48+
+ (SomeObj *)sharedInstance {
49+
return nil;
50+
}
51+
4852
- (instancetype)_init {
4953
self = [super init];
5054
_number = nil;
@@ -61,6 +65,10 @@ - (SomeObj *)mutableCopy {
6165
return copy;
6266
}
6367

68+
- (BOOL)isEqual:(SomeObj *)other {
69+
return self.value == other.value && self.next == other.next && _other == other.other;
70+
}
71+
6472
- (SomeObj *)copyWithValue:(int)value {
6573
auto *copy = [[SomeObj alloc] init];
6674
[copy setValue:_number];

clang/test/Analysis/Checkers/WebKit/retain-ptr-ctor-adopt-use.mm

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,10 @@ - (SomeObj *)mutableCopy {
7474
return copy;
7575
}
7676

77+
- (BOOL)isEqual:(SomeObj *)other {
78+
return self.value == other.value && self.next == other.next && _other == other.other;
79+
}
80+
7781
- (SomeObj *)copyWithValue:(int)value {
7882
auto *copy = [[SomeObj alloc] init];
7983
// expected-warning@-1{{The return value is +1 and results in a memory leak [alpha.webkit.RetainPtrCtorAdoptChecker]}}

clang/test/Analysis/Checkers/WebKit/unretained-call-args.mm

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
SomeObj *provide();
77
void consume_obj(SomeObj*);
88

9+
NSString *provide_str();
10+
911
CFMutableArrayRef provide_cf();
1012
void consume_cf(CFMutableArrayRef);
1113

@@ -610,6 +612,10 @@ - (void)doWorkOnSelf {
610612
[self doWork:__null];
611613
[self doWork:nil];
612614
[NSApp run];
615+
[provide() isEqual:provide()];
616+
[provide_str() isEqualToString:@"foo"];
617+
[provide_str() copyWithZone:nullptr];
618+
[provide_str() mutableCopy];
613619
}
614620

615621
- (SomeObj *)getSomeObj {

0 commit comments

Comments
 (0)