Commit a230b5d2 authored by Thomas Van Lenten's avatar Thomas Van Lenten Committed by GitHub

Rename methods to avoid ObjC KVC collisions. (#1699)

Note: Breaking API change on the Dictionary classes.

The numeric value classes were using "Value" in the naming, but this silently
collided with the KVC category on NSObject; meaning KVC code could break up a
keypath and call these selectors with the wrong types leading to crashes (even
though the code all would compile cleanly).

- Rename the methods to use the "type" instead of literal "Value".
- Update all the impls and tests.
- Enable the warning that will catch issues like this in the future.

Fixes https://github.com/google/protobuf/issues/1616
parent 1a5333b8
This diff is collapsed.
This diff is collapsed.
......@@ -31,6 +31,14 @@
// If you want to build protocol buffers in your own project without adding the
// project dependency, you can just add this file.
// This warning seems to treat code differently when it is #imported than when
// it is inline in the file. GPBDictionary.m compiles cleanly in other targets,
// but when #imported here it triggers a bunch of warnings that don't make
// much sense, and don't trigger when compiled directly. So we shut off the
// warnings here.
#pragma clang diagnostic ignored "-Wnullability-completeness"
#import "GPBArray.m"
#import "GPBCodedInputStream.m"
#import "GPBCodedOutputStream.m"
......
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
......@@ -987,7 +987,7 @@ static NSData *DataFromCStr(const char *str) {
TestMap *msg = [[TestMap alloc] initWithData:data error:NULL];
XCTAssertEqual(msg.mapInt32Int32.count, 1U);
int32_t val = 666;
XCTAssertTrue([msg.mapInt32Int32 valueForKey:1 value:&val]);
XCTAssertTrue([msg.mapInt32Int32 getInt32:&val forKey:1]);
XCTAssertEqual(val, 1);
[msg release];
......@@ -1001,7 +1001,7 @@ static NSData *DataFromCStr(const char *str) {
TestMap *msg = [[TestMap alloc] initWithData:data error:NULL];
XCTAssertEqual(msg.mapInt32Int32.count, 1U);
int32_t val = 666;
XCTAssertTrue([msg.mapInt32Int32 valueForKey:2 value:&val]);
XCTAssertTrue([msg.mapInt32Int32 getInt32:&val forKey:2]);
XCTAssertEqual(val, 1);
[msg release];
......@@ -1015,7 +1015,7 @@ static NSData *DataFromCStr(const char *str) {
TestMap *msg = [[TestMap alloc] initWithData:data error:NULL];
XCTAssertEqual(msg.mapInt32Int32.count, 1U);
int32_t val = 666;
XCTAssertTrue([msg.mapInt32Int32 valueForKey:2 value:&val]);
XCTAssertTrue([msg.mapInt32Int32 getInt32:&val forKey:2]);
XCTAssertEqual(val, 1);
[msg release];
......@@ -1029,7 +1029,7 @@ static NSData *DataFromCStr(const char *str) {
TestMap *msg = [[TestMap alloc] initWithData:data error:NULL];
XCTAssertEqual(msg.mapInt32Int32.count, 1U);
int32_t val = 666;
XCTAssertTrue([msg.mapInt32Int32 valueForKey:1 value:&val]);
XCTAssertTrue([msg.mapInt32Int32 getInt32:&val forKey:1]);
XCTAssertEqual(val, 2);
[msg release];
......@@ -1043,7 +1043,7 @@ static NSData *DataFromCStr(const char *str) {
TestMap *msg = [[TestMap alloc] initWithData:data error:NULL];
XCTAssertEqual(msg.mapInt32Int32.count, 1U);
int32_t val = 666;
XCTAssertTrue([msg.mapInt32Int32 valueForKey:0 value:&val]);
XCTAssertTrue([msg.mapInt32Int32 getInt32:&val forKey:0]);
XCTAssertEqual(val, 1);
[msg release];
......@@ -1057,7 +1057,7 @@ static NSData *DataFromCStr(const char *str) {
TestMap *msg = [[TestMap alloc] initWithData:data error:NULL];
XCTAssertEqual(msg.mapInt32Int32.count, 1U);
int32_t val = 666;
XCTAssertTrue([msg.mapInt32Int32 valueForKey:1 value:&val]);
XCTAssertTrue([msg.mapInt32Int32 getInt32:&val forKey:1]);
XCTAssertEqual(val, 0);
[msg release];
......@@ -1071,7 +1071,7 @@ static NSData *DataFromCStr(const char *str) {
TestMap *msg = [[TestMap alloc] initWithData:data error:NULL];
XCTAssertEqual(msg.mapInt32Int32.count, 1U);
int32_t val = 666;
XCTAssertTrue([msg.mapInt32Int32 valueForKey:2 value:&val]);
XCTAssertTrue([msg.mapInt32Int32 getInt32:&val forKey:2]);
XCTAssertEqual(val, 3);
[msg release];
......@@ -1098,9 +1098,9 @@ static NSData *DataFromCStr(const char *str) {
dictionaryWithValidationFunction:Proto2MapEnumPlusExtra_IsValidValue];
orig.unknownMapField = [GPBInt32EnumDictionary
dictionaryWithValidationFunction:Proto2MapEnumPlusExtra_IsValidValue];
[orig.knownMapField setValue:Proto2MapEnumPlusExtra_EProto2MapEnumFoo
[orig.knownMapField setEnum:Proto2MapEnumPlusExtra_EProto2MapEnumFoo
forKey:0];
[orig.unknownMapField setValue:Proto2MapEnumPlusExtra_EProto2MapEnumExtra
[orig.unknownMapField setEnum:Proto2MapEnumPlusExtra_EProto2MapEnumExtra
forKey:0];
NSData *data = [orig data];
......@@ -1108,7 +1108,7 @@ static NSData *DataFromCStr(const char *str) {
TestEnumMap *msg1 = [TestEnumMap parseFromData:data error:NULL];
XCTAssertEqual(msg1.knownMapField.count, 1U);
int32_t val = -1;
XCTAssertTrue([msg1.knownMapField valueForKey:0 value:&val]);
XCTAssertTrue([msg1.knownMapField getEnum:&val forKey:0]);
XCTAssertEqual(val, Proto2MapEnum_Proto2MapEnumFoo);
XCTAssertEqual(msg1.unknownFields.countOfFields, 1U);
......@@ -1117,11 +1117,11 @@ static NSData *DataFromCStr(const char *str) {
[TestEnumMapPlusExtra parseFromData:data error:NULL];
val = -1;
XCTAssertEqual(msg2.knownMapField.count, 1U);
XCTAssertTrue([msg2.knownMapField valueForKey:0 value:&val]);
XCTAssertTrue([msg2.knownMapField getEnum:&val forKey:0]);
XCTAssertEqual(val, Proto2MapEnumPlusExtra_EProto2MapEnumFoo);
val = -1;
XCTAssertEqual(msg2.unknownMapField.count, 1U);
XCTAssertTrue([msg2.unknownMapField valueForKey:0 value:&val]);
XCTAssertTrue([msg2.unknownMapField getEnum:&val forKey:0]);
XCTAssertEqual(val, Proto2MapEnumPlusExtra_EProto2MapEnumExtra);
XCTAssertEqual(msg2.unknownFields.countOfFields, 0U);
......@@ -1137,32 +1137,32 @@ static NSData *DataFromCStr(const char *str) {
// Key/Value data should result in different byte lengths on wire to ensure
// everything is right.
[msg.mapInt32Int32 setValue:1000 forKey:200];
[msg.mapInt32Int32 setValue:101 forKey:2001];
[msg.mapInt64Int64 setValue:1002 forKey:202];
[msg.mapInt64Int64 setValue:103 forKey:2003];
[msg.mapUint32Uint32 setValue:1004 forKey:204];
[msg.mapUint32Uint32 setValue:105 forKey:2005];
[msg.mapUint64Uint64 setValue:1006 forKey:206];
[msg.mapUint64Uint64 setValue:107 forKey:2007];
[msg.mapSint32Sint32 setValue:1008 forKey:208];
[msg.mapSint32Sint32 setValue:109 forKey:2009];
[msg.mapSint64Sint64 setValue:1010 forKey:210];
[msg.mapSint64Sint64 setValue:111 forKey:2011];
[msg.mapFixed32Fixed32 setValue:1012 forKey:212];
[msg.mapFixed32Fixed32 setValue:113 forKey:2013];
[msg.mapFixed64Fixed64 setValue:1014 forKey:214];
[msg.mapFixed64Fixed64 setValue:115 forKey:2015];
[msg.mapSfixed32Sfixed32 setValue:1016 forKey:216];
[msg.mapSfixed32Sfixed32 setValue:117 forKey:2017];
[msg.mapSfixed64Sfixed64 setValue:1018 forKey:218];
[msg.mapSfixed64Sfixed64 setValue:119 forKey:2019];
[msg.mapInt32Float setValue:1020.f forKey:220];
[msg.mapInt32Float setValue:121.f forKey:2021];
[msg.mapInt32Double setValue:1022. forKey:222];
[msg.mapInt32Double setValue:123. forKey:2023];
[msg.mapBoolBool setValue:false forKey:true];
[msg.mapBoolBool setValue:true forKey:false];
[msg.mapInt32Int32 setInt32:1000 forKey:200];
[msg.mapInt32Int32 setInt32:101 forKey:2001];
[msg.mapInt64Int64 setInt64:1002 forKey:202];
[msg.mapInt64Int64 setInt64:103 forKey:2003];
[msg.mapUint32Uint32 setUInt32:1004 forKey:204];
[msg.mapUint32Uint32 setUInt32:105 forKey:2005];
[msg.mapUint64Uint64 setUInt64:1006 forKey:206];
[msg.mapUint64Uint64 setUInt64:107 forKey:2007];
[msg.mapSint32Sint32 setInt32:1008 forKey:208];
[msg.mapSint32Sint32 setInt32:109 forKey:2009];
[msg.mapSint64Sint64 setInt64:1010 forKey:210];
[msg.mapSint64Sint64 setInt64:111 forKey:2011];
[msg.mapFixed32Fixed32 setUInt32:1012 forKey:212];
[msg.mapFixed32Fixed32 setUInt32:113 forKey:2013];
[msg.mapFixed64Fixed64 setUInt64:1014 forKey:214];
[msg.mapFixed64Fixed64 setUInt64:115 forKey:2015];
[msg.mapSfixed32Sfixed32 setInt32:1016 forKey:216];
[msg.mapSfixed32Sfixed32 setInt32:117 forKey:2017];
[msg.mapSfixed64Sfixed64 setInt64:1018 forKey:218];
[msg.mapSfixed64Sfixed64 setInt64:119 forKey:2019];
[msg.mapInt32Float setFloat:1020.f forKey:220];
[msg.mapInt32Float setFloat:121.f forKey:2021];
[msg.mapInt32Double setDouble:1022. forKey:222];
[msg.mapInt32Double setDouble:123. forKey:2023];
[msg.mapBoolBool setBool:false forKey:true];
[msg.mapBoolBool setBool:true forKey:false];
msg.mapStringString[@"224"] = @"1024";
msg.mapStringString[@"2025"] = @"125";
msg.mapStringBytes[@"226"] = DataFromCStr("1026");
......@@ -1171,12 +1171,12 @@ static NSData *DataFromCStr(const char *str) {
val1.optionalInt32 = 1028;
Message2 *val2 = [[Message2 alloc] init];
val2.optionalInt32 = 129;
[msg.mapStringMessage setValue:val1 forKey:@"228"];
[msg.mapStringMessage setValue:val2 forKey:@"2029"];
[msg.mapStringMessage setObject:val1 forKey:@"228"];
[msg.mapStringMessage setObject:val2 forKey:@"2029"];
[msg.mapInt32Bytes setObject:DataFromCStr("1030 bytes") forKey:230];
[msg.mapInt32Bytes setObject:DataFromCStr("131") forKey:2031];
[msg.mapInt32Enum setValue:Message2_Enum_Bar forKey:232];
[msg.mapInt32Enum setValue:Message2_Enum_Baz forKey:2033];
[msg.mapInt32Enum setEnum:Message2_Enum_Bar forKey:232];
[msg.mapInt32Enum setEnum:Message2_Enum_Baz forKey:2033];
Message2 *val3 = [[Message2 alloc] init];
val3.optionalInt32 = 1034;
Message2 *val4 = [[Message2 alloc] init];
......
......@@ -1171,7 +1171,7 @@
XCTAssertFalse([message2.a hasA]);
// But adding an element to the map should.
[message.a.a.iToI setValue:100 forKey:200];
[message.a.a.iToI setInt32:100 forKey:200];
XCTAssertTrue([message hasA]);
XCTAssertTrue([message.a hasA]);
XCTAssertEqual([message.a.a.iToI count], (NSUInteger)1);
......@@ -1190,7 +1190,7 @@
message1a.a.iToI = message1b.a.iToI;
XCTAssertTrue([message1a hasA]);
XCTAssertFalse([message1b hasA]);
[message1a.a.iToI setValue:1 forKey:2];
[message1a.a.iToI setInt32:1 forKey:2];
XCTAssertTrue([message1a hasA]);
XCTAssertTrue([message1b hasA]);
XCTAssertEqual(message1a.a.iToI, message1b.a.iToI);
......@@ -1224,7 +1224,7 @@
// with different objects that are equal).
TestRecursiveMessageWithRepeatedField *message3 =
[TestRecursiveMessageWithRepeatedField message];
message3.iToI = [GPBInt32Int32Dictionary dictionaryWithValue:10 forKey:20];
message3.iToI = [GPBInt32Int32Dictionary dictionaryWithInt32:10 forKey:20];
message3.strToStr =
[NSMutableDictionary dictionaryWithObject:@"abc" forKey:@"123"];
XCTAssertNotNil(message.iToI);
......@@ -1292,7 +1292,7 @@
XCTAssertFalse([message hasA]);
GPBInt32Int32Dictionary *iToI = [message.a.iToI retain];
XCTAssertEqual(iToI->_autocreator, message.a); // Pointer comparision
message.a.iToI = [GPBInt32Int32Dictionary dictionaryWithValue:6 forKey:7];
message.a.iToI = [GPBInt32Int32Dictionary dictionaryWithInt32:6 forKey:7];
XCTAssertTrue([message hasA]);
XCTAssertNotEqual(message.a.iToI, iToI); // Pointer comparision
XCTAssertNil(iToI->_autocreator);
......
......@@ -53,12 +53,12 @@ class GPBBridgeTests: XCTestCase {
msg.repeatedStringArray.addObject("pqr")
msg.repeatedEnumArray.addValue(Message2_Enum.Bar.rawValue)
msg.repeatedEnumArray.addValue(Message2_Enum.Baz.rawValue)
msg.mapInt32Int32.setValue(400, forKey:500)
msg.mapInt32Int32.setValue(401, forKey:501)
msg.mapInt32Int32.setInt32(400, forKey:500)
msg.mapInt32Int32.setInt32(401, forKey:501)
msg.mapStringString.setObject("foo", forKey:"bar")
msg.mapStringString.setObject("abc", forKey:"xyz")
msg.mapInt32Enum.setValue(Message2_Enum.Bar.rawValue, forKey:600)
msg.mapInt32Enum.setValue(Message2_Enum.Baz.rawValue, forKey:601)
msg.mapInt32Enum.setEnum(Message2_Enum.Bar.rawValue, forKey:600)
msg.mapInt32Enum.setEnum(Message2_Enum.Baz.rawValue, forKey:601)
// Check has*.
XCTAssertTrue(msg.hasOptionalInt32)
......@@ -90,18 +90,18 @@ class GPBBridgeTests: XCTestCase {
XCTAssertEqual(msg.repeatedEnumArray.valueAtIndex(1), Message2_Enum.Baz.rawValue)
XCTAssertEqual(msg.repeatedInt64Array.count, UInt(0))
XCTAssertEqual(msg.mapInt32Int32.count, UInt(2))
var intValue: Int32 = 0;
XCTAssertTrue(msg.mapInt32Int32.valueForKey(500, value:&intValue))
var intValue: Int32 = 0
XCTAssertTrue(msg.mapInt32Int32.getInt32(&intValue, forKey: 500))
XCTAssertEqual(intValue, Int32(400))
XCTAssertTrue(msg.mapInt32Int32.valueForKey(501, value:&intValue))
XCTAssertTrue(msg.mapInt32Int32.getInt32(&intValue, forKey: 501))
XCTAssertEqual(intValue, Int32(401))
XCTAssertEqual(msg.mapStringString.count, Int(2))
XCTAssertEqual(msg.mapStringString.objectForKey("bar") as? String, "foo")
XCTAssertEqual(msg.mapStringString.objectForKey("xyz") as? String, "abc")
XCTAssertEqual(msg.mapInt32Enum.count, UInt(2))
XCTAssertTrue(msg.mapInt32Enum.valueForKey(600, value:&intValue))
XCTAssertTrue(msg.mapInt32Enum.getEnum(&intValue, forKey:600))
XCTAssertEqual(intValue, Message2_Enum.Bar.rawValue)
XCTAssertTrue(msg.mapInt32Enum.valueForKey(601, value:&intValue))
XCTAssertTrue(msg.mapInt32Enum.getEnum(&intValue, forKey:601))
XCTAssertEqual(intValue, Message2_Enum.Baz.rawValue)
// Clearing a string with nil.
......@@ -151,11 +151,11 @@ class GPBBridgeTests: XCTestCase {
msg.repeatedEnumArray.addValue(Message3_Enum.Bar.rawValue)
msg.repeatedEnumArray.addRawValue(666)
SetMessage3_OptionalEnum_RawValue(msg2, 666)
msg.mapInt32Int32.setValue(400, forKey:500)
msg.mapInt32Int32.setValue(401, forKey:501)
msg.mapInt32Int32.setInt32(400, forKey:500)
msg.mapInt32Int32.setInt32(401, forKey:501)
msg.mapStringString.setObject("foo", forKey:"bar")
msg.mapStringString.setObject("abc", forKey:"xyz")
msg.mapInt32Enum.setValue(Message2_Enum.Bar.rawValue, forKey:600)
msg.mapInt32Enum.setEnum(Message2_Enum.Bar.rawValue, forKey:600)
// "proto3" syntax lets enum get unknown values.
msg.mapInt32Enum.setRawValue(666, forKey:601)
......@@ -183,20 +183,20 @@ class GPBBridgeTests: XCTestCase {
XCTAssertEqual(msg2.optionalEnum, Message3_Enum.GPBUnrecognizedEnumeratorValue)
XCTAssertEqual(Message3_OptionalEnum_RawValue(msg2), Int32(666))
XCTAssertEqual(msg.mapInt32Int32.count, UInt(2))
var intValue: Int32 = 0;
XCTAssertTrue(msg.mapInt32Int32.valueForKey(500, value:&intValue))
var intValue: Int32 = 0
XCTAssertTrue(msg.mapInt32Int32.getInt32(&intValue, forKey:500))
XCTAssertEqual(intValue, Int32(400))
XCTAssertTrue(msg.mapInt32Int32.valueForKey(501, value:&intValue))
XCTAssertTrue(msg.mapInt32Int32.getInt32(&intValue, forKey:501))
XCTAssertEqual(intValue, Int32(401))
XCTAssertEqual(msg.mapStringString.count, Int(2))
XCTAssertEqual(msg.mapStringString.objectForKey("bar") as? String, "foo")
XCTAssertEqual(msg.mapStringString.objectForKey("xyz") as? String, "abc")
XCTAssertEqual(msg.mapInt32Enum.count, UInt(2))
XCTAssertTrue(msg.mapInt32Enum.valueForKey(600, value:&intValue))
XCTAssertTrue(msg.mapInt32Enum.getEnum(&intValue, forKey:600))
XCTAssertEqual(intValue, Message2_Enum.Bar.rawValue)
XCTAssertTrue(msg.mapInt32Enum.valueForKey(601, value:&intValue))
XCTAssertTrue(msg.mapInt32Enum.getEnum(&intValue, forKey:601))
XCTAssertEqual(intValue, Message3_Enum.GPBUnrecognizedEnumeratorValue.rawValue)
XCTAssertTrue(msg.mapInt32Enum.valueForKey(601, rawValue:&intValue))
XCTAssertTrue(msg.mapInt32Enum.getRawValue(&intValue, forKey:601))
XCTAssertEqual(intValue, 666)
// Clearing a string with nil.
......@@ -439,8 +439,8 @@ class GPBBridgeTests: XCTestCase {
msg.optionalGroup.a = 102
msg.repeatedStringArray.addObject("abc")
msg.repeatedStringArray.addObject("def")
msg.mapInt32Int32.setValue(200, forKey:300)
msg.mapInt32Int32.setValue(201, forKey:201)
msg.mapInt32Int32.setInt32(200, forKey:300)
msg.mapInt32Int32.setInt32(201, forKey:201)
msg.mapStringString.setObject("foo", forKey:"bar")
msg.mapStringString.setObject("abc", forKey:"xyz")
......
......@@ -1089,19 +1089,19 @@ const uint32_t kGPBDefaultRepeatCount = 2;
- (void)setAllMapFields:(TestMap *)message numEntries:(uint32_t)count {
for (uint32_t i = 0; i < count; i++) {
[message.mapInt32Int32 setValue:(i + 1) forKey:100 + i * 100];
[message.mapInt64Int64 setValue:(i + 1) forKey:101 + i * 100];
[message.mapUint32Uint32 setValue:(i + 1) forKey:102 + i * 100];
[message.mapUint64Uint64 setValue:(i + 1) forKey:103 + i * 100];
[message.mapSint32Sint32 setValue:(i + 1) forKey:104 + i * 100];
[message.mapSint64Sint64 setValue:(i + 1) forKey:105 + i * 100];
[message.mapFixed32Fixed32 setValue:(i + 1) forKey:106 + i * 100];
[message.mapFixed64Fixed64 setValue:(i + 1) forKey:107 + i * 100];
[message.mapSfixed32Sfixed32 setValue:(i + 1) forKey:108 + i * 100];
[message.mapSfixed64Sfixed64 setValue:(i + 1) forKey:109 + i * 100];
[message.mapInt32Float setValue:(i + 1) forKey:110 + i * 100];
[message.mapInt32Double setValue:(i + 1) forKey:111 + i * 100];
[message.mapBoolBool setValue:((i % 2) == 1) forKey:((i % 2) == 0)];
[message.mapInt32Int32 setInt32:(i + 1) forKey:100 + i * 100];
[message.mapInt64Int64 setInt64:(i + 1) forKey:101 + i * 100];
[message.mapUint32Uint32 setUInt32:(i + 1) forKey:102 + i * 100];
[message.mapUint64Uint64 setUInt64:(i + 1) forKey:103 + i * 100];
[message.mapSint32Sint32 setInt32:(i + 1) forKey:104 + i * 100];
[message.mapSint64Sint64 setInt64:(i + 1) forKey:105 + i * 100];
[message.mapFixed32Fixed32 setUInt32:(i + 1) forKey:106 + i * 100];
[message.mapFixed64Fixed64 setUInt64:(i + 1) forKey:107 + i * 100];
[message.mapSfixed32Sfixed32 setInt32:(i + 1) forKey:108 + i * 100];
[message.mapSfixed64Sfixed64 setInt64:(i + 1) forKey:109 + i * 100];
[message.mapInt32Float setFloat:(i + 1) forKey:110 + i * 100];
[message.mapInt32Double setDouble:(i + 1) forKey:111 + i * 100];
[message.mapBoolBool setBool:((i % 2) == 1) forKey:((i % 2) == 0)];
NSString *keyStr = [[NSString alloc] initWithFormat:@"%d", 112 + i * 100];
NSString *dataStr = [[NSString alloc] initWithFormat:@"%d", i + 1];
......@@ -1114,7 +1114,7 @@ const uint32_t kGPBDefaultRepeatCount = 2;
[data release];
[message.mapInt32Enum
setValue:(i % 2) ? MapEnum_MapEnumBar : MapEnum_MapEnumBaz
setEnum:(i % 2) ? MapEnum_MapEnumBar : MapEnum_MapEnumBaz
forKey:114 + i * 100];
ForeignMessage *subMsg = [[ForeignMessage alloc] init];
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment