Serialization errors in size (cpp)

20 views
Skip to first unread message

Zi Wang

unread,
Feb 20, 2020, 2:22:03 AM2/20/20
to Protocol Buffers
Hi,

I have been using protobuf for a while and it worked properly. Recently I have written a new project but has this serialization issue. Here is the example:

In the .proto file, I have this definition:
------------
syntax = "proto2";

package diff_input;

message Diff{
    message File{
        required bool val = 1;
    }
    required File file = 1;
}
------------

After compiling it to .cc and .h files, I have this simple test:
-------------------------
using namespace std;
using namespace google::protobuf::io;
using namespace diff_input;

int main(){
    Diff* file = new Diff();
    Diff_File* input = new Diff_File();
    input->set_val(true);
    file->set_allocated_file(input);
    int outfd = open("serial_file", O_RDWR | O_CREAT|O_TRUNC , 0666);
    if(outfd == -1){
        std::cout<<"Open Error\n";
    }
    ZeroCopyOutputStream* output = new FileOutputStream(outfd);
    CodedOutputStream *p_stream = new CodedOutputStream(output);
    file->SerializeWithCachedSizes(p_stream);
    delete(p_stream);
    delete(output);       
    close(outfd);
    return 1;
}
-------------------------

When I run the executable, I received an error
----------------------
[libprotobuf FATAL ~/build/external.protobuf/src/external.protobuf/src/google/protobuf/wire_format_lite.cc:580] CHECK failed: (end - target) == (size):
Aborted
----------------------

However, if I commented " input->set_val(true);", I would not receive any errors.

My protoc executable is of version "libprotoc 3.5.1".

Many thanks!

Zi Wang

unread,
Feb 20, 2020, 2:34:31 AM2/20/20
to Protocol Buffers
Okay, it seems that before calling "SerializeWithCachedSizes", I need to call "ByteSize()", according to "https://github.com/protocolbuffers/protobuf/issues/5026", which fixes this problem.
Reply all
Reply to author
Forward
0 new messages