Check the message to be encoded is the wrong type. (#4885)

pull/4248/merge
Paul Yang 6 years ago committed by GitHub
parent 0a59054c30
commit 40f9ef2f56
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 7
      ruby/ext/google/protobuf_c/encode_decode.c
  2. 10
      ruby/tests/encode_decode_test.rb

@ -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)) {

@ -84,4 +84,14 @@ class EncodeDecodeTest < Test::Unit::TestCase
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

Loading…
Cancel
Save