From 375d1f47cad2f678da49e3e3d0a5e6c79c5e0f30 Mon Sep 17 00:00:00 2001 From: "Nicolas \"Pixel\" Noble" Date: Wed, 30 Mar 2016 01:53:01 +0200 Subject: Better memory management for grpc_rb_byte_buffer_to_s. --- src/ruby/ext/grpc/rb_byte_buffer.c | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) (limited to 'src/ruby/ext') diff --git a/src/ruby/ext/grpc/rb_byte_buffer.c b/src/ruby/ext/grpc/rb_byte_buffer.c index 52523b8319..9b617e13d3 100644 --- a/src/ruby/ext/grpc/rb_byte_buffer.c +++ b/src/ruby/ext/grpc/rb_byte_buffer.c @@ -50,24 +50,18 @@ grpc_byte_buffer* grpc_rb_s_to_byte_buffer(char *string, size_t length) { } VALUE grpc_rb_byte_buffer_to_s(grpc_byte_buffer *buffer) { - size_t length = 0; - char *string = NULL; VALUE rb_string; - size_t offset = 0; grpc_byte_buffer_reader reader; gpr_slice next; if (buffer == NULL) { return Qnil; } - length = grpc_byte_buffer_length(buffer); - string = xmalloc(length + 1); + rb_string = rb_str_buf_new(grpc_byte_buffer_length(buffer)); grpc_byte_buffer_reader_init(&reader, buffer); while (grpc_byte_buffer_reader_next(&reader, &next) != 0) { - memcpy(string + offset, GPR_SLICE_START_PTR(next), GPR_SLICE_LENGTH(next)); - offset += GPR_SLICE_LENGTH(next); + rb_str_cat(rb_string, (const char *) GPR_SLICE_START_PTR(next), + GPR_SLICE_LENGTH(next)); gpr_slice_unref(next); } - rb_string = rb_str_new(string, length); - xfree(string); return rb_string; } -- cgit v1.2.3