aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas Van Lenten <thomasvl@google.com>2018-03-14 17:23:10 -0400
committerGitHub <noreply@github.com>2018-03-14 17:23:10 -0400
commitaae10ed36d4d6c251acdd32fadf565659a596c4e (patch)
treec7fa2fb1f89a927d652dc923e95d5685d268c79d
parent813848d19d58fac043485b61a92e84dcf1a23b56 (diff)
parent1da9ffe39496e59f439b126a232b7ae18a634aee (diff)
downloadprotobuf-aae10ed36d4d6c251acdd32fadf565659a596c4e.tar.gz
protobuf-aae10ed36d4d6c251acdd32fadf565659a596c4e.tar.bz2
protobuf-aae10ed36d4d6c251acdd32fadf565659a596c4e.zip
Merge pull request #4370 from felixjendrusch/objc-output-stream-write-check
Objective-C: Check return value on write of raw pointer
-rw-r--r--objectivec/GPBCodedOutputStream.m5
-rw-r--r--objectivec/Tests/GPBCodedOuputStreamTests.m10
2 files changed, 14 insertions, 1 deletions
diff --git a/objectivec/GPBCodedOutputStream.m b/objectivec/GPBCodedOutputStream.m
index f832e8a6..b846c2fc 100644
--- a/objectivec/GPBCodedOutputStream.m
+++ b/objectivec/GPBCodedOutputStream.m
@@ -942,7 +942,10 @@ static void GPBWriteRawLittleEndian64(GPBOutputBufferState *state,
state_.position = length;
} else {
// Write is very big. Let's do it all at once.
- [state_.output write:((uint8_t *)value) + offset maxLength:length];
+ NSInteger written = [state_.output write:((uint8_t *)value) + offset maxLength:length];
+ if (written != (NSInteger)length) {
+ [NSException raise:GPBCodedOutputStreamException_WriteFailed format:@""];
+ }
}
}
}
diff --git a/objectivec/Tests/GPBCodedOuputStreamTests.m b/objectivec/Tests/GPBCodedOuputStreamTests.m
index 878e7aa9..109239d5 100644
--- a/objectivec/Tests/GPBCodedOuputStreamTests.m
+++ b/objectivec/Tests/GPBCodedOuputStreamTests.m
@@ -423,4 +423,14 @@
}
}
+- (void)testThatItThrowsWhenWriteRawPtrFails {
+ NSOutputStream *output = [NSOutputStream outputStreamToMemory];
+ GPBCodedOutputStream *codedOutput =
+ [GPBCodedOutputStream streamWithOutputStream:output bufferSize:0]; // Skip buffering.
+ [output close]; // Close the output stream to force failure on write.
+ const char *cString = "raw";
+ XCTAssertThrowsSpecificNamed([codedOutput writeRawPtr:cString offset:0 length:strlen(cString)],
+ NSException, GPBCodedOutputStreamException_WriteFailed);
+}
+
@end