Re: [protobuf] How to prevent different protocol buffers from being parseFrom(byte[])
> I am confused on how to detect this scenario, and ultimately prevent > such things occurring. You can't, at least in the simple way. Protocol buffers on the wire have no type information associated with them, and they're explicitly designed so that they can accept fields they don't expect to be present (for forwards compatibility), so serializing a message of one type and deserializing it as another type will work sometimes. A couple potential solutions come to mind. One is to send all your messages wrapped in another message that includes a type identifier. Another would be to have all your messages include a key field that must be filled in in a specific way (eg, tag 1 in all messages is a int field named tag, and there's a single value for each message type that you require to be filled in). What is most appropriate depends on how you expect a message of one type to be sent to something expecting a different type. (Obviously, the best solution is to set up your well-typed language APIs to keep that from happening.) - Adam -- You received this message because you are subscribed to the Google Groups "Protocol Buffers" group. To post to this group, send email to proto...@googlegroups.com. To unsubscribe from this group, send email to protobuf+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/protobuf?hl=en.
Re: [protobuf] How to prevent different protocol buffers from being parseFrom(byte[])
flyte wrote: Now, if I pass in a byte[] of a protocol buffer that is of a type that I do not expect, the parseFrom() still returns correctly, (I would have though the IOException would have been thrown or something), and some of the fields get merged (the fields with the same IDs?) Due to the way the protocol buffer wire format is defined, this is how it works. Fields with the same ids will be treated as the same, and unknown fields will be parsed as well. This is part of what permits them to be "extensible", and work with different software versions. I am confused on how to detect this scenario, and ultimately prevent such things occurring. You will need to redesign your application so you don't try to parse MessageA as a MessageB. For example, union types may solve your problem if you have some set of acceptable types that your application must handle: http://code.google.com/apis/protocolbuffers/docs/techniques.html#union Alternatively, you may wish to add a "header" describing the type, before the type itself. Hope this helps, Evan -- Evan Jones http://evanjones.ca/ -- You received this message because you are subscribed to the Google Groups "Protocol Buffers" group. To post to this group, send email to proto...@googlegroups.com. To unsubscribe from this group, send email to protobuf+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/protobuf?hl=en.
[protobuf] How to prevent different protocol buffers from being parseFrom(byte[])
Hi All. I am trying to detect when I receive a protocol buffer that is not of the expected type. For example. void myMethod(byte[] theData) { TheMsgTypeExpected theMsg = TheMsgTypeExpected.parseFrom(data); } Now, if I pass in a byte[] of a protocol buffer that is of a type that I do not expect, the parseFrom() still returns correctly, (I would have though the IOException would have been thrown or something), and some of the fields get merged (the fields with the same IDs?) I am confused on how to detect this scenario, and ultimately prevent such things occurring. -- You received this message because you are subscribed to the Google Groups "Protocol Buffers" group. To post to this group, send email to proto...@googlegroups.com. To unsubscribe from this group, send email to protobuf+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/protobuf?hl=en.