问题
Is there any way to verify bytearray with flatbuffer structure in it with flatbuffer verifier if tables in schema for that two object starts from similar data types?
Example schema:
table AddTaskResponse{
blablabla:int;
foobar:int;
}
table AddTaskRequest{
requestId:int;
taskId:int;
profileId:string;
}
My current experiments shows me:
flatbuffers::Verifier verifier(reinterpret_cast<unsigned char*>(data.data()),data.size());
bool isaddTaskResponse = VerifyAddTaskResponseBuffer(verifier);
bool isaddTaskRequest = VerifyAddTaskRequestBuffer(verifier);
Both bools flags isaddTaskResponse and isaddTaskRequest are true and their true state not depends from the actual structure i send AddTaskResponse
or AddTaskRequest
.
回答1:
The verifier just checks that the data is structurally sound (no offsets go out of bounds), but there's no data about types in the buffer, so yes, if types are compatible, it will work.
In this case, if the response verifier sees a request buffer, it thinks it is a buffer from a future version of the schema (it has an extra field, which gets ignored). Similarly, if a request verifier sees a response buffer, it thinks it got an older version of the schema, since a field is missing.
Though this works, it is not recommended, as it complicates schema evolution, and you're making some assumptions about implementation. You should always know the exact type of a buffer, either through external context, or internally (using e.g. a union type).
来源:https://stackoverflow.com/questions/37486992/flatbuffers-verifier-behaviour