Skip to content

Commit a2d2bee

Browse files
committed
fix: use withUnsafeBufferPointer correctly to prevent dangling pointer in release builds
1 parent 28ce250 commit a2d2bee

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

Plugins/RedisDriverPlugin/RedisPluginConnection.swift

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -577,9 +577,11 @@ private extension RedisPluginConnection {
577577
let cStrings: [UnsafeMutablePointer<CChar>] = args.map { arg in
578578
let utf8 = Array(arg.utf8)
579579
let ptr = UnsafeMutablePointer<CChar>.allocate(capacity: utf8.count + 1)
580-
if let base = utf8.withUnsafeBufferPointer({ $0.baseAddress }) {
581-
base.withMemoryRebound(to: CChar.self, capacity: utf8.count) { src in
582-
ptr.initialize(from: src, count: utf8.count)
580+
utf8.withUnsafeBufferPointer { buffer in
581+
if let base = buffer.baseAddress {
582+
base.withMemoryRebound(to: CChar.self, capacity: utf8.count) { src in
583+
ptr.initialize(from: src, count: utf8.count)
584+
}
583585
}
584586
}
585587
ptr[utf8.count] = 0

0 commit comments

Comments
 (0)