Skip to content

[WebKit checkers] Treat global const variables as safe #136170

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions clang/lib/StaticAnalyzer/Checkers/WebKit/ASTUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,11 @@ bool tryToFindPtrOrigin(
std::function<bool(const clang::Expr *, bool)> callback) {
while (E) {
if (auto *DRE = dyn_cast<DeclRefExpr>(E)) {
auto *ValDecl = DRE->getDecl();
auto QT = ValDecl->getType();
auto ValName = ValDecl->getName();
if (ValDecl && (ValName.starts_with('k') || ValName.starts_with("_k")) &&
QT.isConstQualified()) { // Treat constants such as kCF* as safe.
return callback(E, true);
if (auto *VD = dyn_cast_or_null<VarDecl>(DRE->getDecl())) {
auto QT = VD->getType();
if (VD->hasGlobalStorage() && QT.isConstQualified()) {
return callback(E, true);
}
}
}
if (auto *tempExpr = dyn_cast<MaterializeTemporaryExpr>(E)) {
Expand Down
18 changes: 18 additions & 0 deletions clang/test/Analysis/Checkers/WebKit/unretained-call-args-arc.mm
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,24 @@ void foo() {

} // namespace raw_ptr

namespace const_global {

extern NSString * const SomeConstant;
extern CFDictionaryRef const SomeDictionary;
void doWork(NSString *str, CFDictionaryRef dict);
void use_const_global() {
doWork(SomeConstant, SomeDictionary);
}

NSString *provide_str();
CFDictionaryRef provide_dict();
void use_const_local() {
doWork(provide_str(), provide_dict());
// expected-warning@-1{{Call argument for parameter 'dict' is unretained and unsafe}}
}

} // namespace const_global

@interface AnotherObj : NSObject
- (void)foo:(SomeObj *)obj;
- (SomeObj *)getSomeObj;
Expand Down
19 changes: 19 additions & 0 deletions clang/test/Analysis/Checkers/WebKit/unretained-call-args.mm
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,25 @@ void idcf(CFTypeRef obj) {

} // ptr_conversion

namespace const_global {

extern NSString * const SomeConstant;
extern CFDictionaryRef const SomeDictionary;
void doWork(NSString *str, CFDictionaryRef dict);
void use_const_global() {
doWork(SomeConstant, SomeDictionary);
}

NSString *provide_str();
CFDictionaryRef provide_dict();
void use_const_local() {
doWork(provide_str(), provide_dict());
// expected-warning@-1{{Call argument for parameter 'str' is unretained and unsafe}}
// expected-warning@-2{{Call argument for parameter 'dict' is unretained and unsafe}}
}

} // namespace const_global

@interface TestObject : NSObject
- (void)doWork:(NSString *)msg, ...;
- (void)doWorkOnSelf;
Expand Down
20 changes: 20 additions & 0 deletions clang/test/Analysis/Checkers/WebKit/unretained-local-vars-arc.mm
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,26 @@ void bar() {

} // namespace raw_ptr

namespace const_global {

extern NSString * const SomeConstant;
extern CFDictionaryRef const SomeDictionary;
void doWork(NSString *, CFDictionaryRef);
void use_const_global() {
doWork(SomeConstant, SomeDictionary);
}

NSString *provide_str();
CFDictionaryRef provide_dict();
void use_const_local() {
NSString * const str = provide_str();
CFDictionaryRef dict = provide_dict();
// expected-warning@-1{{Local variable 'dict' is unretained and unsafe [alpha.webkit.UnretainedLocalVarsChecker]}}
doWork(str, dict);
}

} // namespace const_global

@interface AnotherObj : NSObject
- (void)foo:(SomeObj *)obj;
@end
Expand Down
21 changes: 21 additions & 0 deletions clang/test/Analysis/Checkers/WebKit/unretained-local-vars.mm
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,27 @@ unsigned ccf(CFTypeRef obj) {

} // ptr_conversion

namespace const_global {

extern NSString * const SomeConstant;
extern CFDictionaryRef const SomeDictionary;
void doWork(NSString *, CFDictionaryRef);
void use_const_global() {
doWork(SomeConstant, SomeDictionary);
}

NSString *provide_str();
CFDictionaryRef provide_dict();
void use_const_local() {
NSString * const str = provide_str();
// expected-warning@-1{{Local variable 'str' is unretained and unsafe [alpha.webkit.UnretainedLocalVarsChecker]}}
CFDictionaryRef dict = provide_dict();
// expected-warning@-1{{Local variable 'dict' is unretained and unsafe [alpha.webkit.UnretainedLocalVarsChecker]}}
doWork(str, dict);
}

} // namespace const_global

bool doMoreWorkOpaque(OtherObj*);
SomeObj* provide();

Expand Down
Loading