Skip to content

Commit

Permalink
[WebKit Checkers] Treat const Objective-C ivar as a safe origin
Browse files Browse the repository at this point in the history
Like const C++ member variables, treat const Ref, RefPtr, CheckedRef, CheckedPtr
Objective-C ivars as a safe pointer origin in WebKit checkers.
  • Loading branch information
rniwa committed Feb 8, 2025
1 parent 4d3148d commit b3d79dd
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
9 changes: 6 additions & 3 deletions clang/lib/StaticAnalyzer/Checkers/WebKit/ASTUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -163,10 +163,13 @@ bool isConstOwnerPtrMemberExpr(const clang::Expr *E) {
if (OCE->getOperator() == OO_Star && OCE->getNumArgs() == 1)
E = OCE->getArg(0);
}
auto *ME = dyn_cast<MemberExpr>(E);
if (!ME)
const ValueDecl *D = nullptr;
if (auto *ME = dyn_cast<MemberExpr>(E))
D = ME->getMemberDecl();
else if (auto *IVR = dyn_cast<ObjCIvarRefExpr>(E))
D = IVR->getDecl();
else
return false;
auto *D = ME->getMemberDecl();
if (!D)
return false;
auto T = D->getType();
Expand Down
9 changes: 8 additions & 1 deletion clang/test/Analysis/Checkers/WebKit/uncounted-obj-arg.mm
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
#import "mock-system-header.h"
#import "../../Inputs/system-header-simulator-for-objc-dealloc.h"

@interface Foo : NSObject
@interface Foo : NSObject {
const Ref<RefCountable> _obj1;
const RefPtr<RefCountable> _obj2;
}

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

Expand All @@ -17,6 +20,9 @@ @implementation Foo

- (void)execute {
self._protectedRefCountable->method();
_obj1->method();
_obj1.get().method();
(*_obj2).method();
}

- (RefPtr<RefCountable>)_protectedRefCountable {
Expand All @@ -30,6 +36,7 @@ - (void)execute {
void ref() const;
void deref() const;
Ref<RefCountedObject> copy() const;
void method();
};

@interface WrapperObj : NSObject
Expand Down

0 comments on commit b3d79dd

Please sign in to comment.