1. Andrew Godwin
  2. protobuf

Commits

ken...@google.com@630680e5-0e50-0410-840e-4b1c322b438d  committed 8dde799

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

  • Participants
  • Parent commits 991c23b
  • Branches default

Comments (0)

Files changed (2)

File src/google/protobuf/message.cc

View file
  • Ignore whitespace
 }
 
 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 {

File src/google/protobuf/message_unittest.cc

View file
  • Ignore whitespace
 #include <unistd.h>
 #endif
 #include <sstream>
+#include <fstream>
 
 #include <google/protobuf/stubs/common.h>
 #include <google/protobuf/io/zero_copy_stream_impl.h>
   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));
 
 }
 
+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";