This gist is a demonstration of an inconsistency between the google/protobuf Python and C++ implementations (in protobuf release version 2.5.0).
The inconsistency is in parsing of protobuf messages from strings. In C++, attempting to parse a message with one or more required fields fails if the input string is empty. In python, a message can always be parsed from the empty string, even if it has required fields.
Below you'll find a simple program. A Teacher
object and a Student
object are instantiated. The teacher administers a simple test to the student. They communicate via serialized protobuf messages. First, the teacher serializes a real MultipleChoiceQuestion
message and the student replies to it. Then, the teacher attempts to "trick" the student by having them attempt to decode a MultipleChoiceQuestion
from an empty string, instead of from a buffer containing a serialized message. The teacher checks whether the student "notices" that the message is empty (i.e. whether parsing fails) -- the student refuses to answer a question that can't be parsed.
In the C++ version of the program, the student can't parse a MultipleChoiceQuestion
message from the empty string, and therefore refuses to reply with a MultipleChoiceAnswer
(returning NULL
instead).
In the Python version of the program, when the student tries to parse a MultipleChoiceQuestion
message from the empty string, the parse succeeds. The student doesn't realize that parsing has failed (although they might realize if they called IsInitialized()
which would return False
). Therefore the student answers the teacher's "trick" question.