Skip to content

Commit 0e4fb17

Browse files
authored
[WebKit checkers] Add support for ns_returns_autoreleased (#161236)
Recognize ns_returns_autoreleased on a function and treat its return value as a safe pointer origin.
1 parent e34b71e commit 0e4fb17

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,8 @@ bool tryToFindPtrOrigin(
9393
if (auto *call = dyn_cast<CallExpr>(E)) {
9494
if (auto *Callee = call->getCalleeDecl()) {
9595
if (Callee->hasAttr<CFReturnsRetainedAttr>() ||
96-
Callee->hasAttr<NSReturnsRetainedAttr>()) {
96+
Callee->hasAttr<NSReturnsRetainedAttr>() ||
97+
Callee->hasAttr<NSReturnsAutoreleasedAttr>()) {
9798
return callback(E, true);
9899
}
99100
}

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -567,6 +567,17 @@ void foo() {
567567

568568
} // namespace ns_retained_return_value
569569

570+
namespace autoreleased {
571+
572+
NSString *provideAutoreleased() __attribute__((ns_returns_autoreleased));
573+
void consume(NSString *);
574+
575+
void foo() {
576+
consume(provideAutoreleased());
577+
}
578+
579+
} // autoreleased
580+
570581
@interface TestObject : NSObject
571582
- (void)doWork:(NSString *)msg, ...;
572583
- (void)doWorkOnSelf;

clang/test/Analysis/Checkers/WebKit/unretained-local-vars.mm

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -535,6 +535,18 @@ unsigned foo() {
535535

536536
} // namespace ns_retained_return_value
537537

538+
namespace autoreleased {
539+
540+
NSString *provideAutoreleased() __attribute__((ns_returns_autoreleased));
541+
void consume(NSString *);
542+
543+
void foo() {
544+
auto *string = provideAutoreleased();
545+
consume(string);
546+
}
547+
548+
} // autoreleased
549+
538550
bool doMoreWorkOpaque(OtherObj*);
539551
SomeObj* provide();
540552

0 commit comments

Comments
 (0)