Additional modeling of NS collections

Summary:public
Add modeling of methods that can raise exceptions when parameters are
nil, and that return nil when passed nil.

Reviewed By: dulmarod

Differential Revision: D3101739

fb-gh-sync-id: 76af5a2
fbshipit-source-id: 76af5a2
master
Josh Berdine 9 years ago committed by Facebook Github Bot 1
parent e9cb9f4352
commit 6278dc7200

@ -11,4 +11,6 @@
@interface NSArray : NSObject
- (NSArray*)arrayByAddingObject:(id)anObject;
@end

@ -11,12 +11,17 @@
@implementation NSArray
- (NSArray*)arrayByAddingObject:(id)anObject {
id a = ((NSObject*)anObject)->isa;
return [NSArray alloc];
}
+ (instancetype)array {
return [NSArray alloc];
}
+ (instancetype)arrayWithObject:(char*)anObject {
char _ = *anObject;
id a = ((NSObject*)anObject)->isa;
return [NSArray alloc];
}

@ -11,4 +11,10 @@
@interface NSDictionary : NSObject
- (id)objectForKey:(id)aKey;
- (id)objectForKeyedSubscript:(id)key;
+ (instancetype)dictionary;
+ (instancetype)dictionaryWithObject:(id)object forKey:(id)key;
@end

@ -15,8 +15,26 @@
@implementation NSDictionary
- (id)objectForKey:(id)aKey {
if (aKey == NULL)
return NULL;
return [NSObject alloc];
}
- (id)objectForKeyedSubscript:(id)aKey {
if (aKey == NULL)
return NULL;
return [NSObject alloc];
}
+ (instancetype)dictionary {
return [NSDictionary alloc];
}
+ (instancetype)dictionaryWithObject:(id)object forKey:(id)key {
id a = ((NSObject*)object)->isa;
id b = ((NSObject*)key)->isa;
return [NSDictionary alloc];
}
@end

@ -0,0 +1,16 @@
/*
* Copyright (c) 2016 - 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/NSObject.h>
@interface NSMapTable : NSObject
- (id)objectForKey:(id)aKey;
@end

@ -0,0 +1,20 @@
/*
* Copyright (c) 2016 - 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 "NSMapTable.h"
@implementation NSMapTable
- (id)objectForKey:(id)aKey {
if (aKey == NULL)
return NULL;
return [NSObject alloc];
}
@end

@ -9,6 +9,13 @@
#import <Foundation/NSObject.h>
@class NSIndexSet;
@interface NSMutableArray : NSObject
- (void)replaceObjectAtIndex:(NSUInteger)index withObject:(id)anObject;
- (void)removeObjectsAtIndexes:(NSIndexSet*)indexes;
- (void)replaceObjectsAtIndexes:(NSIndexSet*)indexes
withObjects:(NSArray*)objects;
@end

@ -15,19 +15,30 @@
@implementation NSMutableArray
- (void)replaceObjectAtIndex:(NSUInteger)index withObject:(id)anObject {
id a = ((NSObject*)anObject)->isa;
}
- (void)removeObjectsAtIndexes:(NSIndexSet*)indexes {
id a = ((NSObject*)indexes)->isa;
}
- (void)replaceObjectsAtIndexes:(NSIndexSet*)indexes
withObjects:(NSArray*)objects {
id a = ((NSObject*)indexes)->isa;
id b = ((NSObject*)objects)->isa;
}
- (void)setObject:(id)anObject atIndexedSubscript:(NSUInteger)index {
NSObject* obj = (NSObject*)anObject;
id isa = obj->isa;
id a = ((NSObject*)anObject)->isa;
}
- (void)addObject:(id)anObject {
NSObject* obj = (NSObject*)anObject;
id isa = obj->isa;
id a = ((NSObject*)anObject)->isa;
}
- (void)insertObject:(id)anObject atIndex:(NSUInteger)index {
NSObject* obj = (NSObject*)anObject;
id isa = obj->isa;
id a = ((NSObject*)anObject)->isa;
}
+ (instancetype)array {

@ -11,4 +11,7 @@
@interface NSMutableDictionary : NSObject
- (void)removeObjectForKey:(id)aKey;
+ (NSMutableDictionary*)dictionaryWithSharedKeySet:(id)keyset;
@end

@ -15,20 +15,25 @@
@implementation NSMutableDictionary
- (void)removeObjectForKey:(id)aKey {
id a = ((NSObject*)aKey)->isa;
}
- (void)setObject:(id)object forKeyedSubscript:(id)aKey {
NSObject* key = (NSObject*)aKey;
id isa2 = key->isa;
id a = ((NSObject*)aKey)->isa;
}
- (void)setObject:(id)anObject forKey:(id)aKey {
NSObject* obj = (NSObject*)anObject;
id isa = obj->isa;
NSObject* key = (NSObject*)aKey;
id isa2 = key->isa;
id a = ((NSObject*)anObject)->isa;
id b = ((NSObject*)aKey)->isa;
}
+ (instancetype)dictionary {
return [NSMutableDictionary alloc];
}
+ (NSMutableDictionary*)dictionaryWithSharedKeySet:(id)keyset {
id a = ((NSObject*)keyset)->isa;
}
@end

@ -95,3 +95,52 @@ void no_npe_for_undef_values(NSDictionary* response) {
NSMutableDictionary* d = [NSMutableDictionary dictionary];
d[@"fds"] = fileInfo;
}
void nullable_NSDictionary_objectForKey(NSDictionary* dict, NSObject* key) {
if (key != nil) {
// check objectForKey may return nil for non-nil key
if ([dict objectForKey:key] == nil) {
char _ = *(char*)nil;
} // report
}
}
void strict_NSDictionary_objectForKey(NSDictionary* dict) {
// check objectForKey returns nil for nil key
if ([dict objectForKey:nil] != nil) {
char _ = *(char*)nil;
} // no report
}
void nullable_NSDictionary_objectForKeyedSubscript(NSDictionary* dict,
NSObject* key) {
if (key != nil) {
// check objectForKey may return nil for non-nil key
if (dict[key] == nil) {
char _ = *(char*)nil;
} // report
}
}
void strict_NSDictionary_objectForKeyedSubscript(NSDictionary* dict) {
// check objectForKey returns nil for nil key
if (dict[nil] != nil) {
char _ = *(char*)nil;
} // no report
}
void nullable_NSMapTable_objectForKey(NSMapTable* map, NSObject* key) {
if (key != nil) {
// check objectForKey may return nil for non-nil key
if ([map objectForKey:key] == nil) {
char _ = *(char*)nil;
} // report
}
}
void strict_NSMapTable_objectForKey(NSMapTable* map) {
// check objectForKey returns nil for nil key
if ([map objectForKey:nil] != nil) {
char _ = *(char*)nil;
} // no report
}

@ -56,7 +56,10 @@ public class UpdateDictNPETest {
"update_array_with_null",
"add_nil_to_array",
"insert_nil_in_array",
"add_nil_in_dict"
"add_nil_in_dict",
"nullable_NSDictionary_objectForKey",
"nullable_NSDictionary_objectForKeyedSubscript",
"nullable_NSMapTable_objectForKey"
};
assertThat(
"Results should contain null pointer dereference error",

Loading…
Cancel
Save