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 <lukasa@apple.com>
This commit is contained in:
Andrew Trick 2023-02-28 00:23:36 -08:00 committed by GitHub
parent a296f30e45
commit e63326aa50
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 9 additions and 9 deletions

View File

@ -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))
}
}
}