fix: Windows bsdsocket bugs and consolidate platform-independent code#1793
Merged
midwan merged 2 commits intoBlitterStudio:masterfrom Feb 18, 2026
Merged
Conversation
Three bugs in the Windows bsdsocket emulation path and one dead declaration are fixed, and six functions duplicated identically between the Windows and POSIX sections are consolidated into shared platform-independent code. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
midwan
approved these changes
Feb 17, 2026
Contributor
Author
|
@midwan it looks like the MacOS cert issue is now blocking, or this needs your help to complete. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
PRs #1787-#1792 fixed 24 bugs in the POSIX bsdsocket emulation path. Three of those bugs also exist in the Windows
#ifdef _WIN32code path, which shares a common WinUAE heritage but has diverged over time. This PR fixes the Windows copies of those bugs and addresses the root cause of the divergence: six functions that were duplicated identically between the Windows and POSIX sections are consolidated into shared platform-independent code inbsdsocket.cpp.The duplication is what allowed these bugs to drift between platforms in the first place.
getservbyportbyte order andgethostnamewrite-back were fixed in the POSIX path (PR #1791) but the Windows copies remained broken. By moving platform-independent logic to shared code, future fixes apply to both platforms automatically.These issues were identified by cross-referencing the fixes from PRs #1787-#1792 against the Windows code path using bsdsocktest, a conformance test suite for Amiga
bsdsocket.library.Bug A: getservbyport byte order (Windows)
The problem
The Amiga passes the port number in network byte order (big-endian) via register D0. The host
getservbyport()expects its port argument in network byte order, but on a little-endian host, the raw 32-bit register value has the wrong byte order. For example, port 21 (0x0015) arrives as 0x00150000, sogetservbyport()looks up port 5376 instead.This was fixed in the POSIX path in PR #1791 (test 94) but the Windows copy at line 2304 was not updated.
The fix
Add
htons((unsigned short)nameport)to convert the register value to the host's network byte order, matching the POSIX fix.Bug B: Dup2Socket out-of-bounds memory access (Windows)
The problem
Two sub-bugs in the Windows
host_dup2socket():Missing return after bounds check: When
fd2exceedsdtablesize, the function sets errno to EBADF but execution falls through tosetsd()with the out-of-range descriptor, corrupting memory beyond the descriptor table.Unchecked
getsd()return: Whenfd2 == -1(allocate new slot),getsd()returns -1 if the descriptor table is full. The unchecked return passes -1 tosetsd(ctx, sb, -1, s1), which accessesdtable[-2]-- an out-of-bounds write.These were fixed in the POSIX path in PR #1791 (test 116). The Windows version additionally lacks a proper
dup()equivalent -- two descriptor table entries alias the same SOCKET handle, so closing one invalidates the other. The correct Windows fix requiresWSADuplicateSocket()+WSASocket(), but this is a pre-existing WinUAE design choice and out of scope for this change (documented with a TODO).The fix
Add
return -1after the bounds-check errno, and add afd2 == -1guard aftergetsd(). This prevents the crashes while preserving the existing handle-aliasing behavior.Refactor: consolidate platform-independent functions
The problem
Six functions existed as near-identical copies in both the
#ifdef _WIN32and#elsesections ofbsdsocket_host.cpp. This duplication meant that bug fixes applied to one platform didn't automatically reach the other, which is exactly how Bugs A and B (and thegethostnamewrite-back bug below) persisted in the Windows path after the POSIX fixes landed.What moved
host_Inet_LnaOfuae_u32. Byte-for-byte identical across platforms.host_Inet_NetOfhost_Inet_MakeAddrhost_inet_addrinet_addr()(available via<winsock2.h>on Windows,<arpa/inet.h>on POSIX). Windows version'sBSDTRACEcall preserved.host_Inet_NtoAtrap_get_areg(ctx, 6)(modern trap-safe API from Windows version) andbsdsocklib_seterrno(ctx, sb, 22)instead of the platform-specificSETERRNOmacro.host_gethostnamegethostname()then writes the result back to Amiga memory viatrap_put_string(). This simultaneously fixes the Windows bug where the function read FROM Amiga memory but never wrote the hostname back (PR #1791 test 98 fix, Windows copy not updated).All six functions now live in
bsdsocket.cppinside the existing#ifdef BSDSOCKETblock. A#ifndef _WIN32/#include <arpa/inet.h>/#endifguard provides the necessary prototypes on POSIX; Windows gets them via<winsock2.h>.The
#include <arpa/inet.h>transitively pulls in<sys/socket.h>on Linux, which definesMSG_EORandMSG_TRUNCwith different values than the Amiga equivalents. Added#undeffor both before the Amiga#defines to prevent redefinition warnings.Cleanup
Removed the dead
host_inet_networkdeclaration frombsdsocket.h. This function was declared but never implemented in either platform section. The dispatch inbsdsocket.cppcallshost_inet_addr()instead.Pre-existing issues documented
Added TODO comments to four additional unchecked
getsd()call sites (2 Windows, 2 POSIX) that have the same out-of-bounds access pattern as Bug B. These exist inhost_socket()andhost_accept()on both platforms and should be addressed in a follow-up.Validation
Linux (Debian 13, kernel 6.12, x86_64):
/etc/networksdatabase)Windows:
#ifdef _WIN32) or shared. CI will verify compilation. For runtime validation, bsdsocktest 0.2.3 can be used to exercise the affected test cases (particularly tests 94, 98, 115-116).Files changed
src/bsdsocket.cpp-- 109 insertions (include guard, 6 shared functions)src/include/bsdsocket.h-- 1 deletion (deadhost_inet_networkdeclaration)src/osdep/bsdsocket_host.cpp-- 22 insertions, 182 deletions (2 bug fixes, 6 function removals from both sections, 4 TODO comments)