[ios] Model NSData initWithBytesNoCopy:data length:dataLength

Reviewed By: akotulski

Differential Revision: D4666223

fbshipit-source-id: 2c88ce8
master
Dulma Churchill 8 years ago committed by Facebook Github Bot
parent b742df7626
commit 612e501dfc

@ -40,6 +40,10 @@ NSData* __objc_alloc(NSData*);
return nil; return nil;
} }
- (instancetype)initWithBytesNoCopy:(void*)bytes length:(NSUInteger)length {
return [self initWithBytesNoCopy:bytes length:length freeWhenDone:YES];
}
- (instancetype)initWithBytesNoCopy:(void*)bytes - (instancetype)initWithBytesNoCopy:(void*)bytes
length:(NSUInteger)length length:(NSUInteger)length
freeWhenDone:(BOOL)flag { freeWhenDone:(BOOL)flag {

@ -25,7 +25,8 @@ SOURCES_DEFAULT = \
field_superclass/B.m \ field_superclass/B.m \
memory_leaks_benchmark/MemoryLeakRaii.m \ memory_leaks_benchmark/MemoryLeakRaii.m \
memory_leaks_benchmark/NSMakeCollectableExample.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 \ memory_leaks_benchmark/RetainReleaseExampleBucketing.m \
npe/Fraction.m \ npe/Fraction.m \
npe/NPD_core_foundation.m \ npe/NPD_core_foundation.m \

@ -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_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, 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/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/NSData_models_tests.m, NSData_models_tests_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/NSString_models_tests.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/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/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/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:] codetoanalyze/objc/errors/npe/NPD_core_foundation.m, NullDeref_createCloseCrossGlyphNoLeak:, 5, Assert_failure, [start of procedure createCloseCrossGlyphNoLeak:]

@ -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 <Foundation/Foundation.h>
#import <stdlib.h>
@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

@ -30,31 +30,6 @@ NSString* createURLQueryStringBodyEscaping(NSDictionary* parameters,
return resultString; 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 { - (NSString*)hexStringValue {
size_t hexLen = 2 * 10 * sizeof(char); size_t hexLen = 2 * 10 * sizeof(char);
char* outString = (char*)malloc(hexLen + 1); char* outString = (char*)malloc(hexLen + 1);
Loading…
Cancel
Save