2009-10-09 52 views
0

我从我的MySQL数据库中抓取了一些二进制数据。它是作为一个mysqlpp :: sql_blob类型出现的。将mysqlpp :: sql_blob转换为std :: string是否安全?

碰巧这BLOB是一个序列化的谷歌的Protobuf。我需要对它进行反序列化,以便我可以正常访问它。

这给出了一个编译错误,因为ParseFromString()不打算mysqlpp是:sql_blob类型:

protobuf.ParseFromString(record.data); 

但是,如果我强迫投,它编译OK:

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

是这安全吗?我特别担心,因为从mysqlpp文档这个片段的:

"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."

感谢您的帮助!

回答

1

它看起来并不像它会通过引用判断问题(它基本上是说,如果一个空字符的BLOB发现它会停止串在那里,但是ASCII字符串将不会在随机空他们中间)。但是,这可能会导致内部化问题(多字节字符集在中间可能有空值)。

+0

基本上,它取决于你如何序列化的protobuf的。如果您将它作为二进制序列化,请注意!你正在设法解决一个问题。但是如果你使用google :: protobuf :: TextFormat :: PrintToString()和ParseFromString()函数,你会没事的。 – Runcible 2009-10-09 18:09:41