Commit d6e84b3e authored by kenton@google.com's avatar kenton@google.com

When serializing to an ostream, verify that there were no stream-level errors…

When serializing to an ostream, verify that there were no stream-level errors before returning success.
parent 28e46df3
...@@ -168,8 +168,11 @@ bool Message::SerializePartialToFileDescriptor(int file_descriptor) const { ...@@ -168,8 +168,11 @@ bool Message::SerializePartialToFileDescriptor(int file_descriptor) const {
} }
bool Message::SerializeToOstream(ostream* output) const { bool Message::SerializeToOstream(ostream* output) const {
{
io::OstreamOutputStream zero_copy_output(output); io::OstreamOutputStream zero_copy_output(output);
return SerializeToZeroCopyStream(&zero_copy_output); if (!SerializeToZeroCopyStream(&zero_copy_output)) return false;
}
return output->good();
} }
bool Message::SerializePartialToOstream(ostream* output) const { bool Message::SerializePartialToOstream(ostream* output) const {
......
...@@ -43,6 +43,7 @@ ...@@ -43,6 +43,7 @@
#include <unistd.h> #include <unistd.h>
#endif #endif
#include <sstream> #include <sstream>
#include <fstream>
#include <google/protobuf/stubs/common.h> #include <google/protobuf/stubs/common.h>
#include <google/protobuf/io/zero_copy_stream_impl.h> #include <google/protobuf/io/zero_copy_stream_impl.h>
...@@ -77,9 +78,9 @@ TEST(MessageTest, SerializeHelpers) { ...@@ -77,9 +78,9 @@ TEST(MessageTest, SerializeHelpers) {
string str1("foo"); string str1("foo");
string str2("bar"); string str2("bar");
message.SerializeToString(&str1); EXPECT_TRUE(message.SerializeToString(&str1));
message.AppendToString(&str2); EXPECT_TRUE(message.AppendToString(&str2));
message.SerializeToOstream(&stream); EXPECT_TRUE(message.SerializeToOstream(&stream));
EXPECT_EQ(str1.size() + 3, str2.size()); EXPECT_EQ(str1.size() + 3, str2.size());
EXPECT_EQ("bar", str2.substr(0, 3)); EXPECT_EQ("bar", str2.substr(0, 3));
...@@ -95,6 +96,14 @@ TEST(MessageTest, SerializeHelpers) { ...@@ -95,6 +96,14 @@ TEST(MessageTest, SerializeHelpers) {
} }
TEST(MessageTest, SerializeToBrokenOstream) {
ofstream out;
protobuf_unittest::TestAllTypes message;
message.set_optional_int32(123);
EXPECT_FALSE(message.SerializeToOstream(&out));
}
TEST(MessageTest, ParseFromFileDescriptor) { TEST(MessageTest, ParseFromFileDescriptor) {
string filename = TestSourceDir() + string filename = TestSourceDir() +
"/google/protobuf/testdata/golden_message"; "/google/protobuf/testdata/golden_message";
......
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