From e63326aa50d68fc26288dbd2b78bfb503bc58edd Mon Sep 17 00:00:00 2001 From: Andrew Trick Date: Tue, 28 Feb 2023 00:23:36 -0800 Subject: [PATCH] Fix an upcoming compiler warning on implicit raw pointer casts. (#2377) setOption forms a raw pointer to a generic argument. The compiler will warn on this as of: [proposal] Constrain implicit raw pointer conversion... #1963 https://github.com/apple/swift-evolution/pull/1963 /Sources/NIOPosix/BaseSocket.swift:286:31: warning: forming 'UnsafeRawPointer' to a variable of type 'T'; this is likely incorrect because 'T' may contain an object reference. option_value: &val, ^ Ideally, this would be fixed by adding a BitwiseCopyable constraint to the 'value' parameter of 'BaseSocker.setOption'. That would not only eliminate the warning, but would make the API safer. But BitwiseCopyable isn't quite ready for public use. In the meantime, this is a reasonable workaround. Co-authored-by: Cory Benfield --- Sources/NIOPosix/BaseSocket.swift | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/Sources/NIOPosix/BaseSocket.swift b/Sources/NIOPosix/BaseSocket.swift index a16e2c452..400357982 100644 --- a/Sources/NIOPosix/BaseSocket.swift +++ b/Sources/NIOPosix/BaseSocket.swift @@ -276,15 +276,15 @@ class BaseSocket: BaseSocketProtocol { // most socket options settings so for the time being we'll just ignore this. Let's revisit for NIO 2.0. return } - return try self.withUnsafeHandle { - var val = value - - try NIOBSDSocket.setsockopt( - socket: $0, - level: level, - option_name: name, - option_value: &val, - option_len: socklen_t(MemoryLayout.size(ofValue: val))) + return try withUnsafeBytes(of: value) { (valueBuffer: UnsafeRawBufferPointer) in + try self.withUnsafeHandle { + try NIOBSDSocket.setsockopt( + socket: $0, + level: level, + option_name: name, + option_value: valueBuffer.baseAddress!, + option_len: socklen_t(valueBuffer.count)) + } } }