When serializing to an ostream, verify that there were no stream-level errors before returning success.

pull/3335/head
kenton@google.com 15 years ago
parent 28e46df3e9
commit d6e84b3ed8
  1. 7
      src/google/protobuf/message.cc
  2. 15
      src/google/protobuf/message_unittest.cc

@ -168,8 +168,11 @@ bool Message::SerializePartialToFileDescriptor(int file_descriptor) const {
}
bool Message::SerializeToOstream(ostream* output) const {
io::OstreamOutputStream zero_copy_output(output);
return SerializeToZeroCopyStream(&zero_copy_output);
{
io::OstreamOutputStream zero_copy_output(output);
if (!SerializeToZeroCopyStream(&zero_copy_output)) return false;
}
return output->good();
}
bool Message::SerializePartialToOstream(ostream* output) const {

@ -43,6 +43,7 @@
#include <unistd.h>
#endif
#include <sstream>
#include <fstream>
#include <google/protobuf/stubs/common.h>
#include <google/protobuf/io/zero_copy_stream_impl.h>
@ -77,9 +78,9 @@ TEST(MessageTest, SerializeHelpers) {
string str1("foo");
string str2("bar");
message.SerializeToString(&str1);
message.AppendToString(&str2);
message.SerializeToOstream(&stream);
EXPECT_TRUE(message.SerializeToString(&str1));
EXPECT_TRUE(message.AppendToString(&str2));
EXPECT_TRUE(message.SerializeToOstream(&stream));
EXPECT_EQ(str1.size() + 3, str2.size());
EXPECT_EQ("bar", str2.substr(0, 3));
@ -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) {
string filename = TestSourceDir() +
"/google/protobuf/testdata/golden_message";

Loading…
Cancel
Save