Skip to content

Commit

Permalink
[self-in-block] Do not report on instance method calls
Browse files Browse the repository at this point in the history
Summary: We want to stop reporting when strongSelf is the receiver of instance method calls because that's not a crash. Previously we were checking the virtual call flags but that was not enough for checking getters and setters of properties, so we are now also checking the instance property of the callee method.

Reviewed By: ngorogiannis

Differential Revision: D66101550

fbshipit-source-id: f11e248845ef698a95695a73140dc36608fbc919
  • Loading branch information
dulmarod authored and facebook-github-bot committed Nov 19, 2024
1 parent 165b77b commit 1f14ab9
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 3 deletions.
21 changes: 19 additions & 2 deletions infer/src/checkers/SelfInBlock.ml
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,18 @@ module TransferFunctions = struct

let pp_session_name _node fmt = F.pp_print_string fmt "SelfCapturedInBlock"

let is_objc_instance attributes_opt =
match attributes_opt with
| Some proc_attrs -> (
match proc_attrs.ProcAttributes.clang_method_kind with
| ClangMethodKind.OBJC_INSTANCE ->
true
| _ ->
false )
| None ->
false


let exec_instr (astate : Domain.t) {IntraproceduralAnalysis.proc_desc} _cfg_node _
(instr : Sil.instr) =
let attributes = Procdesc.get_attributes proc_desc in
Expand Down Expand Up @@ -387,8 +399,13 @@ module TransferFunctions = struct
| Prune (UnOp (LNot, BinOp (Binop.Ne, Var id, e), _), _, _, _)
when Exp.is_null_literal e (* if !(strongSef != nil) *) ->
Domain.clear_unchecked_use id astate
| Call (_, Exp.Const (Const.Cfun _callee_pn), args, _, cf) ->
let fst = if cf.CallFlags.cf_virtual then List.hd args else None in
| Call (_, Exp.Const (Const.Cfun callee_pn), args, _, cf) ->
let fst =
if cf.CallFlags.cf_virtual then List.hd args
else
let attributes_opt = Attributes.load callee_pn in
if is_objc_instance attributes_opt then List.hd args else None
in
let astate =
Option.value_map
~f:(fun (arg, _) ->
Expand Down
9 changes: 9 additions & 0 deletions infer/tests/codetoanalyze/objc/self-in-block/StrongSelf.m
Original file line number Diff line number Diff line change
Expand Up @@ -434,4 +434,13 @@ - (void)strongSelfCheck11_good {
};
}

- (void)strongSelfCheckCallingPropertyGetter_good {
__weak __typeof(self) weakSelf = self;
int (^my_block)(BOOL) = ^(BOOL isTapped) {
__strong __typeof(weakSelf) strongSelf = weakSelf;
SelfInBlockTestUser* user = strongSelf.user; // no bug here
return 0;
};
}

@end
2 changes: 1 addition & 1 deletion infer/tests/codetoanalyze/objc/self-in-block/issues.exp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ codetoanalyze/objc/self-in-block/StrongSelf.m, objc_block_StrongSelf.m:121, 6, S
codetoanalyze/objc/self-in-block/StrongSelf.m, objc_block_StrongSelf.m:136, 2, STRONG_SELF_NOT_CHECKED, no_bucket, ERROR, [Using &strongSelf]
codetoanalyze/objc/self-in-block/StrongSelf.m, objc_block_StrongSelf.m:158, 2, STRONG_SELF_NOT_CHECKED, no_bucket, ERROR, [Using &strongSelf,Using &strongSelf]
codetoanalyze/objc/self-in-block/StrongSelf.m, objc_block_StrongSelf.m:170, 2, STRONG_SELF_NOT_CHECKED, no_bucket, ERROR, [Using &strongSelf,Using &strongSelf]
codetoanalyze/objc/self-in-block/StrongSelf.m, objc_block_StrongSelf.m:179, 2, STRONG_SELF_NOT_CHECKED, no_bucket, ERROR, [Using &strongSelf,Using &strongSelf]
codetoanalyze/objc/self-in-block/StrongSelf.m, objc_block_StrongSelf.m:179, 3, STRONG_SELF_NOT_CHECKED, no_bucket, ERROR, [Using &strongSelf,Using &strongSelf]
codetoanalyze/objc/self-in-block/StrongSelf.m, objc_block_StrongSelf.m:208, 1, MULTIPLE_WEAKSELF, no_bucket, ERROR, [Using &weakSelf,Using &weakSelf]
codetoanalyze/objc/self-in-block/StrongSelf.m, objc_block_StrongSelf.m:229, 2, MULTIPLE_WEAKSELF, no_bucket, ERROR, [Using &weakSelf,Using &weakSelf]
codetoanalyze/objc/self-in-block/StrongSelf.m, objc_block_StrongSelf.m:243, 1, CAPTURED_STRONG_SELF, no_bucket, ERROR, [Using captured &strongSelf,Using captured &strongSelf]
Expand Down

0 comments on commit 1f14ab9

Please sign in to comment.