Skip to content

Commit b2ac7ce

Browse files
peterzhu2118Adam Cozzette
authored andcommitted
Fix memory leak in MessageClass.encode
If the line above raises an exception, the upb_arena is lost and memory is leaked.
1 parent e58469b commit b2ac7ce

File tree

1 file changed

+2
-1
lines changed

1 file changed

+2
-1
lines changed

ruby/ext/google/protobuf_c/message.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1012,14 +1012,15 @@ static VALUE Message_decode_json(int argc, VALUE* argv, VALUE klass) {
10121012
*/
10131013
static VALUE Message_encode(VALUE klass, VALUE msg_rb) {
10141014
Message* msg = ruby_to_Message(msg_rb);
1015-
upb_arena *arena = upb_arena_new();
10161015
const char *data;
10171016
size_t size;
10181017

10191018
if (CLASS_OF(msg_rb) != klass) {
10201019
rb_raise(rb_eArgError, "Message of wrong type.");
10211020
}
10221021

1022+
upb_arena *arena = upb_arena_new();
1023+
10231024
data = upb_encode(msg->msg, upb_msgdef_layout(msg->msgdef), arena,
10241025
&size);
10251026

0 commit comments

Comments
 (0)