Skip to content

CFSocket: register the socket type and fix datagram send and address queries#62

Open
DTW-Thalion wants to merge 1 commit into
gnustep:masterfrom
DTW-Thalion:fix/cfsocket-send-addr
Open

CFSocket: register the socket type and fix datagram send and address queries#62
DTW-Thalion wants to merge 1 commit into
gnustep:masterfrom
DTW-Thalion:fix/cfsocket-send-addr

Conversation

@DTW-Thalion

Copy link
Copy Markdown
Contributor

Summary

CFSocketInitialize was never called from CFInitialize (it is missing from
both the forward-declaration block and the call list), so the CFSocket type was
never registered: _CFRuntimeCreateInstance rejected _kCFSocketTypeID == 0
and CFSocketCreateWithNative always returned NULL. CFSocket was entirely
unusable.
Registering it exposes three further bugs in the send/address code:

  • CFSocketSendData passed length 0 (and the data length as the flags)
    to sendto(), so no data was transmitted. It also treated sendto()/send()
    return values as zero-on-success, whereas they return the byte count — so the
    zero-length "send" was the only call that ever reported success.
  • CFSocketCopyPeerAddress read from and cached into the local-address
    field (_address) instead of _peerAddress.
  • CFSocketCopyAddress / CFSocketCopyPeerAddress called
    getsockname()/getpeername() with an uninitialised addrlen (a
    value-result argument that must be set to the buffer size on entry).

Fix

Add CFSocketInitialize() to CFInitialize; correct the sendto() argument
order and success test; use _peerAddress; initialise addrlen.

Test

Tests/CFSocket/general.m wraps a native UDP socket in a CFSocket, sends a
datagram over loopback and confirms it is received, and checks
CFSocketCopyAddress / CFSocketCopyPeerAddress. (All other corebase tests
still pass with the new type registered.)

CFSocketInitialize was never called from CFInitialize, so the CFSocket
type was never registered and CFSocketCreateWithNative always returned
NULL -- CFSocket was unusable.  Register it, and fix the bugs that were
hidden behind that:

* CFSocketSendData passed length 0 (and the data length as the flags) to
  sendto(), so no data was transmitted; it also treated sendto()/send()
  return values as zero-on-success when they return the byte count, so
  the zero-length "send" was the only call that reported success.
* CFSocketCopyPeerAddress read from and cached into the local-address
  field (_address) instead of _peerAddress.
* CFSocketCopyAddress and CFSocketCopyPeerAddress called
  getsockname()/getpeername() with an uninitialised addrlen.

Add Tests/CFSocket/general.m (loopback UDP send + address queries).
@DTW-Thalion DTW-Thalion force-pushed the fix/cfsocket-send-addr branch from 016eb25 to 250e8b9 Compare July 3, 2026 14:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant