Skip to content

Commit

Permalink
Ruby: Only add RepeatedField instance to cache once fully initialized
Browse files Browse the repository at this point in the history
Fix: protocolbuffers#11968

Since calling a method on WeakMap allow the RubyVM to switch threads,
adding a non-fully initialized object into the cache allow another thread
from using it.

Note: with this fix, the other two thread may create their own wrapper
for the same underlying Array. We don't know if it's a big deal or not.

Co-Authored-By: Peter Zhu <peter@peterzhu.ca>
  • Loading branch information
byroot and peterzhu2118 committed Jun 14, 2023
1 parent 2cf94fa commit efc34c6
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion ruby/ext/google/protobuf_c/repeated_field.c
Original file line number Diff line number Diff line change
Expand Up @@ -87,14 +87,14 @@ VALUE RepeatedField_GetRubyWrapper(upb_Array* array, TypeInfo type_info,
if (val == Qnil) {
val = RepeatedField_alloc(cRepeatedField);
RepeatedField* self;
ObjectCache_Add(array, val);
TypedData_Get_Struct(val, RepeatedField, &RepeatedField_type, self);
self->array = array;
self->arena = arena;
self->type_info = type_info;
if (self->type_info.type == kUpb_CType_Message) {
self->type_class = Descriptor_DefToClass(type_info.def.msgdef);
}
ObjectCache_Add(array, val);
}

PBRUBY_ASSERT(ruby_to_RepeatedField(val)->type_info.type == type_info.type);
Expand Down

0 comments on commit efc34c6

Please sign in to comment.