(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:
mustiikhalil
2024-10-05 00:16:41 +02:00
committed by GitHub
parent b127c57ff0
commit d7a70db6ac
3 changed files with 17 additions and 0 deletions

View File

@@ -19,6 +19,8 @@ import Foundation
/// Collection of thrown from the Flatbuffer verifier
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
case bufferIdDidntMatchPassedId
/// Prefixed size doesnt match the current (readable) buffer size

View File

@@ -201,8 +201,12 @@ public struct Verifier {
_depth -= 1
}
@inline(__always)
mutating func verify(id: String) throws {
let size = MemoryLayout<Int32>.size
guard _capacity >= (size * 2) else {
throw FlatbuffersErrors.bufferDoesntContainID
}
let str = _buffer.readString(at: size, count: size)
if id == str {
return

View File

@@ -63,6 +63,17 @@ final class FlatbuffersVerifierTests: XCTestCase {
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() {
var verifier = try! Verifier(buffer: &buffer)
do {