diff options
author | 2018-06-07 16:49:27 -0700 | |
---|---|---|
committer | 2018-06-07 16:51:41 -0700 | |
commit | 2bf2799ee80791107d4fe587ff9b6c7cf6c8b418 (patch) | |
tree | 697975f1ed6ee164164d8e37f536296ca190408d /tensorflow/c/c_api.cc | |
parent | 82f152ee75261afa3ae59ae7c9e18493d7e8b55e (diff) |
C API: Fail gracefully if the serialized graph would be too large.
See #19657 for some motivation.
Without this explicit check, a large graph would trigger an assertion failure
in the protobuf codebase
(https://github.com/google/protobuf/blob/0456e269ee6505766474aa8d7b8bba7ac047f457/src/google/protobuf/message_lite.cc#L68)
Pull Request for google/protobuf: https://github.com/google/protobuf/pull/4739
PiperOrigin-RevId: 199719082
Diffstat (limited to 'tensorflow/c/c_api.cc')
-rw-r--r-- | tensorflow/c/c_api.cc | 17 |
1 files changed, 16 insertions, 1 deletions
diff --git a/tensorflow/c/c_api.cc b/tensorflow/c/c_api.cc index b86b277ac3..cb0b093ad2 100644 --- a/tensorflow/c/c_api.cc +++ b/tensorflow/c/c_api.cc @@ -631,7 +631,22 @@ Status MessageToBuffer(const tensorflow::protobuf::Message& in, "Failed to allocate memory to serialize message of type '", in.GetTypeName(), "' and size ", proto_size); } - in.SerializeToArray(buf, proto_size); + // SerializeToArray takes size as an int. + // This next 'if' is a workaround till we update to depend on a version + // of protocol buffers that includes + // https://github.com/google/protobuf/pull/4739 + if (proto_size > std::numeric_limits<int>::max()) { + return InvalidArgument("Cannot serialize protocol buffer of type ", + in.GetTypeName(), " as the serialized size (", + proto_size, + "bytes) would be larger than the limit (", + std::numeric_limits<int>::max(), " bytes)"); + } + if (!in.SerializeToArray(buf, proto_size)) { + return InvalidArgument("Unable to serialize ", in.GetTypeName(), + " protocol buffer, perhaps the serialized size (", + proto_size, " bytes) is too large?"); + } out->data = buf; out->length = proto_size; out->data_deallocator = [](void* data, size_t length) { |