Skip to content

Commit 8ca8c40

Browse files
authored
[WebKit checkers] Treat global const variables as safe (#136170)
This PR makes WebKit checkers treat a variable with global storage as safe instead of constraining to ones that start with k or _k.
1 parent a7dcedc commit 8ca8c40

File tree

5 files changed

+83
-6
lines changed

5 files changed

+83
-6
lines changed

Diff for: clang/lib/StaticAnalyzer/Checkers/WebKit/ASTUtils.cpp

+5-6
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,11 @@ bool tryToFindPtrOrigin(
3030
std::function<bool(const clang::Expr *, bool)> callback) {
3131
while (E) {
3232
if (auto *DRE = dyn_cast<DeclRefExpr>(E)) {
33-
auto *ValDecl = DRE->getDecl();
34-
auto QT = ValDecl->getType();
35-
auto ValName = ValDecl->getName();
36-
if (ValDecl && (ValName.starts_with('k') || ValName.starts_with("_k")) &&
37-
QT.isConstQualified()) { // Treat constants such as kCF* as safe.
38-
return callback(E, true);
33+
if (auto *VD = dyn_cast_or_null<VarDecl>(DRE->getDecl())) {
34+
auto QT = VD->getType();
35+
if (VD->hasGlobalStorage() && QT.isConstQualified()) {
36+
return callback(E, true);
37+
}
3938
}
4039
}
4140
if (auto *tempExpr = dyn_cast<MaterializeTemporaryExpr>(E)) {

Diff for: clang/test/Analysis/Checkers/WebKit/unretained-call-args-arc.mm

+18
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,24 @@ void foo() {
1818

1919
} // namespace raw_ptr
2020

21+
namespace const_global {
22+
23+
extern NSString * const SomeConstant;
24+
extern CFDictionaryRef const SomeDictionary;
25+
void doWork(NSString *str, CFDictionaryRef dict);
26+
void use_const_global() {
27+
doWork(SomeConstant, SomeDictionary);
28+
}
29+
30+
NSString *provide_str();
31+
CFDictionaryRef provide_dict();
32+
void use_const_local() {
33+
doWork(provide_str(), provide_dict());
34+
// expected-warning@-1{{Call argument for parameter 'dict' is unretained and unsafe}}
35+
}
36+
37+
} // namespace const_global
38+
2139
@interface AnotherObj : NSObject
2240
- (void)foo:(SomeObj *)obj;
2341
- (SomeObj *)getSomeObj;

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

+19
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,25 @@ void idcf(CFTypeRef obj) {
415415

416416
} // ptr_conversion
417417

418+
namespace const_global {
419+
420+
extern NSString * const SomeConstant;
421+
extern CFDictionaryRef const SomeDictionary;
422+
void doWork(NSString *str, CFDictionaryRef dict);
423+
void use_const_global() {
424+
doWork(SomeConstant, SomeDictionary);
425+
}
426+
427+
NSString *provide_str();
428+
CFDictionaryRef provide_dict();
429+
void use_const_local() {
430+
doWork(provide_str(), provide_dict());
431+
// expected-warning@-1{{Call argument for parameter 'str' is unretained and unsafe}}
432+
// expected-warning@-2{{Call argument for parameter 'dict' is unretained and unsafe}}
433+
}
434+
435+
} // namespace const_global
436+
418437
@interface TestObject : NSObject
419438
- (void)doWork:(NSString *)msg, ...;
420439
- (void)doWorkOnSelf;

Diff for: clang/test/Analysis/Checkers/WebKit/unretained-local-vars-arc.mm

+20
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,26 @@ void bar() {
2525

2626
} // namespace raw_ptr
2727

28+
namespace const_global {
29+
30+
extern NSString * const SomeConstant;
31+
extern CFDictionaryRef const SomeDictionary;
32+
void doWork(NSString *, CFDictionaryRef);
33+
void use_const_global() {
34+
doWork(SomeConstant, SomeDictionary);
35+
}
36+
37+
NSString *provide_str();
38+
CFDictionaryRef provide_dict();
39+
void use_const_local() {
40+
NSString * const str = provide_str();
41+
CFDictionaryRef dict = provide_dict();
42+
// expected-warning@-1{{Local variable 'dict' is unretained and unsafe [alpha.webkit.UnretainedLocalVarsChecker]}}
43+
doWork(str, dict);
44+
}
45+
46+
} // namespace const_global
47+
2848
@interface AnotherObj : NSObject
2949
- (void)foo:(SomeObj *)obj;
3050
@end

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

+21
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,27 @@ unsigned ccf(CFTypeRef obj) {
387387

388388
} // ptr_conversion
389389

390+
namespace const_global {
391+
392+
extern NSString * const SomeConstant;
393+
extern CFDictionaryRef const SomeDictionary;
394+
void doWork(NSString *, CFDictionaryRef);
395+
void use_const_global() {
396+
doWork(SomeConstant, SomeDictionary);
397+
}
398+
399+
NSString *provide_str();
400+
CFDictionaryRef provide_dict();
401+
void use_const_local() {
402+
NSString * const str = provide_str();
403+
// expected-warning@-1{{Local variable 'str' is unretained and unsafe [alpha.webkit.UnretainedLocalVarsChecker]}}
404+
CFDictionaryRef dict = provide_dict();
405+
// expected-warning@-1{{Local variable 'dict' is unretained and unsafe [alpha.webkit.UnretainedLocalVarsChecker]}}
406+
doWork(str, dict);
407+
}
408+
409+
} // namespace const_global
410+
390411
bool doMoreWorkOpaque(OtherObj*);
391412
SomeObj* provide();
392413

0 commit comments

Comments
 (0)