Skip to content

Commit

Permalink
Fixed crash bug and moved initialization into init method.
Browse files Browse the repository at this point in the history
  • Loading branch information
haberman committed Aug 28, 2019
1 parent 3e3407a commit 671c245
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 14 deletions.
21 changes: 9 additions & 12 deletions ruby/ext/google/protobuf_c/message.c
Expand Up @@ -62,26 +62,19 @@ VALUE Message_alloc(VALUE klass) {
Descriptor* desc = ruby_to_Descriptor(descriptor);
MessageHeader* msg;
VALUE ret;
size_t size;

if (desc->layout == NULL) {
create_layout(desc);
}

msg = (MessageHeader*)ALLOC_N(uint8_t,
sizeof(MessageHeader) + desc->layout->size);

// Required in case a GC happens before layout_init().
memset(msg, 0, desc->layout->size);

// We wrap first so that everything in the message object is GC-rooted in case
// a collection happens during object creation in layout_init().
ret = TypedData_Wrap_Struct(klass, &Message_type, msg);
msg = ALLOC_N(uint8_t, sizeof(MessageHeader) + desc->layout->size);
msg->descriptor = desc;
rb_ivar_set(ret, descriptor_instancevar_interned, descriptor);

msg->unknown_fields = NULL;
memcpy(Message_data(msg), desc->layout->empty_template, desc->layout->size);

layout_init(desc->layout, Message_data(msg));
ret = TypedData_Wrap_Struct(klass, &Message_type, msg);
rb_ivar_set(ret, descriptor_instancevar_interned, descriptor);

return ret;
}
Expand Down Expand Up @@ -473,7 +466,11 @@ int Message_initialize_kwarg(VALUE key, VALUE val, VALUE _self) {
* Message class are provided on each concrete message class.
*/
VALUE Message_initialize(int argc, VALUE* argv, VALUE _self) {
MessageHeader* self;
VALUE hash_args;
TypedData_Get_Struct(_self, MessageHeader, &Message_type, self);

layout_init(self->descriptor->layout, Message_data(self));

if (argc == 0) {
return Qnil;
Expand Down
2 changes: 0 additions & 2 deletions ruby/ext/google/protobuf_c/storage.c
Expand Up @@ -947,8 +947,6 @@ void layout_init(MessageLayout* layout, void* storage) {
VALUE* value = (VALUE*)CHARPTR_AT(storage, layout->value_offset);
int i;

memcpy(storage, layout->empty_template, layout->size);

for (i = 0; i < layout->repeated_count; i++, value++) {
*value = RepeatedField_new_this_type(*value);
}
Expand Down

0 comments on commit 671c245

Please sign in to comment.