Skip to content

Commit

Permalink
(fix): #8408 fixes a bug where the capacity of the buffer isnt verifi…
Browse files Browse the repository at this point in the history
…ed before trying to verify the ID (#8413)
  • Loading branch information
mustiikhalil authored Oct 4, 2024
1 parent b127c57 commit d7a70db
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 0 deletions.
2 changes: 2 additions & 0 deletions swift/Sources/FlatBuffers/FlatbuffersErrors.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions swift/Sources/FlatBuffers/Verifier.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit d7a70db

Please sign in to comment.