From c3a0cf74fed14633d77224121f4e3765eacb7de7 Mon Sep 17 00:00:00 2001 From: Thomas Van Lenten Date: Wed, 2 Dec 2015 12:53:35 -0500 Subject: Check and throw errors for nil values/keys (like NSDictionary). --- objectivec/GPBDictionary.m | 141 +++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 136 insertions(+), 5 deletions(-) (limited to 'objectivec/GPBDictionary.m') diff --git a/objectivec/GPBDictionary.m b/objectivec/GPBDictionary.m index 31ee410e..7015b3ee 100644 --- a/objectivec/GPBDictionary.m +++ b/objectivec/GPBDictionary.m @@ -601,7 +601,7 @@ void GPBDictionaryReadEntry(id mapDictionary, //% _dictionary = [[NSMutableDictionary alloc] init]; //% if (count && VNAME##s && keys) { //% for (NSUInteger i = 0; i < count; ++i) { -//% [_dictionary setObject:WRAPPED##VHELPER(VNAME##s[i]) forKey:WRAPPED##KHELPER(keys[i])]; +//%DICTIONARY_VALIDATE_VALUE_##VHELPER(VNAME##s[i], ______)##DICTIONARY_VALIDATE_KEY_##KHELPER(keys[i], ______) [_dictionary setObject:WRAPPED##VHELPER(VNAME##s[i]) forKey:WRAPPED##KHELPER(keys[i])]; //% } //% } //% } @@ -711,7 +711,7 @@ void GPBDictionaryReadEntry(id mapDictionary, //% _validationFunc = (func != NULL ? func : DictDefault_IsValidValue); //% if (count && rawValues && keys) { //% for (NSUInteger i = 0; i < count; ++i) { -//% [_dictionary setObject:WRAPPED##VHELPER(rawValues[i]) forKey:WRAPPED##KHELPER(keys[i])]; +//%DICTIONARY_VALIDATE_KEY_##KHELPER(keys[i], ______) [_dictionary setObject:WRAPPED##VHELPER(rawValues[i]) forKey:WRAPPED##KHELPER(keys[i])]; //% } //% } //% } @@ -776,7 +776,7 @@ void GPBDictionaryReadEntry(id mapDictionary, //%DICTIONARY_MUTABLE_CORE(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER, VHELPER, value, Raw) //% //%- (void)setValue:(VALUE_TYPE)value forKey:(KEY_TYPE##KisP$S##KisP)key { -//% if (!_validationFunc(value)) { +//%DICTIONARY_VALIDATE_KEY_##KHELPER(key, ) if (!_validationFunc(value)) { //% [NSException raise:NSInvalidArgumentException //% format:@"GPB##KEY_NAME##VALUE_NAME##Dictionary: Attempt to set an unknown enum value (%d)", //% value]; @@ -900,7 +900,7 @@ void GPBDictionaryReadEntry(id mapDictionary, //%} //% //%- (void)set##ACCESSOR_NAME##VNAME$u##:(VALUE_TYPE)VNAME forKey:(KEY_TYPE##KisP$S##KisP)key { -//% [_dictionary setObject:WRAPPED##VHELPER(VNAME) forKey:WRAPPED##KHELPER(key)]; +//%DICTIONARY_VALIDATE_VALUE_##VHELPER(VNAME, )##DICTIONARY_VALIDATE_KEY_##KHELPER(key, ) [_dictionary setObject:WRAPPED##VHELPER(VNAME) forKey:WRAPPED##KHELPER(key)]; //% if (_autocreator) { //% GPBAutocreatedDictionaryModified(_autocreator, self); //% } @@ -1171,6 +1171,10 @@ void GPBDictionaryReadEntry(id mapDictionary, //% //%PDDM-DEFINE GPBVALUE_POD(VALUE_NAME) //%value##VALUE_NAME +//%PDDM-DEFINE DICTIONARY_VALIDATE_VALUE_POD(VALUE_NAME, EXTRA_INDENT) +// Empty +//%PDDM-DEFINE DICTIONARY_VALIDATE_KEY_POD(KEY_NAME, EXTRA_INDENT) +// Empty //%PDDM-DEFINE BOOL_DICT_HAS_STORAGE_POD() //% BOOL _valueSet[2]; @@ -1363,7 +1367,18 @@ void GPBDictionaryReadEntry(id mapDictionary, // Empty //%PDDM-DEFINE GPBVALUE_OBJECT(VALUE_NAME) //%valueString - +//%PDDM-DEFINE DICTIONARY_VALIDATE_VALUE_OBJECT(VALUE_NAME, EXTRA_INDENT) +//%##EXTRA_INDENT$S## if (!##VALUE_NAME) { +//%##EXTRA_INDENT$S## [NSException raise:NSInvalidArgumentException +//%##EXTRA_INDENT$S## format:@"Attempting to add nil object to a Dictionary"]; +//%##EXTRA_INDENT$S## } +//% +//%PDDM-DEFINE DICTIONARY_VALIDATE_KEY_OBJECT(KEY_NAME, EXTRA_INDENT) +//%##EXTRA_INDENT$S## if (!##KEY_NAME) { +//%##EXTRA_INDENT$S## [NSException raise:NSInvalidArgumentException +//%##EXTRA_INDENT$S## format:@"Attempting to add nil key to a Dictionary"]; +//%##EXTRA_INDENT$S## } +//% //%PDDM-DEFINE BOOL_DICT_HAS_STORAGE_OBJECT() // Empty @@ -1374,6 +1389,10 @@ void GPBDictionaryReadEntry(id mapDictionary, //% self = [super init]; //% if (self) { //% for (NSUInteger i = 0; i < count; ++i) { +//% if (!objects[i]) { +//% [NSException raise:NSInvalidArgumentException +//% format:@"Attempting to add nil object to a Dictionary"]; +//% } //% int idx = keys[i] ? 1 : 0; //% [_values[idx] release]; //% _values[idx] = (VALUE_TYPE)[objects[i] retain]; @@ -1433,6 +1452,10 @@ void GPBDictionaryReadEntry(id mapDictionary, //%} //% //%- (void)setObject:(VALUE_TYPE)object forKey:(BOOL)key { +//% if (!object) { +//% [NSException raise:NSInvalidArgumentException +//% format:@"Attempting to add nil object to a Dictionary"]; +//% } //% int idx = (key ? 1 : 0); //% [_values[idx] release]; //% _values[idx] = [object retain]; @@ -3239,6 +3262,10 @@ void GPBDictionaryReadEntry(id mapDictionary, _dictionary = [[NSMutableDictionary alloc] init]; if (count && objects && keys) { for (NSUInteger i = 0; i < count; ++i) { + if (!objects[i]) { + [NSException raise:NSInvalidArgumentException + format:@"Attempting to add nil object to a Dictionary"]; + } [_dictionary setObject:objects[i] forKey:@(keys[i])]; } } @@ -3397,6 +3424,10 @@ void GPBDictionaryReadEntry(id mapDictionary, } - (void)setObject:(id)object forKey:(uint32_t)key { + if (!object) { + [NSException raise:NSInvalidArgumentException + format:@"Attempting to add nil object to a Dictionary"]; + } [_dictionary setObject:object forKey:@(key)]; if (_autocreator) { GPBAutocreatedDictionaryModified(_autocreator, self); @@ -5195,6 +5226,10 @@ void GPBDictionaryReadEntry(id mapDictionary, _dictionary = [[NSMutableDictionary alloc] init]; if (count && objects && keys) { for (NSUInteger i = 0; i < count; ++i) { + if (!objects[i]) { + [NSException raise:NSInvalidArgumentException + format:@"Attempting to add nil object to a Dictionary"]; + } [_dictionary setObject:objects[i] forKey:@(keys[i])]; } } @@ -5353,6 +5388,10 @@ void GPBDictionaryReadEntry(id mapDictionary, } - (void)setObject:(id)object forKey:(int32_t)key { + if (!object) { + [NSException raise:NSInvalidArgumentException + format:@"Attempting to add nil object to a Dictionary"]; + } [_dictionary setObject:object forKey:@(key)]; if (_autocreator) { GPBAutocreatedDictionaryModified(_autocreator, self); @@ -7151,6 +7190,10 @@ void GPBDictionaryReadEntry(id mapDictionary, _dictionary = [[NSMutableDictionary alloc] init]; if (count && objects && keys) { for (NSUInteger i = 0; i < count; ++i) { + if (!objects[i]) { + [NSException raise:NSInvalidArgumentException + format:@"Attempting to add nil object to a Dictionary"]; + } [_dictionary setObject:objects[i] forKey:@(keys[i])]; } } @@ -7309,6 +7352,10 @@ void GPBDictionaryReadEntry(id mapDictionary, } - (void)setObject:(id)object forKey:(uint64_t)key { + if (!object) { + [NSException raise:NSInvalidArgumentException + format:@"Attempting to add nil object to a Dictionary"]; + } [_dictionary setObject:object forKey:@(key)]; if (_autocreator) { GPBAutocreatedDictionaryModified(_autocreator, self); @@ -9107,6 +9154,10 @@ void GPBDictionaryReadEntry(id mapDictionary, _dictionary = [[NSMutableDictionary alloc] init]; if (count && objects && keys) { for (NSUInteger i = 0; i < count; ++i) { + if (!objects[i]) { + [NSException raise:NSInvalidArgumentException + format:@"Attempting to add nil object to a Dictionary"]; + } [_dictionary setObject:objects[i] forKey:@(keys[i])]; } } @@ -9265,6 +9316,10 @@ void GPBDictionaryReadEntry(id mapDictionary, } - (void)setObject:(id)object forKey:(int64_t)key { + if (!object) { + [NSException raise:NSInvalidArgumentException + format:@"Attempting to add nil object to a Dictionary"]; + } [_dictionary setObject:object forKey:@(key)]; if (_autocreator) { GPBAutocreatedDictionaryModified(_autocreator, self); @@ -9336,6 +9391,10 @@ void GPBDictionaryReadEntry(id mapDictionary, _dictionary = [[NSMutableDictionary alloc] init]; if (count && values && keys) { for (NSUInteger i = 0; i < count; ++i) { + if (!keys[i]) { + [NSException raise:NSInvalidArgumentException + format:@"Attempting to add nil key to a Dictionary"]; + } [_dictionary setObject:@(values[i]) forKey:keys[i]]; } } @@ -9474,6 +9533,10 @@ void GPBDictionaryReadEntry(id mapDictionary, } - (void)setValue:(uint32_t)value forKey:(NSString *)key { + if (!key) { + [NSException raise:NSInvalidArgumentException + format:@"Attempting to add nil key to a Dictionary"]; + } [_dictionary setObject:@(value) forKey:key]; if (_autocreator) { GPBAutocreatedDictionaryModified(_autocreator, self); @@ -9542,6 +9605,10 @@ void GPBDictionaryReadEntry(id mapDictionary, _dictionary = [[NSMutableDictionary alloc] init]; if (count && values && keys) { for (NSUInteger i = 0; i < count; ++i) { + if (!keys[i]) { + [NSException raise:NSInvalidArgumentException + format:@"Attempting to add nil key to a Dictionary"]; + } [_dictionary setObject:@(values[i]) forKey:keys[i]]; } } @@ -9680,6 +9747,10 @@ void GPBDictionaryReadEntry(id mapDictionary, } - (void)setValue:(int32_t)value forKey:(NSString *)key { + if (!key) { + [NSException raise:NSInvalidArgumentException + format:@"Attempting to add nil key to a Dictionary"]; + } [_dictionary setObject:@(value) forKey:key]; if (_autocreator) { GPBAutocreatedDictionaryModified(_autocreator, self); @@ -9748,6 +9819,10 @@ void GPBDictionaryReadEntry(id mapDictionary, _dictionary = [[NSMutableDictionary alloc] init]; if (count && values && keys) { for (NSUInteger i = 0; i < count; ++i) { + if (!keys[i]) { + [NSException raise:NSInvalidArgumentException + format:@"Attempting to add nil key to a Dictionary"]; + } [_dictionary setObject:@(values[i]) forKey:keys[i]]; } } @@ -9886,6 +9961,10 @@ void GPBDictionaryReadEntry(id mapDictionary, } - (void)setValue:(uint64_t)value forKey:(NSString *)key { + if (!key) { + [NSException raise:NSInvalidArgumentException + format:@"Attempting to add nil key to a Dictionary"]; + } [_dictionary setObject:@(value) forKey:key]; if (_autocreator) { GPBAutocreatedDictionaryModified(_autocreator, self); @@ -9954,6 +10033,10 @@ void GPBDictionaryReadEntry(id mapDictionary, _dictionary = [[NSMutableDictionary alloc] init]; if (count && values && keys) { for (NSUInteger i = 0; i < count; ++i) { + if (!keys[i]) { + [NSException raise:NSInvalidArgumentException + format:@"Attempting to add nil key to a Dictionary"]; + } [_dictionary setObject:@(values[i]) forKey:keys[i]]; } } @@ -10092,6 +10175,10 @@ void GPBDictionaryReadEntry(id mapDictionary, } - (void)setValue:(int64_t)value forKey:(NSString *)key { + if (!key) { + [NSException raise:NSInvalidArgumentException + format:@"Attempting to add nil key to a Dictionary"]; + } [_dictionary setObject:@(value) forKey:key]; if (_autocreator) { GPBAutocreatedDictionaryModified(_autocreator, self); @@ -10160,6 +10247,10 @@ void GPBDictionaryReadEntry(id mapDictionary, _dictionary = [[NSMutableDictionary alloc] init]; if (count && values && keys) { for (NSUInteger i = 0; i < count; ++i) { + if (!keys[i]) { + [NSException raise:NSInvalidArgumentException + format:@"Attempting to add nil key to a Dictionary"]; + } [_dictionary setObject:@(values[i]) forKey:keys[i]]; } } @@ -10298,6 +10389,10 @@ void GPBDictionaryReadEntry(id mapDictionary, } - (void)setValue:(BOOL)value forKey:(NSString *)key { + if (!key) { + [NSException raise:NSInvalidArgumentException + format:@"Attempting to add nil key to a Dictionary"]; + } [_dictionary setObject:@(value) forKey:key]; if (_autocreator) { GPBAutocreatedDictionaryModified(_autocreator, self); @@ -10366,6 +10461,10 @@ void GPBDictionaryReadEntry(id mapDictionary, _dictionary = [[NSMutableDictionary alloc] init]; if (count && values && keys) { for (NSUInteger i = 0; i < count; ++i) { + if (!keys[i]) { + [NSException raise:NSInvalidArgumentException + format:@"Attempting to add nil key to a Dictionary"]; + } [_dictionary setObject:@(values[i]) forKey:keys[i]]; } } @@ -10504,6 +10603,10 @@ void GPBDictionaryReadEntry(id mapDictionary, } - (void)setValue:(float)value forKey:(NSString *)key { + if (!key) { + [NSException raise:NSInvalidArgumentException + format:@"Attempting to add nil key to a Dictionary"]; + } [_dictionary setObject:@(value) forKey:key]; if (_autocreator) { GPBAutocreatedDictionaryModified(_autocreator, self); @@ -10572,6 +10675,10 @@ void GPBDictionaryReadEntry(id mapDictionary, _dictionary = [[NSMutableDictionary alloc] init]; if (count && values && keys) { for (NSUInteger i = 0; i < count; ++i) { + if (!keys[i]) { + [NSException raise:NSInvalidArgumentException + format:@"Attempting to add nil key to a Dictionary"]; + } [_dictionary setObject:@(values[i]) forKey:keys[i]]; } } @@ -10710,6 +10817,10 @@ void GPBDictionaryReadEntry(id mapDictionary, } - (void)setValue:(double)value forKey:(NSString *)key { + if (!key) { + [NSException raise:NSInvalidArgumentException + format:@"Attempting to add nil key to a Dictionary"]; + } [_dictionary setObject:@(value) forKey:key]; if (_autocreator) { GPBAutocreatedDictionaryModified(_autocreator, self); @@ -10802,6 +10913,10 @@ void GPBDictionaryReadEntry(id mapDictionary, _validationFunc = (func != NULL ? func : DictDefault_IsValidValue); if (count && rawValues && keys) { for (NSUInteger i = 0; i < count; ++i) { + if (!keys[i]) { + [NSException raise:NSInvalidArgumentException + format:@"Attempting to add nil key to a Dictionary"]; + } [_dictionary setObject:@(rawValues[i]) forKey:keys[i]]; } } @@ -10982,6 +11097,10 @@ void GPBDictionaryReadEntry(id mapDictionary, } - (void)setRawValue:(int32_t)value forKey:(NSString *)key { + if (!key) { + [NSException raise:NSInvalidArgumentException + format:@"Attempting to add nil key to a Dictionary"]; + } [_dictionary setObject:@(value) forKey:key]; if (_autocreator) { GPBAutocreatedDictionaryModified(_autocreator, self); @@ -10997,6 +11116,10 @@ void GPBDictionaryReadEntry(id mapDictionary, } - (void)setValue:(int32_t)value forKey:(NSString *)key { + if (!key) { + [NSException raise:NSInvalidArgumentException + format:@"Attempting to add nil key to a Dictionary"]; + } if (!_validationFunc(value)) { [NSException raise:NSInvalidArgumentException format:@"GPBStringEnumDictionary: Attempt to set an unknown enum value (%d)", @@ -12754,6 +12877,10 @@ void GPBDictionaryReadEntry(id mapDictionary, self = [super init]; if (self) { for (NSUInteger i = 0; i < count; ++i) { + if (!objects[i]) { + [NSException raise:NSInvalidArgumentException + format:@"Attempting to add nil object to a Dictionary"]; + } int idx = keys[i] ? 1 : 0; [_values[idx] release]; _values[idx] = (id)[objects[i] retain]; @@ -12932,6 +13059,10 @@ void GPBDictionaryReadEntry(id mapDictionary, } - (void)setObject:(id)object forKey:(BOOL)key { + if (!object) { + [NSException raise:NSInvalidArgumentException + format:@"Attempting to add nil object to a Dictionary"]; + } int idx = (key ? 1 : 0); [_values[idx] release]; _values[idx] = [object retain]; -- cgit v1.2.3