diff options
author | Asim Shankar <asimshankar@gmail.com> | 2018-06-07 09:24:42 -0700 |
---|---|---|
committer | Asim Shankar <asimshankar@gmail.com> | 2018-06-07 09:24:42 -0700 |
commit | 2020e3d5eed3029287afb6002266131d87f881e8 (patch) | |
tree | db25a99aa69868313075ce450eb761b8c5ef181f | |
parent | 0456e269ee6505766474aa8d7b8bba7ac047f457 (diff) |
Graceful failure in SerializeToArray().
See https://github.com/tensorflow/tensorflow/issues/19657 for
motivation. But long story short, without this change:
m.SerializeToArray(buffer, m.ByteSizeLong());
would result in a CHECK failure if m.ByteSizeLong() returned a value
>2GB.
-rw-r--r-- | src/google/protobuf/message_lite.cc | 6 |
1 files changed, 5 insertions, 1 deletions
diff --git a/src/google/protobuf/message_lite.cc b/src/google/protobuf/message_lite.cc index 123b142d..65af7cea 100644 --- a/src/google/protobuf/message_lite.cc +++ b/src/google/protobuf/message_lite.cc @@ -316,7 +316,11 @@ bool MessageLite::SerializeToArray(void* data, int size) const { } bool MessageLite::SerializePartialToArray(void* data, int size) const { - int byte_size = ByteSizeLong(); + size_t byte_size = ByteSizeLong(); + if (byte_size > INT_MAX) { + GOOGLE_LOG(ERROR) << "Exceeded maximum protobuf size of 2GB: " << size; + return false; + } if (size < byte_size) return false; uint8* start = reinterpret_cast<uint8*>(data); uint8* end = SerializeWithCachedSizesToArray(start); |