Skip to content

Commit

Permalink
Fix memory leak in MessageClass.encode
Browse files Browse the repository at this point in the history
If the line above raises an exception, the upb_arena is lost and memory
is leaked.
  • Loading branch information
peterzhu2118 committed Oct 25, 2021
1 parent 0707f2e commit 493e0e3
Showing 1 changed file with 2 additions and 1 deletion.
3 changes: 2 additions & 1 deletion ruby/ext/google/protobuf_c/message.c
Expand Up @@ -1012,14 +1012,15 @@ static VALUE Message_decode_json(int argc, VALUE* argv, VALUE klass) {
*/
static VALUE Message_encode(VALUE klass, VALUE msg_rb) {
Message* msg = ruby_to_Message(msg_rb);
upb_arena *arena = upb_arena_new();
const char *data;
size_t size;

if (CLASS_OF(msg_rb) != klass) {
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,
&size);

Expand Down

0 comments on commit 493e0e3

Please sign in to comment.