@@ -746,7 +746,9 @@ static void DecodeSingleValueFromInputStream(GPBExtensionDescriptor *extension,
746
746
if (GPBExtensionIsWireFormat (description)) {
747
747
// For MessageSet fields the message length will have already been
748
748
// read.
749
- [targetMessage mergeFromCodedInputStream: input extensionRegistry: extensionRegistry];
749
+ [targetMessage mergeFromCodedInputStream: input
750
+ extensionRegistry: extensionRegistry
751
+ endingTag: 0 ];
750
752
} else {
751
753
[input readMessage: targetMessage extensionRegistry: extensionRegistry];
752
754
}
@@ -1027,7 +1029,7 @@ - (instancetype)initWithCodedInputStream:(GPBCodedInputStream *)input
1027
1029
error:(NSError **)errorPtr {
1028
1030
if ((self = [self init ])) {
1029
1031
@try {
1030
- [self mergeFromCodedInputStream: input extensionRegistry: extensionRegistry];
1032
+ [self mergeFromCodedInputStream: input extensionRegistry: extensionRegistry endingTag: 0 ];
1031
1033
if (errorPtr) {
1032
1034
*errorPtr = nil ;
1033
1035
}
@@ -2078,8 +2080,7 @@ - (void)clearExtension:(GPBExtensionDescriptor *)extension {
2078
2080
- (void )mergeFromData:(NSData *)data extensionRegistry:(id <GPBExtensionRegistry>)extensionRegistry {
2079
2081
GPBCodedInputStream *input = [[GPBCodedInputStream alloc ] initWithData: data];
2080
2082
@try {
2081
- [self mergeFromCodedInputStream: input extensionRegistry: extensionRegistry];
2082
- [input checkLastTagWas: 0 ];
2083
+ [self mergeFromCodedInputStream: input extensionRegistry: extensionRegistry endingTag: 0 ];
2083
2084
} @finally {
2084
2085
[input release ];
2085
2086
}
@@ -2090,7 +2091,7 @@ - (BOOL)mergeFromData:(NSData *)data
2090
2091
error:(NSError **)errorPtr {
2091
2092
GPBCodedInputStream *input = [[GPBCodedInputStream alloc ] initWithData: data];
2092
2093
@try {
2093
- [self mergeFromCodedInputStream: input extensionRegistry: extensionRegistry];
2094
+ [self mergeFromCodedInputStream: input extensionRegistry: extensionRegistry endingTag: 0 ];
2094
2095
[input checkLastTagWas: 0 ];
2095
2096
if (errorPtr) {
2096
2097
*errorPtr = nil ;
@@ -2227,38 +2228,36 @@ - (void)parseMessageSet:(GPBCodedInputStream *)input
2227
2228
}
2228
2229
}
2229
2230
2230
- - (BOOL )parseUnknownField:(GPBCodedInputStream *)input
2231
+ - (void )parseUnknownField:(GPBCodedInputStream *)input
2231
2232
extensionRegistry:(id <GPBExtensionRegistry>)extensionRegistry
2232
2233
tag:(uint32_t )tag {
2233
- GPBWireFormat wireType = GPBWireFormatGetTagWireType (tag);
2234
2234
int32_t fieldNumber = GPBWireFormatGetTagFieldNumber (tag);
2235
-
2236
2235
GPBDescriptor *descriptor = [self descriptor ];
2237
2236
GPBExtensionDescriptor *extension = [extensionRegistry extensionForDescriptor: descriptor
2238
2237
fieldNumber: fieldNumber];
2239
2238
if (extension == nil ) {
2240
2239
if (descriptor.wireFormat && GPBWireFormatMessageSetItemTag == tag) {
2241
2240
[self parseMessageSet: input extensionRegistry: extensionRegistry];
2242
- return YES ;
2241
+ return ;
2243
2242
}
2244
2243
} else {
2244
+ GPBWireFormat wireType = GPBWireFormatGetTagWireType (tag);
2245
2245
if (extension.wireType == wireType) {
2246
2246
ExtensionMergeFromInputStream (extension, extension.packable , input, extensionRegistry, self);
2247
- return YES ;
2247
+ return ;
2248
2248
}
2249
2249
// Primitive, repeated types can be packed on unpacked on the wire, and are
2250
2250
// parsed either way.
2251
2251
if ([extension isRepeated ] && !GPBDataTypeIsObject (extension->description_ ->dataType ) &&
2252
2252
(extension.alternateWireType == wireType)) {
2253
2253
ExtensionMergeFromInputStream (extension, !extension.packable , input, extensionRegistry, self);
2254
- return YES ;
2254
+ return ;
2255
2255
}
2256
2256
}
2257
- if ([GPBUnknownFieldSet isFieldTag: tag]) {
2258
- GPBUnknownFieldSet *unknownFields = GetOrMakeUnknownFields (self);
2259
- return [unknownFields mergeFieldFrom: tag input: input];
2260
- } else {
2261
- return NO ;
2257
+ GPBUnknownFieldSet *unknownFields = GetOrMakeUnknownFields (self);
2258
+ if (![unknownFields mergeFieldFrom: tag input: input]) {
2259
+ [NSException raise: NSInternalInconsistencyException
2260
+ format: @" Internal Error: Unable to parse unknown field %u " , tag];
2262
2261
}
2263
2262
}
2264
2263
@@ -2465,7 +2464,12 @@ static void MergeRepeatedNotPackedFieldFromCodedInputStream(
2465
2464
}
2466
2465
2467
2466
- (void )mergeFromCodedInputStream:(GPBCodedInputStream *)input
2468
- extensionRegistry:(id <GPBExtensionRegistry>)extensionRegistry {
2467
+ extensionRegistry:(id <GPBExtensionRegistry>)extensionRegistry
2468
+ endingTag:(uint32_t )endingTag {
2469
+ #if defined(DEBUG) && DEBUG
2470
+ NSAssert (endingTag == 0 || GPBWireFormatGetTagWireType(endingTag) == GPBWireFormatEndGroup,
2471
+ @"endingTag should have been an endGroup tag");
2472
+ #endif // DEBUG
2469
2473
GPBDescriptor *descriptor = [self descriptor ];
2470
2474
GPBCodedInputStreamState *state = &input->state_ ;
2471
2475
uint32_t tag = 0 ;
@@ -2475,8 +2479,11 @@ - (void)mergeFromCodedInputStream:(GPBCodedInputStream *)input
2475
2479
while (YES ) {
2476
2480
BOOL merged = NO ;
2477
2481
tag = GPBCodedInputStreamReadTag (state);
2478
- if (tag == 0 ) {
2479
- break ; // Reached end.
2482
+ if (tag == endingTag || tag == 0 ) {
2483
+ // If we got to the end (tag zero), when we were expecting the end group, this will
2484
+ // raise the error.
2485
+ GPBCodedInputStreamCheckLastTagWas (state, endingTag);
2486
+ return ;
2480
2487
}
2481
2488
for (NSUInteger i = 0 ; i < numFields; ++i) {
2482
2489
if (startingIndex >= numFields) startingIndex = 0 ;
@@ -2514,46 +2521,37 @@ - (void)mergeFromCodedInputStream:(GPBCodedInputStream *)input
2514
2521
}
2515
2522
} // for(i < numFields)
2516
2523
2517
- if (!merged && (tag != 0 )) {
2518
- // Primitive, repeated types can be packed on unpacked on the wire, and
2519
- // are parsed either way. The above loop covered tag in the preferred
2520
- // for, so this need to check the alternate form.
2521
- for (NSUInteger i = 0 ; i < numFields; ++i) {
2522
- if (startingIndex >= numFields) startingIndex = 0 ;
2523
- GPBFieldDescriptor *fieldDescriptor = fields[startingIndex];
2524
- if ((fieldDescriptor.fieldType == GPBFieldTypeRepeated) &&
2525
- !GPBFieldDataTypeIsObject (fieldDescriptor) &&
2526
- (GPBFieldAlternateTag (fieldDescriptor) == tag)) {
2527
- BOOL alternateIsPacked = !fieldDescriptor.isPackable ;
2528
- if (alternateIsPacked) {
2529
- MergeRepeatedPackedFieldFromCodedInputStream (self, fieldDescriptor, input);
2530
- // Well formed protos will only have a repeated field that is
2531
- // packed once, advance the starting index to the next field.
2532
- startingIndex += 1 ;
2533
- } else {
2534
- MergeRepeatedNotPackedFieldFromCodedInputStream (self, fieldDescriptor, input,
2535
- extensionRegistry);
2536
- }
2537
- merged = YES ;
2538
- break ;
2539
- } else {
2524
+ if (merged) continue ; // On to the next tag
2525
+
2526
+ // Primitive, repeated types can be packed or unpacked on the wire, and
2527
+ // are parsed either way. The above loop covered tag in the preferred
2528
+ // for, so this need to check the alternate form.
2529
+ for (NSUInteger i = 0 ; i < numFields; ++i) {
2530
+ if (startingIndex >= numFields) startingIndex = 0 ;
2531
+ GPBFieldDescriptor *fieldDescriptor = fields[startingIndex];
2532
+ if ((fieldDescriptor.fieldType == GPBFieldTypeRepeated) &&
2533
+ !GPBFieldDataTypeIsObject (fieldDescriptor) &&
2534
+ (GPBFieldAlternateTag (fieldDescriptor) == tag)) {
2535
+ BOOL alternateIsPacked = !fieldDescriptor.isPackable ;
2536
+ if (alternateIsPacked) {
2537
+ MergeRepeatedPackedFieldFromCodedInputStream (self, fieldDescriptor, input);
2538
+ // Well formed protos will only have a repeated field that is
2539
+ // packed once, advance the starting index to the next field.
2540
2540
startingIndex += 1 ;
2541
+ } else {
2542
+ MergeRepeatedNotPackedFieldFromCodedInputStream (self, fieldDescriptor, input,
2543
+ extensionRegistry);
2541
2544
}
2545
+ merged = YES ;
2546
+ break ;
2547
+ } else {
2548
+ startingIndex += 1 ;
2542
2549
}
2543
2550
}
2544
2551
2545
- if (!merged) {
2546
- if (tag == 0 ) {
2547
- // zero signals EOF / limit reached
2548
- return ;
2549
- } else {
2550
- if (![self parseUnknownField: input extensionRegistry: extensionRegistry tag: tag]) {
2551
- // it's an endgroup tag
2552
- return ;
2553
- }
2554
- }
2555
- } // if(!merged)
2552
+ if (merged) continue ; // On to the next tag
2556
2553
2554
+ [self parseUnknownField: input extensionRegistry: extensionRegistry tag: tag];
2557
2555
} // while(YES)
2558
2556
}
2559
2557
@@ -3554,8 +3552,7 @@ - (instancetype)initWithCoder:(NSCoder *)aDecoder {
3554
3552
if (data.length ) {
3555
3553
GPBCodedInputStream *input = [[GPBCodedInputStream alloc ] initWithData: data];
3556
3554
@try {
3557
- [self mergeFromCodedInputStream: input extensionRegistry: nil ];
3558
- [input checkLastTagWas: 0 ];
3555
+ [self mergeFromCodedInputStream: input extensionRegistry: nil endingTag: 0 ];
3559
3556
} @finally {
3560
3557
[input release ];
3561
3558
}
0 commit comments