FlyingFox icon indicating copy to clipboard operation
FlyingFox copied to clipboard

`withSockAddr()` unsafe

Open lhoward opened this issue 1 month ago • 1 comments

As I understand it, withSockAddr() unsafely casts an existential to a sockaddr, which does work reliably, possibly for alignment reasons. On my system it fails (returns garbage) with domain sockets. Round-tripping via sockaddr_storage seems to work for me.

In my unrelated SocketAddress package [1], withSockAddr is a requirement of the protocol, so you get a pointer to the non-existential (underlying C type).

public protocol SocketAddress: Sendable {
  static var family: sa_family_t { get }

  init(family: sa_family_t, presentationAddress: String) throws
  func withSockAddr<T>(_ body: (_ sa: UnsafePointer<sockaddr>) throws -> T) rethrows -> T

  var presentationAddress: String { get throws }
  var presentationAddressNoPort: String { get throws }

  var port: UInt16 { get throws }
  var size: socklen_t { get }
}

[1] https://github.com/PADL/SocketAddress

lhoward avatar Nov 13 '25 23:11 lhoward