Skip to content

Commit

Permalink
[self-in-block] Add the fix closer to the usage of strongSelf
Browse files Browse the repository at this point in the history
Reviewed By: ngorogiannis

Differential Revision:
D66103261

Privacy Context Container: L1208441

fbshipit-source-id: f25634d9375e3d0cf363d3438e8c1321865990de
  • Loading branch information
dulmarod authored and facebook-github-bot committed Nov 19, 2024
1 parent 1f14ab9 commit b1618a7
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 21 deletions.
35 changes: 15 additions & 20 deletions infer/src/checkers/SelfInBlock.ml
Original file line number Diff line number Diff line change
Expand Up @@ -575,8 +575,7 @@ let report_self_in_block_passed_to_init_issue proc_desc err_log domain (captured
()


let report_unchecked_strongself_issues proc_desc err_log domain strongVars
(strongSelf : DomainData.t) =
let report_unchecked_strongself_issues proc_desc err_log domain (strongSelf : DomainData.t) =
if contains_self strongSelf.pvar then
let message =
F.asprintf
Expand All @@ -588,20 +587,16 @@ let report_unchecked_strongself_issues proc_desc err_log domain strongVars
let attributes = Procdesc.get_attributes proc_desc in
let return_typ = attributes.ProcAttributes.ret_type in
let autofix =
match StrongEqualToWeakCapturedVars.find_opt strongSelf.pvar strongVars with
| Some {loc} when Typ.is_void return_typ ->
let strong_self_line = loc.line in
let replacement =
F.asprintf "\n if (!%s) { return; }" (Mangled.to_string (Pvar.get_name strongSelf.pvar))
in
Some
{ Jsonbug_t.original= None
; replacement= None
; additional=
Some [{Jsonbug_t.line= strong_self_line + 1; column= 1; original= ""; replacement}]
}
| _ ->
None
if Typ.is_void return_typ then
let replacement =
F.asprintf "\n if (!%s) { return; }" (Mangled.to_string (Pvar.get_name strongSelf.pvar))
in
Some
{ Jsonbug_t.original= None
; replacement= None
; additional=
Some [{Jsonbug_t.line= strongSelf.loc.line; column= 1; original= ""; replacement}] }
else None
in
Reporting.log_issue proc_desc err_log ~ltr ~loc:strongSelf.loc SelfInBlock ?autofix
IssueType.strong_self_not_checked message
Expand Down Expand Up @@ -692,7 +687,7 @@ let report_internal_pointer_captured_in_block proc_desc err_log domain (string :

type var_lists = {selfList: DomainData.t list; weakSelfList: DomainData.t list}

let report_issues proc_desc err_log domain strongVars reported_strongSelfNotChecked =
let report_issues proc_desc err_log domain reported_strongSelfNotChecked =
let process_domain_item (var_lists, reported_strongSelfNotChecked)
(_, (domain_data : DomainData.t)) =
match domain_data.kind with
Expand Down Expand Up @@ -730,7 +725,7 @@ let report_issues proc_desc err_log domain strongVars reported_strongSelfNotChec
if Pvar.Set.mem domain_data.pvar reported_strongSelfNotChecked then
(var_lists, reported_strongSelfNotChecked)
else (
report_unchecked_strongself_issues proc_desc err_log domain strongVars domain_data ;
report_unchecked_strongself_issues proc_desc err_log domain domain_data ;
(var_lists, Pvar.Set.add domain_data.pvar reported_strongSelfNotChecked) )
| CHECKED_STRONG_SELF ->
(var_lists, reported_strongSelfNotChecked)
Expand Down Expand Up @@ -781,8 +776,8 @@ let checker ({IntraproceduralAnalysis.proc_desc; err_log} as analysis_data) =
let reported_strongSelfNotChecked_empty = Pvar.Set.empty in
ignore
(Domain.fold
(fun {vars; strongVars} reported_strongSelfNotChecked ->
report_issues proc_desc err_log vars strongVars reported_strongSelfNotChecked )
(fun {vars} reported_strongSelfNotChecked ->
report_issues proc_desc err_log vars reported_strongSelfNotChecked )
domain reported_strongSelfNotChecked_empty )
| None ->
()
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 @@ -10,7 +10,7 @@ codetoanalyze/objc/self-in-block/StrongSelf.m, objc_block_StrongSelf.m:65, 4, MI
codetoanalyze/objc/self-in-block/StrongSelf.m, objc_block_StrongSelf.m:65, 5, MIXED_SELF_WEAKSELF, no_bucket, ERROR, [Using &weakSelf,Using &self,Using &self], "self"=>"strongSelf"@70:8
codetoanalyze/objc/self-in-block/StrongSelf.m, objc_block_StrongSelf.m:78, 1, CAPTURED_STRONG_SELF, no_bucket, ERROR, [Using captured &weakSelf]
codetoanalyze/objc/self-in-block/StrongSelf.m, objc_block_StrongSelf.m:97, 2, STRONG_SELF_NOT_CHECKED, no_bucket, ERROR, [Using &strongSelf]
codetoanalyze/objc/self-in-block/StrongSelf.m, objc_block_StrongSelf.m:106, 6, STRONG_SELF_NOT_CHECKED, no_bucket, ERROR, [Using &strongSelf,Using &strongSelf,Using &strongSelf,Using &strongSelf], +""=>"\n if (!strongSelf) { return; }"@108:1
codetoanalyze/objc/self-in-block/StrongSelf.m, objc_block_StrongSelf.m:106, 6, STRONG_SELF_NOT_CHECKED, no_bucket, ERROR, [Using &strongSelf,Using &strongSelf,Using &strongSelf,Using &strongSelf], +""=>"\n if (!strongSelf) { return; }"@112:1
codetoanalyze/objc/self-in-block/StrongSelf.m, objc_block_StrongSelf.m:121, 6, STRONG_SELF_NOT_CHECKED, no_bucket, ERROR, [Using &strongSelf,Using &strongSelf,Using &strongSelf]
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]
Expand Down

0 comments on commit b1618a7

Please sign in to comment.