22
33#include < Python.h>
44
5+ #include < climits>
56#include < memory>
67#include < string>
78
@@ -38,7 +39,7 @@ PythonMessageMutator::~PythonMessageMutator() {
3839 // check.
3940 if (!PyErr_Occurred () && owned_msg_ != nullptr ) {
4041 std::string wire;
41- message_->SerializeToString (&wire);
42+ message_->SerializePartialToString (&wire);
4243 PyObject* py_wire = PyBytes_FromStringAndSize (
4344 wire.data (), static_cast <Py_ssize_t>(wire.size ()));
4445 PyObject* parse =
@@ -81,8 +82,14 @@ bool PythonConstMessagePointer::NotChanged() {
8182 return false ;
8283 }
8384
85+ // Skip the check if too large. Parse won't work
86+ // for messages larger than 2 GB.
87+ if (message_->ByteSizeLong () > INT_MAX) {
88+ return true ;
89+ }
90+
8491 PyObject* py_serialized_pb (
85- PyObject_CallMethod (py_msg_, " SerializeToString " , nullptr ));
92+ PyObject_CallMethod (py_msg_, " SerializePartialToString " , nullptr ));
8693 if (py_serialized_pb == nullptr ) {
8794 PyErr_Format (PyExc_ValueError, " Fail to serialize py_msg" );
8895 return false ;
@@ -99,19 +106,19 @@ bool PythonConstMessagePointer::NotChanged() {
99106 // serialize result may still diff between languages. So parse to
100107 // another c++ message for compare.
101108 std::unique_ptr<google::protobuf::Message> parsed_msg (owned_msg_->New ());
102- parsed_msg->ParseFromArray (data, static_cast <int >(len));
109+ parsed_msg->ParsePartialFromArray (data, static_cast <int >(len));
103110 std::string wire_other;
104111 google::protobuf::io::StringOutputStream stream_other (&wire_other);
105112 google::protobuf::io::CodedOutputStream output_other (&stream_other);
106113 output_other.SetSerializationDeterministic (true );
107- parsed_msg->SerializeToCodedStream (&output_other);
114+ parsed_msg->SerializePartialToCodedStream (&output_other);
108115 output_other.Trim ();
109116
110117 std::string wire;
111118 google::protobuf::io::StringOutputStream stream (&wire);
112119 google::protobuf::io::CodedOutputStream output (&stream);
113120 output.SetSerializationDeterministic (true );
114- owned_msg_->SerializeToCodedStream (&output);
121+ owned_msg_->SerializePartialToCodedStream (&output);
115122 output.Trim ();
116123
117124 if (wire == wire_other) {
0 commit comments