facebook / infer

A static analyzer for Java, C, C++, and Objective-C
http://fbinfer.com/
MIT License
14.96k stars 2.02k forks source link

[help wanted] iOS custom linter rules #814

Open AwayQu opened 6 years ago

AwayQu commented 6 years ago

I want write a linter rule to check nil safe.

call -addObject: without check nil.

-(void)aMethod:(NSString *)string {
    NSMutableArray *arr = [@[] mutableCopy];
    [arr addObject:string];
}

call -addObject: with check nil.

-(void)aMethod:(NSString *)string {
    NSMutableArray *arr = [@[] mutableCopy];
    if (string) {
         [arr addObject:string];
   }
}

I had read http://fbinfer.com/docs/linters.html but has no idea how to write 0.0

Is possiable using AL write this rule?

dulmarod commented 6 years ago

A linter is less useful for this, because the info about whether the object is nil is not available in the AST of the program, but when the program is run. We have some checks for this already, and are working on a more comprehensive check, which is not on by default yet because it's still being developed, but you can turn it on with --check-nullable. We would appreciate early feedback on the check (cc. @jeremydubreil).

AwayQu commented 6 years ago

@dulmarod I tried to turn on with --check-nullable. As you mentioned, it's not release feature. The analyzing result NULLABLE_DEREFERENCE is not helpful enough.

dulmarod commented 6 years ago

It is being developed currently, and more cases are being added. Can you give us an example code that you would like analyzed that didn't work? (By the way, you should use the latest master, as this is a very new check).

AwayQu commented 6 years ago

@dulmarod I built latest mater unsuccessfully yesterday, these days I use infer 0.13.0 release with --check-nullable on.

On iOS, I find out --check-nullable opt will check all variables rely on annotation __Nullable.

The following code will raise NULLABLE_DEREFERENCE error, because not annotate with __Nullable, but Objective-c is nil safe.So I think the error report is not necessary.

finishButton.titleLabel.font = [UIFont systemFontOfSize:16];

And I also confuse with NULL_DEREFERENCE anaylze result.

- (void)null_dereference_not_raise:(NSString *)str {
     // this not raise NULL_DEREFERENCE
     NSMutableArray *arr = [@[] mutableCopy];
     [arr addObject:str];
}
- (void)null_dereference_raise:(NSString *)str {
     // this raise NULL_DEREFERENCE
     NSString *a = nil;
     if (str) {
       a = str;
     }
     NSMutableArray *arr = [@[] mutableCopy];
     [arr addObject:a];
}

I think both code snippet should raise NULL_DERFERENCE error.

dulmarod commented 6 years ago

Thanks for the examples, that's useful for us. You should try to build master, since the check has been improved a lot since the release was made.

LRH-iOS commented 2 years ago

I want to add a custom rule. rule: analyze .ipa or .app to find a specific string. My questions are as follows:(string, such as "strlen")

  1. How many specific strings are displayed?
  2. In which class does it run?
  3. How can I customize this rule?