Fix memory leak in MessageClass.encode

If the line above raises an exception, the upb_arena is lost and memory
is leaked.
This commit is contained in:
Peter Zhu 2021-10-25 15:41:19 -04:00 committed by Adam Cozzette
parent e58469bdbe
commit b2ac7ced50

View File

@ -1012,7 +1012,6 @@ 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;
@ -1020,6 +1019,8 @@ static VALUE Message_encode(VALUE klass, VALUE msg_rb) {
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);