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:
Cory Benfield 2022-07-05 09:25:11 +01:00 committed by GitHub
parent acb6425a09
commit 605f7a4c55
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 9 additions and 2 deletions

View File

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