apple / swift-corelibs-foundation

The Foundation Project, providing core utilities, internationalization, and OS independence
swift.org
Apache License 2.0
5.23k stars 1.12k forks source link

CFSocket should use dynamically-sized `fd_set` on Windows #4958

Open tristanlabelle opened 1 month ago

tristanlabelle commented 1 month ago

The fd_set structure on Windows has a trailing array of socket that defaults to size 64, which we override to 1024 https://github.com/apple/swift-corelibs-foundation/blob/06072877b110c4ba5501ddc012830c1072f7198c/CoreFoundation/Base.subproj/CoreFoundation_Prefix.h#L133C1-L133C24 .

Instead of using a large fixed-sized buffer, we can treat the structure as dynamically-sized and reallocate it when it gets full. Winsock functions read the size of the array from the count field in order to support the array to exceed 64 as needed. The only caveat is that we can't use the FD_SET macro because it has to use the compile-time size.