Flatbuffers verifier behaviour

风格不统一 提交于 2021-02-10 06:56:14

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!