Skip to content

Commit

Permalink
Check the message to be encoded is the wrong type. (protocolbuffers#4885
Browse files Browse the repository at this point in the history
)
  • Loading branch information
TeBoring committed Jul 20, 2018
1 parent fc90fd6 commit bbd5282
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
7 changes: 7 additions & 0 deletions ruby/ext/google/protobuf_c/encode_decode.c
Original file line number Diff line number Diff line change
Expand Up @@ -1106,6 +1106,13 @@ static void putmsg(VALUE msg_rb, const Descriptor* desc,

TypedData_Get_Struct(msg_rb, MessageHeader, &Message_type, msg);

if (desc != msg->descriptor) {
rb_raise(cTypeError,
"The type of given msg is '%s', expect '%s'.",
upb_msgdef_fullname(msg->descriptor->msgdef),
upb_msgdef_fullname(desc->msgdef));
}

for (upb_msg_field_begin(&i, desc->msgdef);
!upb_msg_field_done(&i);
upb_msg_field_next(&i)) {
Expand Down
10 changes: 10 additions & 0 deletions ruby/tests/encode_decode_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,14 @@ def test_encode_json

assert_match 'optional_int32', json
end

def test_encode_wrong_msg
e = assert_raise Google::Protobuf::TypeError do
m = A::B::C::TestMessage.new(
:optional_int32 => 1,
)
Google::Protobuf::Any.encode(m)
end
end

end

0 comments on commit bbd5282

Please sign in to comment.