File tree 5 files changed +83
-6
lines changed
lib/StaticAnalyzer/Checkers/WebKit
test/Analysis/Checkers/WebKit
5 files changed +83
-6
lines changed Original file line number Diff line number Diff line change @@ -30,12 +30,11 @@ bool tryToFindPtrOrigin(
30
30
std::function<bool(const clang::Expr *, bool )> callback) {
31
31
while (E) {
32
32
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
+ }
39
38
}
40
39
}
41
40
if (auto *tempExpr = dyn_cast<MaterializeTemporaryExpr>(E)) {
Original file line number Diff line number Diff line change @@ -18,6 +18,24 @@ void foo() {
18
18
19
19
} // namespace raw_ptr
20
20
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
+
21
39
@interface AnotherObj : NSObject
22
40
- (void )foo : (SomeObj *)obj ;
23
41
- (SomeObj *)getSomeObj ;
Original file line number Diff line number Diff line change @@ -415,6 +415,25 @@ void idcf(CFTypeRef obj) {
415
415
416
416
} // ptr_conversion
417
417
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
+
418
437
@interface TestObject : NSObject
419
438
- (void )doWork : (NSString *)msg , ...;
420
439
- (void )doWorkOnSelf ;
Original file line number Diff line number Diff line change @@ -25,6 +25,26 @@ void bar() {
25
25
26
26
} // namespace raw_ptr
27
27
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
+
28
48
@interface AnotherObj : NSObject
29
49
- (void )foo : (SomeObj *)obj ;
30
50
@end
Original file line number Diff line number Diff line change @@ -387,6 +387,27 @@ unsigned ccf(CFTypeRef obj) {
387
387
388
388
} // ptr_conversion
389
389
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
+
390
411
bool doMoreWorkOpaque (OtherObj*);
391
412
SomeObj* provide ();
392
413
You can’t perform that action at this time.
0 commit comments