Skip to content

Commit

Permalink
Fix crashing on parsing ObjectId as String in Swift 5.7
Browse files Browse the repository at this point in the history
  • Loading branch information
Joannis committed Jun 17, 2022
1 parent 382c037 commit e5f3c18
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 18 deletions.
9 changes: 0 additions & 9 deletions Sources/BSON/Helpers/PointerHelpers.swift

This file was deleted.

13 changes: 4 additions & 9 deletions Sources/BSON/Types/ObjectId.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,7 @@ public struct ObjectId: Sendable {

/// Decodes the ObjectID from the provided (24 character) hexString
public init?(_ hex: String) {
let storage = UnsafeMutablePointer<UInt8>.allocate(capacity: 12)
defer {
storage.deallocate()
}
var buffer = ByteBufferAllocator().buffer(capacity: 12)

let cString = hex.utf8CString

Expand All @@ -44,7 +41,6 @@ public struct ObjectId: Sendable {
}

var input = 0
var output = 0
while input < 23 {
guard
let c1 = cString[input].hexDecoded(),
Expand All @@ -53,14 +49,13 @@ public struct ObjectId: Sendable {
return nil
}

storage[output] = UInt8(bitPattern: c1 << 4) | UInt8(bitPattern: c2)
buffer.writeInteger(UInt8(bitPattern: c1 << 4) | UInt8(bitPattern: c2))

input = input &+ 2
output = output &+ 1
}

_timestamp = storage.withMemoryRebound(to: UInt32.self, capacity: 1) { $0.pointee.bigEndian }
_random = storage.advanced(by: 4).withMemoryRebound(to: UInt64.self, capacity: 1) { $0.pointee.bigEndian }
_timestamp = buffer.readInteger()!
_random = buffer.readInteger()!
}

/// The 12 bytes represented as 24-character hex-string
Expand Down

0 comments on commit e5f3c18

Please sign in to comment.