dvidelabs/flatcc

Unwanted result when verifying buffer

victorgrcp opened this issue · 3 comments

Hi, I'm working with two different types of flatbuffers and I want to verify during execution that the type of the buffer it's the correct to avoid errors or strange behaviours.

The schemas of the flatbuffers are the nexts:

  • Detection
namespace postprocessed;

struct Bbox {
  x_min:float;
  x_max:float;
  y_min:float;
  y_max:float;
}

struct DetectionAnn {
  bbox:Bbox;
  prob:float;
  category:float;
}

table Detection {
  annotations:[DetectionAnn];
}

root_type Detection;
  • Classification
namespace postprocessed;

struct ClassificationAnn {
  category:float;
  prob:float;
}

table Classification {
  annotations:[ClassificationAnn];
}

root_type Classification;

The problem is that when using a Detection flatbuffer and verifying it with Classification_verify_as_root(buffer, size) the return value is 0 ('ok') and my aim is that the result, in this case, is not correct as its not a valid Classification fb but a Detection one. It's any other (better) way to verify the buffers?

I use this code to create the Detection buffer,

flatcc_builder_t builder, *B;
B = &builder;
flatcc_builder_init(B);
ns(Detection_start_as_root)(B);
/// Add here all the data
ns(Detection_annotations_start)(B);
// Create all the annotations
for (int i = 0; i < num_ann; i++) {
     ns(Detection_annotations_push_create)(B, bbox1, bbox2, bbox3, bbox4, prob, cat);
}
ns(Detection_annotations_end)(B);
/// Finish adding data
ns(Detection_end_as_root)(B);
assert(B != NULL);

void *buffer;
size_t size;
buffer = flatcc_builder_finalize_aligned_buffer(B, &size);

And this to verify,

int verify = ns(Classification_verify_as_root_with_identifier(buffer, size, Classification_file_identifier)));
printf("Verify = %s (%i)\n", flatcc_verify_error_string(verify), verify);
assert(!verify);

Thank you!

I'm not sure if I understand your problem, but I assume the problem is that you have two different types of buffers but the verifier of one type also (unexpectedly) verifies a buffer of the other type where an error was expected.

If that is the case:

The schema has an optional file identifier. If you have not set this, or use the same for both tables, then there is no direct way to tell that the buffers are different. If a buffer of one type could legally be interpreted as a buffer of the other type, then verifier cannot, and should not complain.

FlatCC additionally has type identifier hashes that allows you to use a custom table specific identifier for the buffer. You can use this to gain more control over the file identifier than what is possible in the schema.
https://github.com/dvidelabs/flatcc#type-identifiers

If you don't like the type identifiers, you can also use your own custom identifiers when building the buffer.
Note that other languages might not be able handle such buffers but I suspect most will ignore the file identifier when reading the buffer normally.

It may be worth adding that if both tables are defined in the same schema file, they will have the same file identifier if it is present, or both will will have 0 identifier if absent.

Yes, you understood my problem, sorry if I didn't explain myself well.
I will try the approach of the type identifiers, thank you!