2009-10-09 53 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