views:

110

answers:

1

I'm grabbing some binary data out of my MySQL database. It comes out as a mysqlpp::sql_blob type.

It just so happens that this BLOB is a serialized Google Protobuf. I need to de-serialize it so that I can access it normally.

This gives a compile error, since ParseFromString() is not intended for mysqlpp:sql_blob types:

protobuf.ParseFromString( record.data );

However, if I force the cast, it compiles OK:

protobuf.ParseFromString( (std::string) record.data );

Is this safe? I'm particularly worried because of this snippet from the mysqlpp documentation:

"Because C++ strings handle binary data just fine, you might think you can use std::string instead of sql_blob, but the current design of String converts to std::string via a C string. As a result, the BLOB data is truncated at the first embedded null character during population of the SSQLS. There’s no way to fix that without completely redesigning either String or the SSQLS mechanism."

Thanks for your assistance!

+1  A: 

It doesn't look like it would be a problem judging by that quote (it's basically saying if a null character is found in the blob it will stop the string there, however ASCII strings won't have random nulls in the middle of them). However, this might present a problem for internalization (multibyte charsets may have nulls in the middle).

Chris T
Basically, it depends on how you serialized the protobuf. If you serialized it as a binary, watch out! You're setting yourself up for a problem down the road. But if you use the google::protobuf::TextFormat::PrintToString() and ParseFromString() functions, you'll be all right.
Runcible

related questions