mirror of
https://github.com/google/flatbuffers.git
synced 2026-06-29 03:00:04 +00:00
(fix): #8408 fixes a bug where the capacity of the buffer isnt verified before trying to verify the ID (#8413)
This commit is contained in:
@@ -19,6 +19,8 @@ import Foundation
|
|||||||
/// Collection of thrown from the Flatbuffer verifier
|
/// Collection of thrown from the Flatbuffer verifier
|
||||||
public enum FlatbuffersErrors: Error, Equatable {
|
public enum FlatbuffersErrors: Error, Equatable {
|
||||||
|
|
||||||
|
/// Thrown when trying to verify a buffer that doesnt have the length of an ID
|
||||||
|
case bufferDoesntContainID
|
||||||
/// Thrown when verifying a file id that doesnt match buffer id
|
/// Thrown when verifying a file id that doesnt match buffer id
|
||||||
case bufferIdDidntMatchPassedId
|
case bufferIdDidntMatchPassedId
|
||||||
/// Prefixed size doesnt match the current (readable) buffer size
|
/// Prefixed size doesnt match the current (readable) buffer size
|
||||||
|
|||||||
@@ -201,8 +201,12 @@ public struct Verifier {
|
|||||||
_depth -= 1
|
_depth -= 1
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@inline(__always)
|
||||||
mutating func verify(id: String) throws {
|
mutating func verify(id: String) throws {
|
||||||
let size = MemoryLayout<Int32>.size
|
let size = MemoryLayout<Int32>.size
|
||||||
|
guard _capacity >= (size * 2) else {
|
||||||
|
throw FlatbuffersErrors.bufferDoesntContainID
|
||||||
|
}
|
||||||
let str = _buffer.readString(at: size, count: size)
|
let str = _buffer.readString(at: size, count: size)
|
||||||
if id == str {
|
if id == str {
|
||||||
return
|
return
|
||||||
|
|||||||
@@ -63,6 +63,17 @@ final class FlatbuffersVerifierTests: XCTestCase {
|
|||||||
XCTAssertThrowsError(try Verifier(buffer: &buffer))
|
XCTAssertThrowsError(try Verifier(buffer: &buffer))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func testFailingID() {
|
||||||
|
let dutData : [UInt8] = [1,2,3,4,5,6,7]
|
||||||
|
var buff = ByteBuffer(bytes: dutData)
|
||||||
|
do {
|
||||||
|
let _: Monster = try getCheckedRoot(byteBuffer: &buff, fileId: "ABCD")
|
||||||
|
XCTFail("This should always fail")
|
||||||
|
} catch {
|
||||||
|
XCTAssertEqual(error as? FlatbuffersErrors, .bufferDoesntContainID)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func testVerifierCheckAlignment() {
|
func testVerifierCheckAlignment() {
|
||||||
var verifier = try! Verifier(buffer: &buffer)
|
var verifier = try! Verifier(buffer: &buffer)
|
||||||
do {
|
do {
|
||||||
|
|||||||
Reference in New Issue
Block a user