diff --git a/infer/src/bufferoverrun/bufferOverrunModels.ml b/infer/src/bufferoverrun/bufferOverrunModels.ml index c6f1cd704..844afbde0 100644 --- a/infer/src/bufferoverrun/bufferOverrunModels.ml +++ b/infer/src/bufferoverrun/bufferOverrunModels.ml @@ -1169,6 +1169,12 @@ module NSCollection = struct {exec; check= no_check} end +module NSURL = struct + let get_resource_value = + let exec _model_env ~ret:(id, _) mem = model_by_value (Dom.Val.of_itv Itv.zero_one) id mem in + {exec; check= no_check} +end + module JavaClass = struct let decl_array {pname; node_hash; location} ~ret:(ret_id, _) length mem = let loc = @@ -1758,6 +1764,9 @@ module Call = struct ; +PatternMatch.ObjectiveC.implements "NSString" &:: "initWithBytes:length:encoding:" <>$ capt_exp $+ capt_exp $+ capt_exp $+ any_arg $!--> NSString.init_with_c_string_with_len + ; +PatternMatch.ObjectiveC.implements "NSURL" + &:: "getResourceValue:forKey:error:" &--> NSURL.get_resource_value + ; +PatternMatch.ObjectiveC.implements "NSURL" &:: "path" $ capt_exp $--> id ; (* C++ models *) -"boost" &:: "split" $ capt_arg_of_typ (-"std" &:: "vector") diff --git a/infer/tests/codetoanalyze/objc/performance/NSURL.m b/infer/tests/codetoanalyze/objc/performance/NSURL.m new file mode 100644 index 000000000..03b26c407 --- /dev/null +++ b/infer/tests/codetoanalyze/objc/performance/NSURL.m @@ -0,0 +1,19 @@ +/* + * Copyright (c) Facebook, Inc. and its affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ +#import + +void get_resource_value_constant(NSURL* url) { + BOOL b = [url getResourceValue:nil forKey:@"key" error:nil]; + for (int i = 0; i < b; i++) { + } +} + +void path_linear(NSURL* url) { + NSString* path = (NSString*)url.path; + for (int i = 0; i < path.length; i++) { + } +} diff --git a/infer/tests/codetoanalyze/objc/performance/cost-issues.exp b/infer/tests/codetoanalyze/objc/performance/cost-issues.exp index 031f0c714..66e7a27d4 100644 --- a/infer/tests/codetoanalyze/objc/performance/cost-issues.exp +++ b/infer/tests/codetoanalyze/objc/performance/cost-issues.exp @@ -91,6 +91,8 @@ codetoanalyze/objc/performance/NSString.m, string_is_equal_to_string_linear, 4 + codetoanalyze/objc/performance/NSString.m, string_length_linear, 3 + 3 ⋅ s.length.ub + 4 ⋅ (s.length.ub + 1), OnUIThread:false, [{s.length.ub + 1},Loop,{s.length.ub},Loop] codetoanalyze/objc/performance/NSString.m, string_with_utf8_string_linear, 7 + 3 ⋅ p->strlen.ub + p->strlen.ub + 4 ⋅ (p->strlen.ub + 1), OnUIThread:false, [{p->strlen.ub + 1},Loop,{p->strlen.ub},Modeled call to NSString.stringWithUTF8String:,{p->strlen.ub},Loop] codetoanalyze/objc/performance/NSString.m, substring_no_end_linear, 10 + 6 ⋅ (-x + s.length.ub), OnUIThread:false, [{-x + s.length.ub},Loop] +codetoanalyze/objc/performance/NSURL.m, get_resource_value_constant, 16, OnUIThread:false, [] +codetoanalyze/objc/performance/NSURL.m, path_linear, 6 + 3 ⋅ url.length.ub + 3 ⋅ (url.length.ub + 1), OnUIThread:false, [{url.length.ub + 1},Loop,{url.length.ub},Loop] codetoanalyze/objc/performance/araii.m, Araii.buffer, 4, OnUIThread:false, [] codetoanalyze/objc/performance/araii.m, Araii.dealloc, 4, OnUIThread:false, [] codetoanalyze/objc/performance/araii.m, Araii.initWithBuffer, 15, OnUIThread:false, []