Fix memory leak in MessageClass.encode

If the line above raises an exception, the upb_arena is lost and memory
is leaked.
pull/9150/head
Peter Zhu 3 years ago
parent 0707f2e7f5
commit 3a18299cc0
  1. 3
      ruby/ext/google/protobuf_c/message.c

@ -1012,7 +1012,6 @@ static VALUE Message_decode_json(int argc, VALUE* argv, VALUE klass) {
*/ */
static VALUE Message_encode(VALUE klass, VALUE msg_rb) { static VALUE Message_encode(VALUE klass, VALUE msg_rb) {
Message* msg = ruby_to_Message(msg_rb); Message* msg = ruby_to_Message(msg_rb);
upb_arena *arena = upb_arena_new();
const char *data; const char *data;
size_t size; size_t size;
@ -1020,6 +1019,8 @@ static VALUE Message_encode(VALUE klass, VALUE msg_rb) {
rb_raise(rb_eArgError, "Message of wrong type."); rb_raise(rb_eArgError, "Message of wrong type.");
} }
upb_arena *arena = upb_arena_new();
data = upb_encode(msg->msg, upb_msgdef_layout(msg->msgdef), arena, data = upb_encode(msg->msg, upb_msgdef_layout(msg->msgdef), arena,
&size); &size);

Loading…
Cancel
Save