flatbuffers icon indicating copy to clipboard operation
flatbuffers copied to clipboard

[C++] Missing verifier method for nested structs

Open kendegemaro opened this issue 2 years ago • 1 comments

Hello,

I am currently working with flatbuffer reflections and implemented processing of nested structs. I'd like to verify nested structs with the flatbuffers::Verifier::VerifyFieldStruct() method, because I've got nothing else to use. I've got the following example schema:

struct secondStruct {
  dummyInt:Int;
}

struct firstStruct {
  dummyInt:Int;
  myNestedStruct:secondStruct;
}

table rootTable {
  myStruct:firstStruct;
}

As of my understanding, nested structs aren't represented in the vTable and instead are referenced in flatbuffers reflection by the Field-->Offset value pointing inside the parent struct. This makes it impossible to simply use the flatbuffers::Verifier:VerifyFieldStruct() method on myNestedStruct without workarounds, because my required function parameter elem_off doesn't exist in the vTable.

To be able to still use VerifyFieldStruct() on nested structs, my idea is to calculate the required elem_off value manually by substracting the pointer of the table root pointer off the pointer of the nested struct, and feeding that into the method:

flatbuffers::voffset_t elem_off = static_cast<flatbuffers::voffset_t>((uint8_t*)myNestedStructPointer - (uint8_t*)myTableRootPointer);
if (!myVerifier->VerifyFieldStruct(..., elem_off, ..., ...))
{
  // Bang
}

Is this way of usage logically correct? Does a better solution to this problem exist which I have not seen yet? Thanks for your answers!

kendegemaro avatar Dec 12 '23 15:12 kendegemaro

The verifier is to check that all offsets point to within the buffer. A nested struct is the same as a single struct with the nested part inlined. So I don't think we need to verify a nested struct anymore than what we currently have. Am I missing something?

dbaileychess avatar Dec 20 '23 23:12 dbaileychess

Hello Derek, thank you for your answer and sorry for taking some time with mine. I have examined the related verifier methods again and now I understand the meaning behind the code. We can close this issue :)

kendegemaro avatar Jan 15 '24 13:01 kendegemaro