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 @interface NSArray : NSObject
- (NSArray*)arrayByAddingObject:(id)anObject;
@end @end

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

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

@ -15,8 +15,26 @@
@implementation NSDictionary @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 { + (instancetype)dictionary {
return [NSDictionary alloc]; return [NSDictionary alloc];
} }
+ (instancetype)dictionaryWithObject:(id)object forKey:(id)key {
id a = ((NSObject*)object)->isa;
id b = ((NSObject*)key)->isa;
return [NSDictionary alloc];
}
@end @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> #import <Foundation/NSObject.h>
@class NSIndexSet;
@interface NSMutableArray : NSObject @interface NSMutableArray : NSObject
- (void)replaceObjectAtIndex:(NSUInteger)index withObject:(id)anObject;
- (void)removeObjectsAtIndexes:(NSIndexSet*)indexes;
- (void)replaceObjectsAtIndexes:(NSIndexSet*)indexes
withObjects:(NSArray*)objects;
@end @end

@ -15,19 +15,30 @@
@implementation NSMutableArray @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 { - (void)setObject:(id)anObject atIndexedSubscript:(NSUInteger)index {
NSObject* obj = (NSObject*)anObject; id a = ((NSObject*)anObject)->isa;
id isa = obj->isa;
} }
- (void)addObject:(id)anObject { - (void)addObject:(id)anObject {
NSObject* obj = (NSObject*)anObject; id a = ((NSObject*)anObject)->isa;
id isa = obj->isa;
} }
- (void)insertObject:(id)anObject atIndex:(NSUInteger)index { - (void)insertObject:(id)anObject atIndex:(NSUInteger)index {
NSObject* obj = (NSObject*)anObject; id a = ((NSObject*)anObject)->isa;
id isa = obj->isa;
} }
+ (instancetype)array { + (instancetype)array {

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

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

@ -95,3 +95,52 @@ void no_npe_for_undef_values(NSDictionary* response) {
NSMutableDictionary* d = [NSMutableDictionary dictionary]; NSMutableDictionary* d = [NSMutableDictionary dictionary];
d[@"fds"] = fileInfo; 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", "update_array_with_null",
"add_nil_to_array", "add_nil_to_array",
"insert_nil_in_array", "insert_nil_in_array",
"add_nil_in_dict" "add_nil_in_dict",
"nullable_NSDictionary_objectForKey",
"nullable_NSDictionary_objectForKeyedSubscript",
"nullable_NSMapTable_objectForKey"
}; };
assertThat( assertThat(
"Results should contain null pointer dereference error", "Results should contain null pointer dereference error",

Loading…
Cancel
Save