Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Swift] Addresses a bug where verifier skips capacity when verifying ID on buffers #8413

Merged
merged 1 commit into from
Oct 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading