diff --git a/infer/models/objc/src/NSData.m b/infer/models/objc/src/NSData.m index 8ac01d548..70b88bbf3 100644 --- a/infer/models/objc/src/NSData.m +++ b/infer/models/objc/src/NSData.m @@ -40,6 +40,10 @@ NSData* __objc_alloc(NSData*); return nil; } +- (instancetype)initWithBytesNoCopy:(void*)bytes length:(NSUInteger)length { + return [self initWithBytesNoCopy:bytes length:length freeWhenDone:YES]; +} + - (instancetype)initWithBytesNoCopy:(void*)bytes length:(NSUInteger)length freeWhenDone:(BOOL)flag { diff --git a/infer/tests/codetoanalyze/objc/errors/Makefile b/infer/tests/codetoanalyze/objc/errors/Makefile index 23be442a2..a0850e557 100644 --- a/infer/tests/codetoanalyze/objc/errors/Makefile +++ b/infer/tests/codetoanalyze/objc/errors/Makefile @@ -25,7 +25,8 @@ SOURCES_DEFAULT = \ field_superclass/B.m \ memory_leaks_benchmark/MemoryLeakRaii.m \ memory_leaks_benchmark/NSMakeCollectableExample.m \ - memory_leaks_benchmark/NSStringInitWithBytesNoCopyExample.m \ + memory_leaks_benchmark/NSString_models_tests.m \ + memory_leaks_benchmark/NSData_models_tests.m \ memory_leaks_benchmark/RetainReleaseExampleBucketing.m \ npe/Fraction.m \ npe/NPD_core_foundation.m \ diff --git a/infer/tests/codetoanalyze/objc/errors/issues.exp b/infer/tests/codetoanalyze/objc/errors/issues.exp index 978824de2..dfa634a43 100644 --- a/infer/tests/codetoanalyze/objc/errors/issues.exp +++ b/infer/tests/codetoanalyze/objc/errors/issues.exp @@ -90,9 +90,9 @@ codetoanalyze/objc/errors/variadic_methods/premature_nil_termination.m, Prematur codetoanalyze/objc/shared/memory_leaks_benchmark/TollBridgeExample.m, TollBridgeExample_brideRetained, 2, MEMORY_LEAK, [start of procedure brideRetained] codetoanalyze/objc/shared/memory_leaks_benchmark/TollBridgeExample.m, TollBridgeExample_bridge, 2, MEMORY_LEAK, [start of procedure bridge] codetoanalyze/objc/shared/memory_leaks_benchmark/TollBridgeExample.m, bridgeDictionaryNoLeak, 1, UNINITIALIZED_VALUE, [start of procedure bridgeDictionaryNoLeak()] -codetoanalyze/objc/errors/memory_leaks_benchmark/NSStringInitWithBytesNoCopyExample.m, StringInitA_macForIV:, 2, MEMORY_LEAK, [start of procedure macForIV:] -codetoanalyze/objc/errors/memory_leaks_benchmark/NSStringInitWithBytesNoCopyExample.m, createURLQueryStringBodyEscaping, 6, PRECONDITION_NOT_MET, [start of procedure createURLQueryStringBodyEscaping(),Condition is true] -codetoanalyze/objc/errors/memory_leaks_benchmark/NSStringInitWithBytesNoCopyExample.m, createURLQueryStringBodyEscaping, 11, UNINITIALIZED_VALUE, [start of procedure createURLQueryStringBodyEscaping(),Condition is false] +codetoanalyze/objc/errors/memory_leaks_benchmark/NSData_models_tests.m, NSData_models_tests_macForIV:, 2, MEMORY_LEAK, [start of procedure macForIV:] +codetoanalyze/objc/errors/memory_leaks_benchmark/NSString_models_tests.m, createURLQueryStringBodyEscaping, 6, PRECONDITION_NOT_MET, [start of procedure createURLQueryStringBodyEscaping(),Condition is true] +codetoanalyze/objc/errors/memory_leaks_benchmark/NSString_models_tests.m, createURLQueryStringBodyEscaping, 11, UNINITIALIZED_VALUE, [start of procedure createURLQueryStringBodyEscaping(),Condition is false] codetoanalyze/objc/errors/memory_leaks_benchmark/RetainReleaseExampleBucketing.m, RetainReleaseTest, 0, RETURN_VALUE_IGNORED, [start of procedure RetainReleaseTest()] codetoanalyze/objc/errors/npe/Fraction.m, test_virtual_call, 7, NULL_DEREFERENCE, [start of procedure test_virtual_call(),start of procedure setNumerator:,return from a call to Fraction_setNumerator:,start of procedure getNumerator,return from a call to Fraction_getNumerator,Condition is true] codetoanalyze/objc/errors/npe/NPD_core_foundation.m, NullDeref_createCloseCrossGlyphNoLeak:, 5, Assert_failure, [start of procedure createCloseCrossGlyphNoLeak:] diff --git a/infer/tests/codetoanalyze/objc/errors/memory_leaks_benchmark/NSData_models_tests.m b/infer/tests/codetoanalyze/objc/errors/memory_leaks_benchmark/NSData_models_tests.m new file mode 100644 index 000000000..e9b4a6d3c --- /dev/null +++ b/infer/tests/codetoanalyze/objc/errors/memory_leaks_benchmark/NSData_models_tests.m @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2014 - present Facebook, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + */ + +#import +#import + +@interface NSData_models_tests : NSObject + +@end + +@implementation NSData_models_tests + ++ (NSData*)randomBytes:(NSUInteger)numOfBytes { + uint8_t* buffer = malloc(numOfBytes); + NSData* data = [NSData dataWithBytesNoCopy:buffer length:numOfBytes]; + if (data) { + return data; + } else { + free(buffer); + return nil; + } +} + +- (NSData*)readDataOfLength:(NSUInteger)length { + size_t bytesLength = length; + void* bytes = malloc(bytesLength); + if (bytes == NULL) { + return nil; + } + return [[NSData alloc] initWithBytesNoCopy:bytes length:5 freeWhenDone:YES]; +} + +- (NSData*)decodedImageData:(size_t)dataLength { + void* data = calloc(dataLength, 1); + return [[NSData alloc] initWithBytesNoCopy:data length:dataLength]; +} + +- (NSData*)macForIV:(NSData*)IV { + uint8_t* result = malloc(10); + return [NSData dataWithBytesNoCopy:result length:10]; +} + +@end diff --git a/infer/tests/codetoanalyze/objc/errors/memory_leaks_benchmark/NSStringInitWithBytesNoCopyExample.m b/infer/tests/codetoanalyze/objc/errors/memory_leaks_benchmark/NSString_models_tests.m similarity index 71% rename from infer/tests/codetoanalyze/objc/errors/memory_leaks_benchmark/NSStringInitWithBytesNoCopyExample.m rename to infer/tests/codetoanalyze/objc/errors/memory_leaks_benchmark/NSString_models_tests.m index dfb62cfb1..3daf462e5 100644 --- a/infer/tests/codetoanalyze/objc/errors/memory_leaks_benchmark/NSStringInitWithBytesNoCopyExample.m +++ b/infer/tests/codetoanalyze/objc/errors/memory_leaks_benchmark/NSString_models_tests.m @@ -30,31 +30,6 @@ NSString* createURLQueryStringBodyEscaping(NSDictionary* parameters, return resultString; } -+ (NSData*)randomBytes:(NSUInteger)numOfBytes { - uint8_t* buffer = malloc(numOfBytes); - NSData* data = [NSData dataWithBytesNoCopy:buffer length:numOfBytes]; - if (data) { - return data; - } else { - free(buffer); - return nil; - } -} - -- (NSData*)readDataOfLength:(NSUInteger)length { - size_t bytesLength = length; - void* bytes = malloc(bytesLength); - if (bytes == NULL) { - return nil; - } - return [[NSData alloc] initWithBytesNoCopy:bytes length:5 freeWhenDone:YES]; -} - -- (NSData*)macForIV:(NSData*)IV { - uint8_t* result = malloc(10); - return [NSData dataWithBytesNoCopy:result length:10]; -} - - (NSString*)hexStringValue { size_t hexLen = 2 * 10 * sizeof(char); char* outString = (char*)malloc(hexLen + 1);