From 90c8ded4499a935b241a29c0050a2bd9bf2de58e Mon Sep 17 00:00:00 2001 From: Lawrence Chan Date: Tue, 13 Jun 2017 10:50:27 -0500 Subject: [PATCH] gRPC: fix memory leak (#4351) SerializationTraits::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. --- include/flatbuffers/grpc.h | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/include/flatbuffers/grpc.h b/include/flatbuffers/grpc.h index 809f1032f..a5730f661 100644 --- a/include/flatbuffers/grpc.h +++ b/include/flatbuffers/grpc.h @@ -208,12 +208,15 @@ class SerializationTraits> { // `grpc_byte_buffer`, incrementing the refcount in the process. *buffer = grpc_raw_byte_buffer_create(slice, 1); *own_buffer = true; - return grpc::Status(); + return grpc::Status::OK; } // Deserialize by pulling the static grpc::Status Deserialize(grpc_byte_buffer *buffer, flatbuffers::grpc::Message *msg) { + if (!buffer) { + return ::grpc::Status(::grpc::StatusCode::INTERNAL, "No payload"); + } // Check if this is a single uncompressed slice. if ((buffer->type == GRPC_BB_RAW) && (buffer->data.raw.compression == GRPC_COMPRESS_NONE) && @@ -233,6 +236,7 @@ class SerializationTraits> { // We wrap a `Message` around the slice, but dont increment refcount *msg = flatbuffers::grpc::Message(slice, false); } + grpc_byte_buffer_destroy(buffer); #if FLATBUFFERS_GRPC_DISABLE_AUTO_VERIFICATION return ::grpc::Status::OK; #else