Skip to content

Commit

Permalink
Fixed a very obscure bug regarding contiguous vs dynamic backing stor…
Browse files Browse the repository at this point in the history
…age in arrays of bytes
  • Loading branch information
Obbut committed Oct 4, 2017
1 parent d6dda58 commit dfb2efc
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion Sources/BSON/DeserializationHelpers.swift
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import Foundation

internal func fromBytes<T, S : Collection>(_ bytes: S) throws -> T where S.Iterator.Element == Byte, S.IndexDistance == Int {
guard bytes.count >= MemoryLayout<T>.size else {
throw DeserializationError.invalidElementSize
}

return UnsafeRawPointer(Bytes(bytes)).assumingMemoryBound(to: T.self).pointee
return Data(bytes).withUnsafeBytes { (pointer: UnsafePointer<T>) in
return pointer.pointee
}
}

extension Int32 {
Expand Down

0 comments on commit dfb2efc

Please sign in to comment.