Skip to content

Commit 985a3f5

Browse files
authored
fix incorrect usages of NIO unsafe API (#48)
* fix string->bytes method, #44 * fix getUUID method, #45 * at label * better readFloat/Double methods, #46 * remove unused get float/double methods
1 parent 020658a commit 985a3f5

File tree

4 files changed

+17
-51
lines changed

4 files changed

+17
-51
lines changed

Sources/PostgresNIO/Connection/PostgresConnection+Authenticate.swift

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,6 @@ private final class PostgresAuthenticationRequest: PostgresRequest {
104104
}
105105

106106
func bytes(_ string: String) -> [UInt8] {
107-
return string.withCString { ptr in
108-
return UnsafeBufferPointer(start: ptr, count: string.count).withMemoryRebound(to: UInt8.self) { buffer in
109-
return [UInt8](buffer)
110-
}
111-
}
107+
return Array(string.utf8)
112108
}
113109
}

Sources/PostgresNIO/Data/PostgresData+Double.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ extension PostgresData {
1414
case .binary:
1515
switch self.type {
1616
case .float4:
17-
return value.readFloat(as: Float.self)
17+
return value.readFloat()
1818
.flatMap { Double($0) }
1919
case .float8:
20-
return value.readFloat(as: Double.self)
20+
return value.readDouble()
2121
case .numeric:
2222
return self.numeric?.double
2323
default:

Sources/PostgresNIO/Data/PostgresData+Float.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ extension PostgresData {
1212
case .binary:
1313
switch self.type {
1414
case .float4:
15-
return value.readFloat(as: Float.self)
15+
return value.readFloat()
1616
case .float8:
17-
return value.readFloat(as: Double.self)
17+
return value.readDouble()
1818
.flatMap { Float($0) }
1919
default:
2020
return nil

Sources/PostgresNIO/Utilities/NIOUtils.swift

Lines changed: 12 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -65,37 +65,15 @@ internal extension ByteBuffer {
6565
}
6666
return array
6767
}
68-
69-
mutating func readFloat<T: BinaryFloatingPoint>(as: T.Type = T.self) -> T? {
70-
guard self.readableBytes >= MemoryLayout<T>.size else {
71-
return nil
72-
}
73-
74-
let value: T = self.getFloat(at: self.readerIndex)! /* must work as we have enough bytes */
75-
// should be MoveReaderIndex
76-
self.moveReaderIndex(forwardBy: MemoryLayout<T>.size)
77-
return value
68+
69+
mutating func readFloat() -> Float? {
70+
return self.readInteger(as: UInt32.self).map { Float(bitPattern: $0) }
7871
}
7972

80-
func getFloat<T: BinaryFloatingPoint>(at index: Int, as: T.Type = T.self) -> T? {
81-
precondition(index >= 0, "index must not be negative")
82-
return self.withVeryUnsafeBytes { ptr in
83-
guard index <= ptr.count - MemoryLayout<T>.size else {
84-
return nil
85-
}
86-
var value: T = 0
87-
withUnsafeMutableBytes(of: &value) { valuePtr in
88-
valuePtr.copyBytes(
89-
from: UnsafeRawBufferPointer(
90-
start: ptr.baseAddress!.advanced(by: index),
91-
count: MemoryLayout<T>.size
92-
).reversed()
93-
)
94-
}
95-
return value
96-
}
73+
mutating func readDouble() -> Double? {
74+
return self.readInteger(as: UInt64.self).map { Double(bitPattern: $0) }
9775
}
98-
76+
9977
mutating func readUUID() -> UUID? {
10078
guard self.readableBytes >= MemoryLayout<UUID>.size else {
10179
return nil
@@ -108,21 +86,13 @@ internal extension ByteBuffer {
10886
}
10987

11088
func getUUID(at index: Int) -> UUID? {
111-
precondition(index >= 0, "index must not be negative")
112-
return self.withVeryUnsafeBytes { ptr in
113-
guard index <= ptr.count - MemoryLayout<uuid_t>.size else {
114-
return nil
115-
}
116-
var value: uuid_t = (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
117-
withUnsafeMutableBytes(of: &value) { valuePtr in
118-
valuePtr.copyMemory(
119-
from: UnsafeRawBufferPointer(
120-
start: ptr.baseAddress!.advanced(by: index),
121-
count: MemoryLayout<UUID>.size
122-
)
123-
)
89+
var uuid: uuid_t = (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
90+
return self.viewBytes(at: index, length: MemoryLayout.size(ofValue: uuid)).map { bufferBytes in
91+
withUnsafeMutableBytes(of: &uuid) { target in
92+
precondition(target.count <= bufferBytes.count)
93+
target.copyBytes(from: bufferBytes)
12494
}
125-
return UUID(uuid: value)
95+
return UUID(uuid: uuid)
12696
}
12797
}
12898
}

0 commit comments

Comments
 (0)