Skip to content

Commit 802bd39

Browse files
committed
(fix): #8408 fixes a bug where the capacity of the buffer isnt verified before trying to verify the ID
1 parent 2436bd8 commit 802bd39

File tree

3 files changed

+17
-0
lines changed

3 files changed

+17
-0
lines changed

swift/Sources/FlatBuffers/FlatbuffersErrors.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ import Foundation
1919
/// Collection of thrown from the Flatbuffer verifier
2020
public enum FlatbuffersErrors: Error, Equatable {
2121

22+
/// Thrown when trying to verify a buffer that doesnt have the length of an ID
23+
case bufferDoesntContainID
2224
/// Thrown when verifying a file id that doesnt match buffer id
2325
case bufferIdDidntMatchPassedId
2426
/// Prefixed size doesnt match the current (readable) buffer size

swift/Sources/FlatBuffers/Verifier.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,8 +201,12 @@ public struct Verifier {
201201
_depth -= 1
202202
}
203203

204+
@inline(__always)
204205
mutating func verify(id: String) throws {
205206
let size = MemoryLayout<Int32>.size
207+
guard _capacity >= (size * 2) else {
208+
throw FlatbuffersErrors.bufferDoesntContainID
209+
}
206210
let str = _buffer.readString(at: size, count: size)
207211
if id == str {
208212
return

tests/swift/tests/Tests/FlatBuffers.Test.SwiftTests/FlatbuffersVerifierTests.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,17 @@ final class FlatbuffersVerifierTests: XCTestCase {
6363
XCTAssertThrowsError(try Verifier(buffer: &buffer))
6464
}
6565

66+
func testFailingID() {
67+
let dutData : [UInt8] = [1,2,3,4,5,6,7]
68+
var buff = ByteBuffer(bytes: dutData)
69+
do {
70+
let _: Monster = try getCheckedRoot(byteBuffer: &buff, fileId: "ABCD")
71+
XCTFail("This should always fail")
72+
} catch {
73+
XCTAssertEqual(error as? FlatbuffersErrors, .bufferDoesntContainID)
74+
}
75+
}
76+
6677
func testVerifierCheckAlignment() {
6778
var verifier = try! Verifier(buffer: &buffer)
6879
do {

0 commit comments

Comments
 (0)