Skip to content

Commit

Permalink
Check the message to be encoded is the wrong type. (#4885)
Browse files Browse the repository at this point in the history
  • Loading branch information
TeBoring committed Jul 12, 2018
1 parent 0a59054 commit 40f9ef2
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
Expand Up @@ -1108,6 +1108,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
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 40f9ef2

Please sign in to comment.