google / flatbuffers

FlatBuffers: Memory Efficient Serialization Library

Home Page:https://flatbuffers.dev/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[Swift] Verifier does not check file identifier.

kennycarruthers opened this issue · comments

I'm not sure what the current state is for Verification support within Swift, but the current implementation does not appear to take the file_identifier into account when verifying a buffer. However, the C++ generated code does.

Schema:

table Entity { 
  x:int;
}

file_identifier "ENTT";
root_type Entity;

C++ Code:

Note the top-level functions that are generated which consider the identifier when performing validation.

inline const char *EntityIdentifier() {
  return "ENTT";
}

inline bool EntityBufferHasIdentifier(const void *buf) {
  return flatbuffers::BufferHasIdentifier(buf, EntityIdentifier());
}

inline bool SizePrefixedEntityBufferHasIdentifier(const void *buf) {
  return flatbuffers::BufferHasIdentifier(buf, EntityIdentifier(), true);
}

inline bool VerifyEntityBuffer(flatbuffers::Verifier &verifier) {
  return verifier.VerifyBuffer<Entity>(EntityIdentifier());
}

inline bool VerifySizePrefixedEntityBuffer(flatbuffers::Verifier &verifier) {
  return verifier.VerifySizePrefixedBuffer<Entity>(EntityIdentifier());
}

Swift Code:

Note the sole verify function that is generated that does not consider the identifier when performing validation.

public struct Entity: FlatBufferObject, Verifiable, ObjectAPIPacker {

  // ... snip ...

  public static func verify<T>(_ verifier: inout Verifier, at position: Int, of type: T.Type) throws where T: Verifiable {
    var _v = try verifier.visitTable(at: position)
    try _v.visit(field: VTOFFSET.x.p, fieldName: "x", required: false, type: Int32.self)
    _v.finish()
  }
}

Being able to validate based on the `file_identifier` is quite helpful when storing FlatBuffers as discrete files or as blobs within a database. 

Hey, thanks for opening the issue.

Yeah we can definitely include this, however i am not sure when i would be able to implement this due to work.

Yeah we can definitely include this, however i am not sure when i would be able to implement this due to work.

Oh yeah, no worries. Just figured I'd document this in case other Swift users encountered it. Thanks for the existing work on this package, much appreciated.