Commit 90c8ded4 authored by Lawrence Chan's avatar Lawrence Chan Committed by Wouter van Oortmerssen

gRPC: fix memory leak (#4351)

SerializationTraits<T>::Deserialize _transfers_ ownership of the buffer,
so we must destroy it.

This commit also includes some misc fixes:
- Use grpc::Status::OK rather than default ctor for clarity.
- Check for a null buffer passed into Deserialize, and handle it the
  same way as the protobuf deserializer.
parent 8f864aad
...@@ -208,12 +208,15 @@ class SerializationTraits<flatbuffers::grpc::Message<T>> { ...@@ -208,12 +208,15 @@ class SerializationTraits<flatbuffers::grpc::Message<T>> {
// `grpc_byte_buffer`, incrementing the refcount in the process. // `grpc_byte_buffer`, incrementing the refcount in the process.
*buffer = grpc_raw_byte_buffer_create(slice, 1); *buffer = grpc_raw_byte_buffer_create(slice, 1);
*own_buffer = true; *own_buffer = true;
return grpc::Status(); return grpc::Status::OK;
} }
// Deserialize by pulling the // Deserialize by pulling the
static grpc::Status Deserialize(grpc_byte_buffer *buffer, static grpc::Status Deserialize(grpc_byte_buffer *buffer,
flatbuffers::grpc::Message<T> *msg) { flatbuffers::grpc::Message<T> *msg) {
if (!buffer) {
return ::grpc::Status(::grpc::StatusCode::INTERNAL, "No payload");
}
// Check if this is a single uncompressed slice. // Check if this is a single uncompressed slice.
if ((buffer->type == GRPC_BB_RAW) && if ((buffer->type == GRPC_BB_RAW) &&
(buffer->data.raw.compression == GRPC_COMPRESS_NONE) && (buffer->data.raw.compression == GRPC_COMPRESS_NONE) &&
...@@ -233,6 +236,7 @@ class SerializationTraits<flatbuffers::grpc::Message<T>> { ...@@ -233,6 +236,7 @@ class SerializationTraits<flatbuffers::grpc::Message<T>> {
// We wrap a `Message<T>` around the slice, but dont increment refcount // We wrap a `Message<T>` around the slice, but dont increment refcount
*msg = flatbuffers::grpc::Message<T>(slice, false); *msg = flatbuffers::grpc::Message<T>(slice, false);
} }
grpc_byte_buffer_destroy(buffer);
#if FLATBUFFERS_GRPC_DISABLE_AUTO_VERIFICATION #if FLATBUFFERS_GRPC_DISABLE_AUTO_VERIFICATION
return ::grpc::Status::OK; return ::grpc::Status::OK;
#else #else
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment