diff --git a/infer/tests/codetoanalyze/objc/performance/NSArray.m b/infer/tests/codetoanalyze/objc/performance/NSArray.m index 71f3ac9eb..295a36733 100644 --- a/infer/tests/codetoanalyze/objc/performance/NSArray.m +++ b/infer/tests/codetoanalyze/objc/performance/NSArray.m @@ -42,6 +42,18 @@ NSArray* nsarray_init_with_objects_constant() { return [NSArray arrayWithObjects:strings count:2]; } +NSArray* nsarray_array_with_objects_constant() { + NSDate* aDate = [NSDate distantFuture]; + NSValue* aValue = @(5); + NSString* aString = @"hello"; + + NSArray* array = [NSArray arrayWithObjects:aDate, aValue, aString, nil]; + for (id item in array) { + } + + return array; +} + // derive new array NSArray* nsarray_add_object_constant(id obj) { @@ -70,6 +82,11 @@ void nsarray_access_linear_FP(NSArray* array) { } } +void nsarray_find_linear_FP(NSArray* array) { + for (int i = 0; i < array.count && i != [array[i] integerValue]; i++) { + } +} + void nsarray_contains_object_linear_FN(NSArray* array) { [array containsObject:@1]; } @@ -124,8 +141,20 @@ void nsarray_enumerator_linear_FN(NSArray* array) { } } +void nsarray_next_object_linear_FN(NSArray* array) { + for (id item in array) { + } +} + // compare array boolean_t nsarray_is_equal_to_array_linear_FN(NSArray* array1, NSArray* array2) { return [array1 isEqualToArray:array2]; } + +// count + +void nsarray_count_bounded_linear_FP(NSArray* array) { + for (int i = 0; i < array.count; i++) { + } +} diff --git a/infer/tests/codetoanalyze/objc/performance/NSDictionary.m b/infer/tests/codetoanalyze/objc/performance/NSDictionary.m index 61b986f54..102ffbbfc 100644 --- a/infer/tests/codetoanalyze/objc/performance/NSDictionary.m +++ b/infer/tests/codetoanalyze/objc/performance/NSDictionary.m @@ -25,6 +25,29 @@ void nsdictionary_init_with_dictionary_linear_FP(NSDictionary* dict) { } } +NSDictionary* nsdictionary_dictionary_with_objects_linear(int n_entries) { + NSDictionary* asciiDict; + NSString* keyArray[n_entries]; + NSNumber* valueArray[n_entries]; + NSInteger i; + + for (i = 0; i < n_entries; i++) { + + char charValue = 'a' + i; + keyArray[i] = [NSString stringWithFormat:@"%c", charValue]; + valueArray[i] = [NSNumber numberWithChar:charValue]; + } + + asciiDict = [NSDictionary dictionaryWithObjects:(id*)valueArray + forKeys:(id*)keyArray + count:n_entries]; + + for (id key in asciiDict) { + } + + return asciiDict; +} + // accessing values and keys void nsdictionary_all_keys_linear_FP(NSDictionary* dict) { @@ -37,6 +60,15 @@ void nsdictionary_all_values_linear_FP(NSDictionary* dict) { } } +id nsdictionary_find_key_linear_FN(NSDictionary* dict, id item) { + NSEnumerator* enumerator = [dict keyEnumerator]; + id key; + while ((key = [enumerator nextObject]) && dict[key] == item) { + return key; + } + return NULL; +} + // enumerate dictionary void nsdictionary_fast_enumerate_linear_FN(NSDictionary* dict) { diff --git a/infer/tests/codetoanalyze/objc/performance/NSString.m b/infer/tests/codetoanalyze/objc/performance/NSString.m index 4ae87c07a..0ad5d8f51 100644 --- a/infer/tests/codetoanalyze/objc/performance/NSString.m +++ b/infer/tests/codetoanalyze/objc/performance/NSString.m @@ -87,3 +87,12 @@ void replace_linear_FP(NSString* s) { for (int i = 0; i < r.length; i++) { } } + +NSString* string_with_utf8_string_linear_FP(const char* p) { + NSString* s = [NSString stringWithUTF8String:p]; + NSInteger i = 0; + while ([s characterAtIndex:i] != 'a' && i < s.length) { + i++; + } + return s; +} diff --git a/infer/tests/codetoanalyze/objc/performance/cost-issues.exp b/infer/tests/codetoanalyze/objc/performance/cost-issues.exp index a9bf3a979..14b34d754 100644 --- a/infer/tests/codetoanalyze/objc/performance/cost-issues.exp +++ b/infer/tests/codetoanalyze/objc/performance/cost-issues.exp @@ -1,10 +1,13 @@ codetoanalyze/objc/performance/NSArray.m, nsarray_access_constant, 42, OnUIThread:false, [] -codetoanalyze/objc/performance/NSArray.m, nsarray_access_linear_FP, ⊤, OnUIThread:false, [Unbounded loop,Loop at line 68, column 3] +codetoanalyze/objc/performance/NSArray.m, nsarray_access_linear_FP, ⊤, OnUIThread:false, [Unbounded loop,Loop at line 80, column 3] codetoanalyze/objc/performance/NSArray.m, nsarray_add_object_constant, 8, OnUIThread:false, [] codetoanalyze/objc/performance/NSArray.m, nsarray_add_objects_from_array_linear_FN, 8, OnUIThread:false, [] +codetoanalyze/objc/performance/NSArray.m, nsarray_array_with_objects_constant, 23, OnUIThread:false, [] codetoanalyze/objc/performance/NSArray.m, nsarray_binary_search_log_FN, 10, OnUIThread:false, [] codetoanalyze/objc/performance/NSArray.m, nsarray_contains_object_linear_FN, 4, OnUIThread:false, [] +codetoanalyze/objc/performance/NSArray.m, nsarray_count_bounded_linear_FP, ⊤, OnUIThread:false, [Unbounded loop,Loop at line 158, column 3] codetoanalyze/objc/performance/NSArray.m, nsarray_enumerator_linear_FN, 14, OnUIThread:false, [] +codetoanalyze/objc/performance/NSArray.m, nsarray_find_linear_FP, ⊤, OnUIThread:false, [Unbounded loop,Loop at line 86, column 3] codetoanalyze/objc/performance/NSArray.m, nsarray_first_object_constant, 4, OnUIThread:false, [] codetoanalyze/objc/performance/NSArray.m, nsarray_init_constant_FP, ⊤, OnUIThread:false, [Unbounded loop,Loop at line 14, column 3] codetoanalyze/objc/performance/NSArray.m, nsarray_init_with_array_constant_FP, ⊤, OnUIThread:false, [Call to nsarray_init_with_array_linear_FP,Unbounded loop,Loop at line 21, column 3] @@ -14,14 +17,17 @@ codetoanalyze/objc/performance/NSArray.m, nsarray_init_with_objects_constant, 9, codetoanalyze/objc/performance/NSArray.m, nsarray_is_equal_to_array_linear_FN, 5, OnUIThread:false, [] codetoanalyze/objc/performance/NSArray.m, nsarray_iterate_linear_FN, 14, OnUIThread:false, [] codetoanalyze/objc/performance/NSArray.m, nsarray_last_object_constant, 4, OnUIThread:false, [] +codetoanalyze/objc/performance/NSArray.m, nsarray_next_object_linear_FN, 10, OnUIThread:false, [] codetoanalyze/objc/performance/NSArray.m, nsarray_sort_using_descriptors_constant, 14, OnUIThread:false, [] codetoanalyze/objc/performance/NSArray.m, nsarray_sort_using_descriptors_nlogn_FN, 9, OnUIThread:false, [] codetoanalyze/objc/performance/NSArray.m, objc_blocknsarray_binary_search_log_FN_1, 5, OnUIThread:false, [] -codetoanalyze/objc/performance/NSDictionary.m, nsdictionary_all_keys_linear_FP, ⊤, OnUIThread:false, [Unbounded loop,Loop at line 31, column 3] -codetoanalyze/objc/performance/NSDictionary.m, nsdictionary_all_values_linear_FP, ⊤, OnUIThread:false, [Unbounded loop,Loop at line 36, column 3] +codetoanalyze/objc/performance/NSDictionary.m, nsdictionary_all_keys_linear_FP, ⊤, OnUIThread:false, [Unbounded loop,Loop at line 54, column 3] +codetoanalyze/objc/performance/NSDictionary.m, nsdictionary_all_values_linear_FP, ⊤, OnUIThread:false, [Unbounded loop,Loop at line 59, column 3] +codetoanalyze/objc/performance/NSDictionary.m, nsdictionary_dictionary_with_objects_linear, 21 + 14 ⋅ n_entries + 2 ⋅ (1+max(0, n_entries)), OnUIThread:false, [{1+max(0, n_entries)},Loop at line 34, column 3,{n_entries},Loop at line 34, column 3] codetoanalyze/objc/performance/NSDictionary.m, nsdictionary_enumerate_constant, 20, OnUIThread:false, [] codetoanalyze/objc/performance/NSDictionary.m, nsdictionary_enumerator_linear_FN, 10, OnUIThread:false, [] codetoanalyze/objc/performance/NSDictionary.m, nsdictionary_fast_enumerate_linear_FN, 10, OnUIThread:false, [] +codetoanalyze/objc/performance/NSDictionary.m, nsdictionary_find_key_linear_FN, 19, OnUIThread:false, [] codetoanalyze/objc/performance/NSDictionary.m, nsdictionary_init_dictionary_constant, 3, OnUIThread:false, [] codetoanalyze/objc/performance/NSDictionary.m, nsdictionary_init_literal_constant, 9, OnUIThread:false, [] codetoanalyze/objc/performance/NSDictionary.m, nsdictionary_init_with_dictionary_linear_FP, ⊤, OnUIThread:false, [Unbounded loop,Loop at line 24, column 3] @@ -51,6 +57,7 @@ codetoanalyze/objc/performance/NSString.m, rangeof_string_quadratic_FN, 6, OnUI codetoanalyze/objc/performance/NSString.m, replace_linear_FP, ⊤, OnUIThread:false, [Unbounded loop,Loop at line 87, column 3] codetoanalyze/objc/performance/NSString.m, string_by_appending_same_string_linear_FN, 7, OnUIThread:false, [] codetoanalyze/objc/performance/NSString.m, string_by_appending_string_linear_FN, 7, OnUIThread:false, [] +codetoanalyze/objc/performance/NSString.m, string_with_utf8_string_linear_FP, ⊤, OnUIThread:false, [Unbounded loop,Loop at line 94, column 3] codetoanalyze/objc/performance/NSString.m, substring_from_index_linear_FN, 13, OnUIThread:false, [] codetoanalyze/objc/performance/NSString.m, substring_no_end_linear_FP, ⊤, OnUIThread:false, [Unbounded loop,Loop at line 81, column 3] codetoanalyze/objc/performance/araii.m, Araii.buffer, 4, OnUIThread:false, [] diff --git a/infer/tests/codetoanalyze/objc/performance/issues.exp b/infer/tests/codetoanalyze/objc/performance/issues.exp index 6019ef1bb..16c888df7 100644 --- a/infer/tests/codetoanalyze/objc/performance/issues.exp +++ b/infer/tests/codetoanalyze/objc/performance/issues.exp @@ -1,6 +1,10 @@ -codetoanalyze/objc/performance/NSArray.m, nsarray_access_linear_FP, 0, INFINITE_EXECUTION_TIME, no_bucket, ERROR, [Unbounded loop,Loop at line 68, column 3] +codetoanalyze/objc/performance/NSArray.m, nsarray_access_linear_FP, 0, INFINITE_EXECUTION_TIME, no_bucket, ERROR, [Unbounded loop,Loop at line 80, column 3] codetoanalyze/objc/performance/NSArray.m, nsarray_access_linear_FP, 2, INTEGER_OVERFLOW_U5, no_bucket, ERROR, [,Unknown value from: NSArray.count,Binary operation: ([0, +oo] + 1):signed32] +codetoanalyze/objc/performance/NSArray.m, nsarray_count_bounded_linear_FP, 0, INFINITE_EXECUTION_TIME, no_bucket, ERROR, [Unbounded loop,Loop at line 158, column 3] +codetoanalyze/objc/performance/NSArray.m, nsarray_count_bounded_linear_FP, 1, INTEGER_OVERFLOW_U5, no_bucket, ERROR, [,Unknown value from: NSArray.count,Binary operation: ([0, +oo] + 1):signed32] codetoanalyze/objc/performance/NSArray.m, nsarray_enumerator_linear_FN, 7, INTEGER_OVERFLOW_U5, no_bucket, ERROR, [,Assignment,,Unknown value from: NSEnumerator.nextObject,Assignment,Binary operation: ([-oo, +oo] + [-oo, +oo]):signed64] +codetoanalyze/objc/performance/NSArray.m, nsarray_find_linear_FP, 0, INFINITE_EXECUTION_TIME, no_bucket, ERROR, [Unbounded loop,Loop at line 86, column 3] +codetoanalyze/objc/performance/NSArray.m, nsarray_find_linear_FP, 1, INTEGER_OVERFLOW_U5, no_bucket, ERROR, [,Unknown value from: NSArray.count,Binary operation: ([0, +oo] + 1):signed32] codetoanalyze/objc/performance/NSArray.m, nsarray_init_constant_FP, 0, INFINITE_EXECUTION_TIME, no_bucket, ERROR, [Unbounded loop,Loop at line 14, column 3] codetoanalyze/objc/performance/NSArray.m, nsarray_init_constant_FP, 3, INTEGER_OVERFLOW_U5, no_bucket, ERROR, [,Unknown value from: NSArray.count,Binary operation: ([0, +oo] + 1):signed32] codetoanalyze/objc/performance/NSArray.m, nsarray_init_with_array_constant_FP, 0, INFINITE_EXECUTION_TIME, no_bucket, ERROR, [Call to nsarray_init_with_array_linear_FP,Unbounded loop,Loop at line 21, column 3] @@ -9,9 +13,9 @@ codetoanalyze/objc/performance/NSArray.m, nsarray_init_with_array_copy_linear_FP codetoanalyze/objc/performance/NSArray.m, nsarray_init_with_array_linear_FP, 0, INFINITE_EXECUTION_TIME, no_bucket, ERROR, [Unbounded loop,Loop at line 21, column 3] codetoanalyze/objc/performance/NSArray.m, nsarray_init_with_array_linear_FP, 3, INTEGER_OVERFLOW_U5, no_bucket, ERROR, [,Unknown value from: NSArray.count,Binary operation: ([0, +oo] + 1):signed32] codetoanalyze/objc/performance/NSArray.m, nsarray_iterate_linear_FN, 3, INTEGER_OVERFLOW_U5, no_bucket, ERROR, [,Assignment,,Unknown value from: NSArray.nextObject,Assignment,Binary operation: ([-oo, +oo] + [-oo, +oo]):signed64] -codetoanalyze/objc/performance/NSDictionary.m, nsdictionary_all_keys_linear_FP, 0, INFINITE_EXECUTION_TIME, no_bucket, ERROR, [Unbounded loop,Loop at line 31, column 3] +codetoanalyze/objc/performance/NSDictionary.m, nsdictionary_all_keys_linear_FP, 0, INFINITE_EXECUTION_TIME, no_bucket, ERROR, [Unbounded loop,Loop at line 54, column 3] codetoanalyze/objc/performance/NSDictionary.m, nsdictionary_all_keys_linear_FP, 1, INTEGER_OVERFLOW_U5, no_bucket, ERROR, [,Unknown value from: NSArray.count,Binary operation: ([0, +oo] + 1):signed32] -codetoanalyze/objc/performance/NSDictionary.m, nsdictionary_all_values_linear_FP, 0, INFINITE_EXECUTION_TIME, no_bucket, ERROR, [Unbounded loop,Loop at line 36, column 3] +codetoanalyze/objc/performance/NSDictionary.m, nsdictionary_all_values_linear_FP, 0, INFINITE_EXECUTION_TIME, no_bucket, ERROR, [Unbounded loop,Loop at line 59, column 3] codetoanalyze/objc/performance/NSDictionary.m, nsdictionary_all_values_linear_FP, 1, INTEGER_OVERFLOW_U5, no_bucket, ERROR, [,Unknown value from: NSArray.count,Binary operation: ([0, +oo] + 1):signed32] codetoanalyze/objc/performance/NSDictionary.m, nsdictionary_init_with_dictionary_linear_FP, 0, INFINITE_EXECUTION_TIME, no_bucket, ERROR, [Unbounded loop,Loop at line 24, column 3] codetoanalyze/objc/performance/NSDictionary.m, nsdictionary_init_with_dictionary_linear_FP, 2, INTEGER_OVERFLOW_U5, no_bucket, ERROR, [,Unknown value from: NSArray.count,Binary operation: ([0, +oo] + 1):signed32] @@ -39,6 +43,8 @@ codetoanalyze/objc/performance/NSString.m, init_with_string_linear_FP, 0, INFINI codetoanalyze/objc/performance/NSString.m, init_with_string_linear_FP, 2, INTEGER_OVERFLOW_U5, no_bucket, ERROR, [,Unknown value from: NSString.length,Binary operation: ([0, +oo] + 1):signed32] codetoanalyze/objc/performance/NSString.m, replace_linear_FP, 0, INFINITE_EXECUTION_TIME, no_bucket, ERROR, [Unbounded loop,Loop at line 87, column 3] codetoanalyze/objc/performance/NSString.m, replace_linear_FP, 2, INTEGER_OVERFLOW_U5, no_bucket, ERROR, [,Unknown value from: NSString.length,Binary operation: ([0, +oo] + 1):signed32] +codetoanalyze/objc/performance/NSString.m, string_with_utf8_string_linear_FP, 0, INFINITE_EXECUTION_TIME, no_bucket, ERROR, [Unbounded loop,Loop at line 94, column 3] +codetoanalyze/objc/performance/NSString.m, string_with_utf8_string_linear_FP, 4, INTEGER_OVERFLOW_U5, no_bucket, ERROR, [,Unknown value from: NSString.length,Binary operation: ([0, +oo] + 1):signed64] codetoanalyze/objc/performance/NSString.m, substring_no_end_linear_FP, 0, INFINITE_EXECUTION_TIME, no_bucket, ERROR, [Unbounded loop,Loop at line 81, column 3] codetoanalyze/objc/performance/NSString.m, substring_no_end_linear_FP, 2, INTEGER_OVERFLOW_U5, no_bucket, ERROR, [,Unknown value from: NSString.length,Binary operation: ([0, +oo] + 1):signed32] codetoanalyze/objc/performance/block.m, objc_blockblock_multiply_array_linear_FN_1, 3, INTEGER_OVERFLOW_U5, no_bucket, ERROR, [,Assignment,,Unknown value from: NSNumber.integerValue,Binary operation: ([-oo, +oo] + [-oo, +oo]):signed64]