kidok / protobuf

Automatically exported from code.google.com/p/protobuf
0 stars 0 forks source link

FatalException on calling SerializeAsString() #593

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
Constellation:
Protobuf 2.5.0
Ubuntu 12.04 LTS
C++ API

Problem: 
Protobuf randomly throws a FatalException on calling SerializeAsString() with 
this output:
[libprotobuf FATAL google/protobuf/message_lite.cc:224] CHECK failed: 
!coded_out.HadError():
terminate called after throwing an instance of 
'google::protobuf::FatalException'
  what(): CHECK failed: !coded_out.HadError()

This happens on every 10th or 100th call of SerializeAsString().

This is the source code in which the SerializeAsString is called:

QByteArray ProtobufSerializer::serialize(AbstractTransferable* transferable) 
throw(NexusDataLinkException) {
    ProtoEnvelop envelop;
    ProtoPayloadWrapper* wrapper = envelop.add_payloadwrapper();

    ChannelResponse* response = static_cast<ChannelResponse*>(transferable);
    ProtoResponse* allocatedProtoResponse(wrapper->mutable_channelresponse());
    populateProtoResponse(response, allocatedProtoResponse, transferable->getDataContainer());

    envelop.set_destination_id(CommonDefinitions::SERVER_ID);
    envelop.set_source_id(agentId);

    // convert to byte array
    std::string dataString = envelop.SerializeAsString();
    QByteArray data = QByteArray(dataString.data(), dataString.size()); // makes a deep copy of the data contained in dataString

    return data;
}

What could be the cause of this error? I could not find anything similar on 
google & Co.

Kind regards, 
Manfred

Original issue reported on code.google.com by manfred....@posteo.de on 3 Jan 2014 at 6:33

GoogleCodeExporter commented 9 years ago
Additional notes:
The error seems to happen only for embedded messages that contain a bool value. 
Other embedded messages seem to work as expected

Original comment by manfred....@posteo.de on 4 Jan 2014 at 12:50

GoogleCodeExporter commented 9 years ago
Did such an error occur before? Please let me know if you require additional 
information or the .proto-files

Original comment by manfred....@posteo.de on 6 Jan 2014 at 4:20

GoogleCodeExporter commented 9 years ago
Program throwing exceptions indeterminately suggests multi-threading bugs.

Original comment by xiaof...@google.com on 10 Jan 2014 at 12:27

GoogleCodeExporter commented 9 years ago

This exact same issue occurs for me too - and when I replace the usage of all 
bools with uint32s, the problem goes away.

Looking through the source code, it seems that the only way in which the 
_has_error flag can be set is if _Next returns false. The check that dies is in 
message_lite.cc:224, and from a cursor examination the only code path that 
would cause the error is if the return value of GetCachedSize() is insufficient 
to serialise the message.

The reason this happens with bools is that a bool is always considered to be 
one byte, but if your bool value is unitialised (as mine was) then when 
protobuf actually tries to serialise the value it may end up using more than 
one byte.

IMHO protobuf should assert if you try to set a value other than 0 or 1 into a 
bool. This would catch the error where it actually occurs, rather than later on 
when it attempts to serialise the data.

Original comment by dave.b...@gmail.com on 2 Jul 2014 at 10:53