ParseFromString fails for message containing a single required float

1,217 views
Skip to first unread message

Nikolas Engelhard

unread,
Aug 15, 2013, 11:48:42 AM8/15/13
to prot...@googlegroups.com

Hello

I have a strange problem and need some assistance: I just started using protobuf and transmit the serialized messages via TCP-Sockets from Python to C++. This works very well if I don't use floats in the .proto files. As soon as a message is read, it is parsed via ParseFromString(buffer).
I used so far the addressbook.proto and amessage.proto from the examples and everything worked well. I the just wrote a new proto containing a single required float and ParseFromString now returns false and also fails to extract the float value from the message.
If I just replace the float with an int32, the message is correctly parsed and ParseFromString returns True. I have no idea what I'm doing wrong, so it would be great if someone could help me out.

Cheers,
   Nikolas

Christopher Head

unread,
Aug 15, 2013, 4:22:58 PM8/15/13
to Nikolas Engelhard, prot...@googlegroups.com
You never mentioned what data type “buffer” is. The ParseFromString
function takes an std::string. If “buffer” is an array of char/unsigned
char/uint8_t/something similar, you are implicitly invoking the
std::string constructor taking a single const char* parameter, and that
constructor constructs the std::string by assuming the pointed-to
buffer is NUL-terminated, which Protobuf data is not (Protobuf encoded
data can contain bytes whose values are zero). If your buffer is indeed
an array, use ParseFromArray instead of ParseFromString, and pass the
proper size (Protobuf messages are not inherently length-prefixed nor
delimited, so you must pass the size of the encoded data yourself).

Chris
signature.asc

Nikolas Engelhard

unread,
Aug 16, 2013, 9:22:09 AM8/16/13
to Christopher Head, prot...@googlegroups.com
Thank you, that sounds very convincing. Buffer is in deed just a uchar array. I already prepend the length of the message before I send it via TCP, so I can easily pass this value to ParseFromArray. Thanks :)


2013/8/15 Christopher Head <hea...@gmail.com>
Reply all
Reply to author
Forward
0 new messages