mirror of
https://github.com/google/flatbuffers.git
synced 2026-11-07 20:59:10 +00:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
3f4f0497ea | ||
|
|
6dda43bbaa | ||
|
|
9a8a511b52 | ||
|
|
b8431fbcd7 | ||
|
|
5761d6e67a |
@@ -271,7 +271,7 @@ namespace flatbuffers {
|
||||
#if (defined(_MSC_VER) && _MSC_VER >= 1800) || \
|
||||
(defined(__ANDROID__) && defined(__ANDROID_API__) && __ANDROID_API__>= 26) || \
|
||||
(defined(_XOPEN_VERSION) && (_XOPEN_VERSION >= 700)) && \
|
||||
!defined(__Fuchsia__)
|
||||
!defined(__Fuchsia__) && (!defined(__linux__) || defined(__GLIBC__))
|
||||
#define FLATBUFFERS_LOCALE_INDEPENDENT 1
|
||||
#else
|
||||
#define FLATBUFFERS_LOCALE_INDEPENDENT 0
|
||||
|
||||
@@ -24,15 +24,14 @@ class handle:
|
||||
struct offset:
|
||||
o:int
|
||||
|
||||
enum sizeof:
|
||||
sz_8 = 1
|
||||
sz_16 = 2
|
||||
sz_32 = 4
|
||||
sz_64 = 8
|
||||
sz_voffset = 2
|
||||
sz_uoffset = 4
|
||||
sz_soffset = 4
|
||||
sz_metadata_fields = 2
|
||||
let sz_8 = 1
|
||||
let sz_16 = 2
|
||||
let sz_32 = 4
|
||||
let sz_64 = 8
|
||||
let sz_voffset = 2
|
||||
let sz_uoffset = 4
|
||||
let sz_soffset = 4
|
||||
let sz_metadata_fields = 2
|
||||
|
||||
class builder:
|
||||
buf = "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
|
||||
|
||||
+1
-1
@@ -404,7 +404,7 @@ class CppGenerator : public BaseGenerator {
|
||||
code_ += "struct {{STRUCT_NAME}}BinarySchema {";
|
||||
code_ += " static const uint8_t *data() {";
|
||||
code_ += " // Buffer containing the binary schema.";
|
||||
code_ += " static const uint8_t bfbsData[" +
|
||||
code_ += " alignas(uint64_t) static const uint8_t bfbsData[" +
|
||||
NumToString(parser_.builder_.GetSize()) + "] = {";
|
||||
code_ += binary_schema_hex_text;
|
||||
code_ += " };";
|
||||
|
||||
@@ -16,6 +16,30 @@
|
||||
|
||||
import Foundation
|
||||
|
||||
@inline(__always)
|
||||
private func verifySizePrefix(
|
||||
byteBuffer: inout ByteBuffer,
|
||||
requireExactSize: Bool,
|
||||
options: VerifierOptions) throws
|
||||
{
|
||||
let verifier = try Verifier(buffer: &byteBuffer, options: options)
|
||||
let prefixPosition = byteBuffer.reader
|
||||
let prefix: UOffset = try verifier.getValue(at: prefixPosition)
|
||||
let availableSize = byteBuffer.size &- UOffset(MemoryLayout<UOffset>.size)
|
||||
|
||||
if requireExactSize {
|
||||
guard prefix == availableSize else {
|
||||
throw FlatbuffersErrors.prefixedSizeNotEqualToBufferSize
|
||||
}
|
||||
} else if prefix > availableSize {
|
||||
throw FlatbuffersErrors.outOfBounds(
|
||||
position: UInt(prefixPosition)
|
||||
&+ UInt(MemoryLayout<UOffset>.size)
|
||||
&+ UInt(prefix),
|
||||
end: byteBuffer.capacity)
|
||||
}
|
||||
}
|
||||
|
||||
/// Takes in a prefixed sized buffer, where the prefixed size would be skipped.
|
||||
/// And would verify that the buffer passed is a valid `Flatbuffers` Object.
|
||||
/// - Parameters:
|
||||
@@ -31,6 +55,10 @@ public func getPrefixedSizeCheckedRoot<T: FlatBufferTable & Verifiable>(
|
||||
fileId: String? = nil,
|
||||
options: VerifierOptions = .init()) throws -> T
|
||||
{
|
||||
try verifySizePrefix(
|
||||
byteBuffer: &byteBuffer,
|
||||
requireExactSize: false,
|
||||
options: options)
|
||||
byteBuffer.skipPrefix()
|
||||
return try getCheckedRoot(
|
||||
byteBuffer: &byteBuffer,
|
||||
@@ -53,10 +81,11 @@ public func getCheckedPrefixedSizeRoot<T: FlatBufferTable & Verifiable>(
|
||||
fileId: String? = nil,
|
||||
options: VerifierOptions = .init()) throws -> T
|
||||
{
|
||||
let prefix = byteBuffer.skipPrefix()
|
||||
if prefix != byteBuffer.size {
|
||||
throw FlatbuffersErrors.prefixedSizeNotEqualToBufferSize
|
||||
}
|
||||
try verifySizePrefix(
|
||||
byteBuffer: &byteBuffer,
|
||||
requireExactSize: true,
|
||||
options: options)
|
||||
byteBuffer.skipPrefix()
|
||||
return try getCheckedRoot(
|
||||
byteBuffer: &byteBuffer,
|
||||
fileId: fileId,
|
||||
@@ -95,14 +124,15 @@ public func getCheckedRoot<T: FlatBufferTable & Verifiable>(
|
||||
options: VerifierOptions = .init()) throws -> T
|
||||
{
|
||||
var verifier = try Verifier(buffer: &byteBuffer, options: options)
|
||||
let rootPosition = byteBuffer.reader
|
||||
if let fileId = fileId {
|
||||
try verifier.verify(id: fileId)
|
||||
try verifier.verify(id: fileId, at: rootPosition)
|
||||
}
|
||||
try ForwardOffset<T>.verify(&verifier, at: 0, of: T.self)
|
||||
try ForwardOffset<T>.verify(&verifier, at: rootPosition, of: T.self)
|
||||
return T.init(
|
||||
byteBuffer,
|
||||
o: Int32(byteBuffer.read(def: UOffset.self, position: byteBuffer.reader))
|
||||
&+ Int32(byteBuffer.reader))
|
||||
o: Int32(byteBuffer.read(def: UOffset.self, position: rootPosition))
|
||||
&+ Int32(rootPosition))
|
||||
}
|
||||
|
||||
/// Returns a `NON-Checked` flatbuffers object
|
||||
|
||||
@@ -215,12 +215,16 @@ public struct Verifier {
|
||||
}
|
||||
|
||||
@inline(__always)
|
||||
func verify(id: String) throws {
|
||||
func verify(id: String, at position: Int) throws {
|
||||
let size = MemoryLayout<Int32>.size
|
||||
guard storage.capacity >= (size &* 2) else {
|
||||
guard
|
||||
position >= 0,
|
||||
position <= storage.capacity,
|
||||
storage.capacity - position >= size &* 2
|
||||
else {
|
||||
throw FlatbuffersErrors.bufferDoesntContainID
|
||||
}
|
||||
let str = _buffer.readString(at: size, count: size)
|
||||
let str = _buffer.readString(at: position &+ size, count: size)
|
||||
if id == str {
|
||||
return
|
||||
}
|
||||
|
||||
@@ -12,7 +12,7 @@ namespace Example {
|
||||
struct MonsterBinarySchema {
|
||||
static const uint8_t *data() {
|
||||
// Buffer containing the binary schema.
|
||||
static const uint8_t bfbsData[14520] = {
|
||||
alignas(uint64_t) static const uint8_t bfbsData[14520] = {
|
||||
0x20,0x00,0x00,0x00,0x42,0x46,0x42,0x53,0x00,0x00,0x00,0x00,0x14,0x00,0x20,0x00,0x04,0x00,0x08,0x00,
|
||||
0x0C,0x00,0x10,0x00,0x14,0x00,0x18,0x00,0x00,0x00,0x1C,0x00,0x14,0x00,0x00,0x00,0x58,0x00,0x00,0x00,
|
||||
0x34,0x00,0x00,0x00,0x24,0x00,0x00,0x00,0x18,0x00,0x00,0x00,0x70,0x0D,0x00,0x00,0x08,0x00,0x00,0x00,
|
||||
|
||||
@@ -373,6 +373,58 @@ final class FlatbuffersVerifierTests {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
func testSizePrefixedVerifierUsesPostPrefixRoot() throws {
|
||||
// The size prefix points to a valid empty decoy table at byte 44. The real
|
||||
// root at byte 4 points to a truncated UInt64 vector that must be rejected.
|
||||
let bytes: [UInt8] = [
|
||||
44, 0, 0, 0,
|
||||
16, 0, 0, 0,
|
||||
6, 0, 8, 0, 4, 0,
|
||||
0, 0, 0, 0, 0, 0,
|
||||
12, 0, 0, 0,
|
||||
8, 0, 0, 0, 0, 0, 0, 0,
|
||||
2, 0, 0, 0, 65, 66,
|
||||
0, 0,
|
||||
4, 0, 4, 0,
|
||||
4, 0, 0, 0,
|
||||
]
|
||||
|
||||
var strictBuffer = ByteBuffer(bytes: bytes)
|
||||
#expect(throws: FlatbuffersErrors.self) {
|
||||
try getCheckedPrefixedSizeRoot(
|
||||
byteBuffer: &strictBuffer) as Swift_Tests_Vectors
|
||||
}
|
||||
|
||||
var prefixedBuffer = ByteBuffer(bytes: bytes)
|
||||
#expect(throws: FlatbuffersErrors.self) {
|
||||
try getPrefixedSizeCheckedRoot(
|
||||
byteBuffer: &prefixedBuffer) as Swift_Tests_Vectors
|
||||
}
|
||||
|
||||
var builder = FlatBufferBuilder()
|
||||
let movie = Movie.createMovie(&builder)
|
||||
Movie.finish(&builder, end: movie, prefix: true)
|
||||
var validBuffer = builder.sizedBuffer
|
||||
let _: Movie = try getCheckedPrefixedSizeRoot(
|
||||
byteBuffer: &validBuffer,
|
||||
fileId: Movie.id)
|
||||
|
||||
var shortBuffer = ByteBuffer(bytes: [0, 0, 0, 0])
|
||||
#expect(throws: FlatbuffersErrors.bufferDoesntContainID) {
|
||||
try getCheckedRoot(
|
||||
byteBuffer: &shortBuffer,
|
||||
fileId: Movie.id) as Movie
|
||||
}
|
||||
|
||||
var oversizedPrefix = ByteBuffer(bytes: [5, 0, 0, 0, 0, 0, 0, 0])
|
||||
#expect(throws: FlatbuffersErrors.outOfBounds(position: 9, end: 8)) {
|
||||
try getPrefixedSizeCheckedRoot(
|
||||
byteBuffer: &oversizedPrefix) as Movie
|
||||
}
|
||||
#expect(oversizedPrefix.reader == 0)
|
||||
}
|
||||
|
||||
@Test
|
||||
func testFullVerifier() throws {
|
||||
_ =
|
||||
|
||||
Reference in New Issue
Block a user