Skip to content

Commit

Permalink
Error during parsing for invalid UTF-8 instead of dropping dropping d…
Browse files Browse the repository at this point in the history
…ata.

This seems to be some code evolution side effects. Back when there was a custom
string class, we couldn't really error when we finally saw the string was bad
so we had to return the empty string, but now that full validation is done
up front, it can error out.
  • Loading branch information
thomasvl committed Apr 5, 2016
1 parent f3f5b3f commit c9167f2
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
7 changes: 4 additions & 3 deletions objectivec/GPBCodedInputStream.m
Original file line number Diff line number Diff line change
Expand Up @@ -219,15 +219,16 @@ int32_t GPBCodedInputStreamReadTag(GPBCodedInputStreamState *state) {
result = [[NSString alloc] initWithBytes:&state->bytes[state->bufferPos]
length:size
encoding:NSUTF8StringEncoding];
state->bufferPos += size;
if (!result) {
result = @"";
#ifdef DEBUG
// https://developers.google.com/protocol-buffers/docs/proto#scalar
NSLog(@"UTF8 failure, is some field type 'string' when it should be "
NSLog(@"UTF-8 failure, is some field type 'string' when it should be "
@"'bytes'?");
#endif
[NSException raise:NSParseErrorException
format:@"Invalid UTF-8 for a 'string'"];
}
state->bufferPos += size;
}
return result;
}
Expand Down
11 changes: 5 additions & 6 deletions objectivec/Tests/GPBCodedInputStreamTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -283,16 +283,15 @@ - (void)testReadMalformedString {
[output writeRawData:[NSData dataWithBytes:bytes length:sizeof(bytes)]];
[output flush];

NSData* data =
NSData *data =
[rawOutput propertyForKey:NSStreamDataWrittenToMemoryStreamKey];
GPBCodedInputStream* input = [GPBCodedInputStream streamWithData:data];
NSError *error = nil;
TestAllTypes* message = [TestAllTypes parseFromCodedInputStream:input
extensionRegistry:nil
error:NULL];
XCTAssertNotNil(message);
// Make sure we can read string properties twice without crashing.
XCTAssertEqual([message.defaultString length], (NSUInteger)0);
XCTAssertEqualObjects(@"", message.defaultString);
error:&error];
XCTAssertNotNil(error);
XCTAssertNil(message);
}

- (void)testBOMWithinStrings {
Expand Down

0 comments on commit c9167f2

Please sign in to comment.