From ec88edd564239f507a1a98f82332ab290b3e5e1f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ezgi=20=C3=87i=C3=A7ek?= Date: Wed, 7 Oct 2020 00:42:20 -0700 Subject: [PATCH] [inferbo] Add model for NSDictionary.initWithDictionary Summary: Model it similar to `NSArray.initWithArray` as copying from the given dictionary elements. Removes a FP as expected. Reviewed By: ngorogiannis Differential Revision: D24136868 fbshipit-source-id: ed31c3c8f --- infer/src/bufferoverrun/bufferOverrunModels.ml | 5 +++++ infer/tests/codetoanalyze/objc/performance/NSDictionary.m | 2 +- infer/tests/codetoanalyze/objc/performance/cost-issues.exp | 2 +- infer/tests/codetoanalyze/objc/performance/issues.exp | 2 -- 4 files changed, 7 insertions(+), 4 deletions(-) diff --git a/infer/src/bufferoverrun/bufferOverrunModels.ml b/infer/src/bufferoverrun/bufferOverrunModels.ml index def0e9f1a..acb0f2565 100644 --- a/infer/src/bufferoverrun/bufferOverrunModels.ml +++ b/infer/src/bufferoverrun/bufferOverrunModels.ml @@ -1518,6 +1518,9 @@ let objc_malloc exp = match exp with | Exp.Sizeof {typ} when PatternMatch.ObjectiveC.implements "NSArray" tenv (Typ.to_string typ) -> NSCollection.new_collection.exec model ~ret mem + | Exp.Sizeof {typ} + when PatternMatch.ObjectiveC.implements "NSDictionary" tenv (Typ.to_string typ) -> + NSCollection.new_collection.exec model ~ret mem | Exp.Sizeof {typ} when PatternMatch.ObjectiveC.implements "NSString" tenv (Typ.to_string typ) -> (NSString.create_with_c_string (Exp.Const (Const.Cstr ""))).exec model ~ret mem @@ -1679,6 +1682,8 @@ module Call = struct ; +PatternMatch.ObjectiveC.implements "NSArray" &:: "array" <>--> NSCollection.new_collection ; +PatternMatch.ObjectiveC.implements "NSArray" &:: "firstObject" <>$ capt_var_exn $!--> NSCollection.get_first + ; +PatternMatch.ObjectiveC.implements "NSDictionary" + &:: "initWithDictionary:" <>$ capt_var_exn $+ capt_exp $--> NSCollection.copy ; +PatternMatch.ObjectiveC.implements "NSArray" &:: "initWithArray:" <>$ capt_var_exn $+ capt_exp $--> NSCollection.copy ; +PatternMatch.ObjectiveC.implements "NSArray" diff --git a/infer/tests/codetoanalyze/objc/performance/NSDictionary.m b/infer/tests/codetoanalyze/objc/performance/NSDictionary.m index 1a1271200..32453761c 100644 --- a/infer/tests/codetoanalyze/objc/performance/NSDictionary.m +++ b/infer/tests/codetoanalyze/objc/performance/NSDictionary.m @@ -19,7 +19,7 @@ NSDictionary* nsdictionary_init_dictionary_constant() { return [NSDictionary dictionary]; } -void nsdictionary_init_with_dictionary_linear_FP(NSDictionary* dict) { +void nsdictionary_init_with_dictionary_linear(NSDictionary* dict) { NSDictionary* copy_dict = [[NSDictionary alloc] initWithDictionary:dict]; for (int i = 0; i < [copy_dict allValues].count; i++) { } diff --git a/infer/tests/codetoanalyze/objc/performance/cost-issues.exp b/infer/tests/codetoanalyze/objc/performance/cost-issues.exp index e79e2952c..e28f67618 100644 --- a/infer/tests/codetoanalyze/objc/performance/cost-issues.exp +++ b/infer/tests/codetoanalyze/objc/performance/cost-issues.exp @@ -40,7 +40,7 @@ codetoanalyze/objc/performance/NSDictionary.m, nsdictionary_fast_enumerate_linea codetoanalyze/objc/performance/NSDictionary.m, nsdictionary_find_key_constant, 19, OnUIThread:false, [] codetoanalyze/objc/performance/NSDictionary.m, nsdictionary_init_dictionary_constant, 3, OnUIThread:false, [] codetoanalyze/objc/performance/NSDictionary.m, nsdictionary_init_literal_constant, 45, OnUIThread:false, [] -codetoanalyze/objc/performance/NSDictionary.m, nsdictionary_init_with_dictionary_linear_FP, ⊤, OnUIThread:false, [Unbounded loop,Loop] +codetoanalyze/objc/performance/NSDictionary.m, nsdictionary_init_with_dictionary_linear, 7 + 3 ⋅ dict->elements.length.ub + 4 ⋅ (dict->elements.length.ub + 1), OnUIThread:false, [{dict->elements.length.ub + 1},Loop,{dict->elements.length.ub},Loop] codetoanalyze/objc/performance/NSInteger.m, nsinteger_value_linear, 3 + 3 ⋅ integer + 2 ⋅ (1+max(0, integer)), OnUIThread:false, [{1+max(0, integer)},Loop,{integer},Loop] codetoanalyze/objc/performance/NSInteger.m, nsnumber_number_with_int_integer_value_constant, 34, OnUIThread:false, [] codetoanalyze/objc/performance/NSMutableArray.m, nsarray_new_constant, 9, OnUIThread:false, [] diff --git a/infer/tests/codetoanalyze/objc/performance/issues.exp b/infer/tests/codetoanalyze/objc/performance/issues.exp index d2f44ae0f..e42093ad5 100644 --- a/infer/tests/codetoanalyze/objc/performance/issues.exp +++ b/infer/tests/codetoanalyze/objc/performance/issues.exp @@ -1,7 +1,5 @@ codetoanalyze/objc/performance/NSArray.m, nsarray_empty_array_constant, 3, CONDITION_ALWAYS_FALSE, no_bucket, WARNING, [Here] codetoanalyze/objc/performance/NSArray.m, nsarray_init_constant, 3, CONDITION_ALWAYS_FALSE, no_bucket, WARNING, [Here] -codetoanalyze/objc/performance/NSDictionary.m, nsdictionary_init_with_dictionary_linear_FP, 0, INFINITE_EXECUTION_TIME, no_bucket, ERROR, [Unbounded loop,Loop] -codetoanalyze/objc/performance/NSDictionary.m, nsdictionary_init_with_dictionary_linear_FP, 2, INTEGER_OVERFLOW_U5, no_bucket, ERROR, [,Unknown value from: NSDictionary.initWithDictionary:,Binary operation: ([0, +oo] + 1):signed32] codetoanalyze/objc/performance/NSMutableArray.m, nsarray_new_constant, 2, CONDITION_ALWAYS_FALSE, no_bucket, WARNING, [Here] codetoanalyze/objc/performance/NSMutableArray.m, nsmarray_remove_constant, 6, BUFFER_OVERRUN_L1, no_bucket, ERROR, [,Array declaration,Through,Through,Through,Through,Array access: Offset: 0 Size: 0] codetoanalyze/objc/performance/NSMutableArray.m, nsmarray_remove_in_loop_constant, 6, BUFFER_OVERRUN_L3, no_bucket, ERROR, [,Set array size,,Assignment,Set array size,Array access: Offset: [0, 9] Size: [1, 10]]