From 562f9d1c7cad03a9cff372bdfe731c9ba354f943 Mon Sep 17 00:00:00 2001 From: Martin Trojer Date: Wed, 1 Aug 2018 07:07:24 -0700 Subject: [PATCH] Add guard against triggering static self logic when we have nested class calls Reviewed By: jvillard Differential Revision: D9116721 fbshipit-source-id: 8d6f4a98d --- infer/src/clang/cTrans.ml | 13 +++---- .../objc/liveness/NestedClassCalls.m | 35 +++++++++++++++++++ .../codetoanalyze/objc/liveness/issues.exp | 1 + 3 files changed, 43 insertions(+), 6 deletions(-) create mode 100644 infer/tests/codetoanalyze/objc/liveness/NestedClassCalls.m diff --git a/infer/src/clang/cTrans.ml b/infer/src/clang/cTrans.ml index c9dcd10a2..304b40a82 100644 --- a/infer/src/clang/cTrans.ml +++ b/infer/src/clang/cTrans.ml @@ -1195,6 +1195,8 @@ module CTrans_funct (F : CModule_type.CFrontend) : CModule_type.CTranslation = s None + and is_receiver_instance = function `Instance | `SuperInstance -> true | _ -> false + and objCMessageExpr_trans_special_cases trans_state si obj_c_message_expr_info method_type trans_state_pri sil_loc act_params = let context = trans_state.context in @@ -1206,7 +1208,8 @@ module CTrans_funct (F : CModule_type.CFrontend) : CModule_type.CTranslation = s CMethod_trans.get_class_name_method_call_from_receiver_kind context obj_c_message_expr_info act_params in - if trans_state.is_fst_arg_objc_instance_method_call then + if trans_state.is_fst_arg_objc_instance_method_call && is_receiver_instance receiver_kind + then raise (Self.SelfClassException {class_name; position= __POS__; source_range= si.Clang_ast_t.si_source_range}) @@ -1241,11 +1244,9 @@ module CTrans_funct (F : CModule_type.CFrontend) : CModule_type.CTranslation = s in try let trans_state_param' = - match obj_c_message_expr_info.Clang_ast_t.omei_receiver_kind with - | `Instance | `SuperInstance -> - {trans_state_param with is_fst_arg_objc_instance_method_call= true} - | _ -> - {trans_state_param with is_fst_arg_objc_instance_method_call= false} + if is_receiver_instance obj_c_message_expr_info.Clang_ast_t.omei_receiver_kind then + {trans_state_param with is_fst_arg_objc_instance_method_call= true} + else {trans_state_param with is_fst_arg_objc_instance_method_call= false} in let fst_res_trans = instruction trans_state_param' stmt in (obj_c_message_expr_info, fst_res_trans :: param_trans_results) diff --git a/infer/tests/codetoanalyze/objc/liveness/NestedClassCalls.m b/infer/tests/codetoanalyze/objc/liveness/NestedClassCalls.m new file mode 100644 index 000000000..605221a4a --- /dev/null +++ b/infer/tests/codetoanalyze/objc/liveness/NestedClassCalls.m @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2018-present, Facebook, Inc. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ +#import + +@interface A : NSObject +@property int val; +@end + +bool myrand(); + +@implementation A +- (instancetype)myDoO:(id)o { + return self; +} ++ (instancetype)initWithVal:(int)v { + A* a = [A alloc]; + a.val = v; + return a; +} ++ (instancetype)nestedGood { + const BOOL isB = myrand(); + return [[[A initWithVal:isB ? 0 : 1] myDoO:[NSObject class]] + myDoO:[NSObject class]]; +} + ++ (instancetype)nestedBad { + const BOOL isB = myrand(); + return [[[A initWithVal:42] myDoO:[NSObject class]] myDoO:[NSObject class]]; +} + +@end diff --git a/infer/tests/codetoanalyze/objc/liveness/issues.exp b/infer/tests/codetoanalyze/objc/liveness/issues.exp index e69de29bb..fdf0fad36 100644 --- a/infer/tests/codetoanalyze/objc/liveness/issues.exp +++ b/infer/tests/codetoanalyze/objc/liveness/issues.exp @@ -0,0 +1 @@ +codetoanalyze/objc/liveness/NestedClassCalls.m, A_nestedBad, 1, DEAD_STORE, no_bucket, ERROR, [Write of unused value]