Here's a comment from the code(google/protobuf/io/coded_stream.h
)爲那些想知道他們正在談論的安全原因的人設置消息限制。就我而言,我無法修改我的應用程序的工作方式,因此我必須更改此限制。
這個帖子已經很老了,但最近深入的學習已經得到了關注,圖書館咖啡館使用了Protobuf,所以也許更多的人會偶然發現這一點。我必須和Caffe一起做神經網絡的東西,整個網絡甚至在批量最小的情況下也花費了很多內存。
// Total Bytes Limit -----------------------------------------------
// To prevent malicious users from sending excessively large messages
// and causing integer overflows or memory exhaustion, CodedInputStream
// imposes a hard limit on the total number of bytes it will read.
// Sets the maximum number of bytes that this CodedInputStream will read
// before refusing to continue. To prevent integer overflows in the
// protocol buffers implementation, as well as to prevent servers from
// allocating enormous amounts of memory to hold parsed messages, the
// maximum message length should be limited to the shortest length that
// will not harm usability. The theoretical shortest message that could
// cause integer overflows is 512MB. The default limit is 64MB. Apps
// should set shorter limits if possible. If warning_threshold is not -1,
// a warning will be printed to stderr after warning_threshold bytes are
// read. For backwards compatibility all negative values get squashed to -1,
// as other negative values might have special internal meanings.
// An error will always be printed to stderr if the limit is reached.
//
// This is unrelated to PushLimit()/PopLimit().
//
// Hint: If you are reading this because your program is printing a
// warning about dangerously large protocol messages, you may be
// confused about what to do next. The best option is to change your
// design such that excessively large messages are not necessary.
// For example, try to design file formats to consist of many small
// messages rather than a single large one. If this is infeasible,
// you will need to increase the limit. Chances are, though, that
// your code never constructs a CodedInputStream on which the limit
// can be set. You probably parse messages by calling things like
// Message::ParseFromString(). In this case, you will need to change
// your code to instead construct some sort of ZeroCopyInputStream
// (e.g. an ArrayInputStream), construct a CodedInputStream around
// that, then call Message::ParseFromCodedStream() instead. Then
// you can adjust the limit. Yes, it's more work, but you're doing
// something unusual.
而不是建議管理你自己的protobuf代碼分支(唉),可以手動構建CodedInputStream並使用它。重新設計應用程序很可能是最佳選擇,但確實沒有理由維護自己的分支。 – Voo
快速和骯髒的方法爲我工作。謝謝。 –