diff --git a/objectivec/GPBMessage.h b/objectivec/GPBMessage.h index 90d6991de2..2d9b67ebd3 100644 --- a/objectivec/GPBMessage.h +++ b/objectivec/GPBMessage.h @@ -510,10 +510,11 @@ CF_EXTERN_C_END * If the intent is to *replace* the message's unknown fields, call `-clearUnknownFields` first. * * Since the data from the GPBUnknownFields will always be well formed, this call will almost never - * fail. What could cause it to fail is if the GPBUnknownFields contains a field values it is - * and error for the message's schema - i.e.: if it contains a length delimited field where the + * fail. What could cause it to fail is if the GPBUnknownFields contains a field value that is + * an error for the message's schema - i.e.: if it contains a length delimited field where the * field number for the message is defined to be a _string_ field, however the length delimited - * data provide is not a valid UTF8 string. + * data provide is not a valid UTF8 string, or if the field is a _packed_ number field, but the + * data provided is not a valid for that field. * * @param unknownFields The unknown fields to merge the data from. * @param extensionRegistry The extension registry to use to look up extensions, can be `nil`. diff --git a/objectivec/Tests/GPBUnknownFieldsTest.m b/objectivec/Tests/GPBUnknownFieldsTest.m index c1e5e10634..42c3f7dc87 100644 --- a/objectivec/Tests/GPBUnknownFieldsTest.m +++ b/objectivec/Tests/GPBUnknownFieldsTest.m @@ -911,16 +911,32 @@ } - (void)testMergeFailures { - // Valid data, pushes to the string just fine. + // Valid data, pushes to the fields just fine. { GPBUnknownFields* ufs = [[[GPBUnknownFields alloc] init] autorelease]; [ufs addFieldNumber:TestAllTypes_FieldNumber_OptionalString lengthDelimited:DataFromCStr("abc")]; + [ufs addFieldNumber:TestAllTypes_FieldNumber_RepeatedInt32Array + lengthDelimited:DataFromBytes(0x01, 0x02)]; + [ufs addFieldNumber:TestAllTypes_FieldNumber_RepeatedFixed32Array + lengthDelimited:DataFromBytes(0x03, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00)]; + [ufs addFieldNumber:TestAllTypes_FieldNumber_RepeatedFixed64Array + lengthDelimited:DataFromBytes(0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00)]; TestAllTypes* msg = [TestAllTypes message]; NSError* error = nil; XCTAssertTrue([msg mergeUnknownFields:ufs extensionRegistry:nil error:&error]); XCTAssertNil(error); XCTAssertEqualObjects(msg.optionalString, @"abc"); + XCTAssertEqual(msg.repeatedInt32Array.count, 2); + XCTAssertEqual([msg.repeatedInt32Array valueAtIndex:0], 1); + XCTAssertEqual([msg.repeatedInt32Array valueAtIndex:1], 2); + XCTAssertEqual(msg.repeatedFixed32Array.count, 2); + XCTAssertEqual([msg.repeatedFixed32Array valueAtIndex:0], 3); + XCTAssertEqual([msg.repeatedFixed32Array valueAtIndex:1], 4); + XCTAssertEqual(msg.repeatedFixed64Array.count, 2); + XCTAssertEqual([msg.repeatedFixed64Array valueAtIndex:0], 5); + XCTAssertEqual([msg.repeatedFixed64Array valueAtIndex:1], 6); } // Invalid UTF-8 causes a failure when pushed to the message. @@ -933,6 +949,39 @@ XCTAssertFalse([msg mergeUnknownFields:ufs extensionRegistry:nil error:&error]); XCTAssertNotNil(error); } + + // Invalid packed varint causes a failure when pushed to the message. + { + GPBUnknownFields* ufs = [[[GPBUnknownFields alloc] init] autorelease]; + [ufs addFieldNumber:TestAllTypes_FieldNumber_RepeatedInt32Array + lengthDelimited:DataFromBytes(0xff)]; // Invalid varint + TestAllTypes* msg = [TestAllTypes message]; + NSError* error = nil; + XCTAssertFalse([msg mergeUnknownFields:ufs extensionRegistry:nil error:&error]); + XCTAssertNotNil(error); + } + + // Invalid packed fixed32 causes a failure when pushed to the message. + { + GPBUnknownFields* ufs = [[[GPBUnknownFields alloc] init] autorelease]; + [ufs addFieldNumber:TestAllTypes_FieldNumber_RepeatedFixed32Array + lengthDelimited:DataFromBytes(0x01, 0x00, 0x00)]; // Truncated fixed32 + TestAllTypes* msg = [TestAllTypes message]; + NSError* error = nil; + XCTAssertFalse([msg mergeUnknownFields:ufs extensionRegistry:nil error:&error]); + XCTAssertNotNil(error); + } + + // Invalid packed fixed64 causes a failure when pushed to the message. + { + GPBUnknownFields* ufs = [[[GPBUnknownFields alloc] init] autorelease]; + [ufs addFieldNumber:TestAllTypes_FieldNumber_RepeatedFixed64Array + lengthDelimited:DataFromBytes(0x01, 0x00, 0x00, 0x00, 0x00)]; // Truncated fixed64 + TestAllTypes* msg = [TestAllTypes message]; + NSError* error = nil; + XCTAssertFalse([msg mergeUnknownFields:ufs extensionRegistry:nil error:&error]); + XCTAssertNotNil(error); + } } - (void)testLargeVarint {