Kitura / Kitura-WebSocket

WebSocket support for Kitura
Apache License 2.0
68 stars 30 forks source link

Request to cleanup some uses of bindMemory and assumingMemoryBound #96

Closed atrick closed 5 years ago

atrick commented 5 years ago

Doing a random github search I found some potential misuses of the bindMemory API

https://github.com/IBM-Swift/Kitura-WebSocket/blob/0fdd351196addf30a789956dc71fc045d9ee5580/Tests/KituraWebSocketTests/KituraTest%2BFrames.swift

which should be written (safely) as

  _ = bytes.load(fromByteOffset: position, as: UInt8.self)

  let payloadBytes = UnsafeRawBufferPointer(bytes, capacity: length)
  _ = payloadBytes[i]

And of assumingMemoryBound(to:)

https://github.com/IBM-Swift/Kitura-WebSocket/blob/0fdd351196addf30a789956dc71fc045d9ee5580/Sources/KituraWebSocket/WSFrameParser.swift

which should be written as:

let bytes = UnsafeRawBufferPointer(buffer.bytes, capacity: buffer.length)

private mutating func parseOpCode(bytes: UnsafeRawBufferPointer, from: Int) -> (WebSocketError?, Int) {
                                         ^^^^^^^^^^^^^^^^^^^^^^

bytes.load(fromByteOffset: from+1, as: UInt16.self)

Same thing here:

https://github.com/IBM-Swift/Kitura-WebSocket/blob/0fdd351196addf30a789956dc71fc045d9ee5580/Tests/KituraWebSocketTests/KituraTest%2BFrames.swift

And here:

https://github.com/IBM-Swift/Kitura-WebSocket/blob/231ee01f9d17ffb68bebce061b624bf31dbb013b/Tests/KituraWebSocketTests/KituraTest.swift

ianpartridge commented 5 years ago

Thanks!