From 443165f88f9c99642e826e17292b3d101ccedf66 Mon Sep 17 00:00:00 2001 From: Alexander Polcyn Date: Mon, 22 Jan 2018 19:37:15 -0800 Subject: Fix two ruby memory leaks when exceptions are raised --- src/ruby/ext/grpc/rb_channel.c | 3 +-- src/ruby/ext/grpc/rb_compression_options.c | 16 +++++++++++++--- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/src/ruby/ext/grpc/rb_channel.c b/src/ruby/ext/grpc/rb_channel.c index 1d11a53aa7..7f021784e5 100644 --- a/src/ruby/ext/grpc/rb_channel.c +++ b/src/ruby/ext/grpc/rb_channel.c @@ -427,16 +427,15 @@ static VALUE grpc_rb_channel_create_call(VALUE self, VALUE parent, VALUE mask, parent_call = grpc_rb_get_wrapped_call(parent); } - cq = grpc_completion_queue_create_for_pluck(NULL); TypedData_Get_Struct(self, grpc_rb_channel, &grpc_channel_data_type, wrapper); if (wrapper->bg_wrapped == NULL) { rb_raise(rb_eRuntimeError, "closed!"); return Qnil; } + cq = grpc_completion_queue_create_for_pluck(NULL); method_slice = grpc_slice_from_copied_buffer(RSTRING_PTR(method), RSTRING_LEN(method)); - call = grpc_channel_create_call(wrapper->bg_wrapped->channel, parent_call, flags, cq, method_slice, host_slice_ptr, grpc_rb_time_timeval(deadline, diff --git a/src/ruby/ext/grpc/rb_compression_options.c b/src/ruby/ext/grpc/rb_compression_options.c index e24f20d2f9..7fdec2ee8b 100644 --- a/src/ruby/ext/grpc/rb_compression_options.c +++ b/src/ruby/ext/grpc/rb_compression_options.c @@ -27,6 +27,8 @@ #include #include #include +#include +#include #include #include "rb_grpc.h" @@ -159,7 +161,6 @@ void grpc_rb_compression_options_algorithm_name_to_value_internal( grpc_compression_algorithm* algorithm_value, VALUE algorithm_name) { grpc_slice name_slice; VALUE algorithm_name_as_string = Qnil; - char* tmp_str = NULL; Check_Type(algorithm_name, T_SYMBOL); @@ -175,8 +176,17 @@ void grpc_rb_compression_options_algorithm_name_to_value_internal( * the algorithm parse function * in GRPC core. */ if (!grpc_compression_algorithm_parse(name_slice, algorithm_value)) { - tmp_str = grpc_slice_to_c_string(name_slice); - rb_raise(rb_eNameError, "Invalid compression algorithm name: %s", tmp_str); + char* name_slice_str = grpc_slice_to_c_string(name_slice); + char* error_message_str = NULL; + VALUE error_message_ruby_str = Qnil; + GPR_ASSERT(gpr_asprintf(&error_message_str, + "Invalid compression algorithm name: %s", + name_slice_str) != -1); + gpr_free(name_slice_str); + error_message_ruby_str = + rb_str_new(error_message_str, strlen(error_message_str)); + gpr_free(error_message_str); + rb_raise(rb_eNameError, StringValueCStr(error_message_ruby_str)); } grpc_slice_unref(name_slice); -- cgit v1.2.3