From e5353ab3a36c94a566adb22a0ec6a2c4926e95be Mon Sep 17 00:00:00 2001 From: Benjamin Sergeant Date: Tue, 17 Nov 2026 12:40:12 -0800 Subject: [PATCH] Close SecureTransport native sockets without a TLS context --- ixwebsocket/IXSocketAppleSSL.cpp | 11 ++++++----- test/IXSocketAppleSSLReadTest.cpp | 18 ++++++++++++++++++ 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/ixwebsocket/IXSocketAppleSSL.cpp b/ixwebsocket/IXSocketAppleSSL.cpp index 7d96ca06..8cb6b325 100644 --- a/ixwebsocket/IXSocketAppleSSL.cpp +++ b/ixwebsocket/IXSocketAppleSSL.cpp @@ -254,11 +254,12 @@ namespace ix { std::lock_guard lock(_mutex); - if (_sslContext == nullptr) return; - - SSLClose(_sslContext); - CFRelease(_sslContext); - _sslContext = nullptr; + if (_sslContext != nullptr) + { + SSLClose(_sslContext); + CFRelease(_sslContext); + _sslContext = nullptr; + } Socket::close(); } diff --git a/test/IXSocketAppleSSLReadTest.cpp b/test/IXSocketAppleSSLReadTest.cpp index 60d5c8f9..6fb87684 100644 --- a/test/IXSocketAppleSSLReadTest.cpp +++ b/test/IXSocketAppleSSLReadTest.cpp @@ -3,7 +3,10 @@ * Copyright (c) 2026 Machine Zone, Inc. All rights reserved. */ #include +#include +#include #include +#include namespace ix { @@ -48,6 +51,21 @@ namespace ix }; } // namespace ix +TEST_CASE("SecureTransport closes the native socket without a TLS context", "[socket_apple][close]") +{ + const bool contextCreated = GENERATE(false, true); + const int fd = ::socket(AF_INET, SOCK_STREAM, 0); + REQUIRE(fd != -1); + ix::SocketAppleSSL socket(ix::SocketTLSOptions {}, fd); + if (contextCreated) ix::SocketAppleSSLReadTestAccess::install(socket); + + socket.close(); + errno = 0; + CHECK(fcntl(fd, F_GETFD) == -1); + CHECK(errno == EBADF); + socket.close(); +} + TEST_CASE("SecureTransport terminal errors clear a previous wait status", "[socket_apple][read]") { const bool reading = GENERATE(false, true);