Add defensive lifetime management for security metadata (#152)
Motivation: Right now we're playing a little fast and loose with the lifetimes of the sec_protocol_metadata_t. As a practical matter it is highly likely that this is owned (and so kept alive by) the NWConnection, but rather than risk that we should tighten up the lifetime management. Modifications: Use withExtendedLifetime to extend the lifetime. Result: Better lifetime management.
This commit is contained in:
parent
acb6425a09
commit
605f7a4c55
|
|
@ -795,9 +795,16 @@ extension NIOTSConnectionChannel {
|
|||
|
||||
if let metadata = self.nwConnection?.metadata(definition: NWProtocolTLS.definition) as? NWProtocolTLS.Metadata {
|
||||
// This is a TLS connection, we may need to fire some other events.
|
||||
let negotiatedProtocol = sec_protocol_metadata_get_negotiated_protocol(metadata.securityProtocolMetadata).map {
|
||||
String(cString: $0)
|
||||
let securityMetadata = metadata.securityProtocolMetadata
|
||||
|
||||
// The pointer returned by `sec_protocol_metadata_get_negotiated_protocol` is presumably owned by it, so we need
|
||||
// to confirm it's still alive while we copy the data out.
|
||||
let negotiatedProtocol = withExtendedLifetime(securityMetadata) {
|
||||
sec_protocol_metadata_get_negotiated_protocol(metadata.securityProtocolMetadata).map {
|
||||
String(cString: $0)
|
||||
}
|
||||
}
|
||||
|
||||
self.pipeline.fireUserInboundEventTriggered(TLSUserEvent.handshakeCompleted(negotiatedProtocol: negotiatedProtocol))
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue