Socket channel names contain a heap address (visible to safe interps)
set srv [socket -server {apply {{c a p} {close $c}}} -myaddr 127.0.0.1 0]
set c [socket 127.0.0.1 [lindex [chan configure $srv -sockname] 2]]
set s [interp create -safe]
interp transfer {} $c $s
puts [interp eval $s {chan names}]
Actual (9.1, 9.0, 8.6.18 on Linux): sock5644b332a410. Expected: a name that does not depend on memory layout, e.g. sock plus a counter, as reflected channels already do (rc%lu in generic/tclIORChan.c).
Where the names are formed:
- Unix: SOCK_TEMPLATE in unix/tclUnixSock.c, used with PTR2INT(statePtr) at all four Tcl_CreateChannel calls. Since [8eefe5a06f] (2011, the IPv6 socket merge, first in 8.6.0); up to 8.5 the name was sock + fd. The fd could not be kept because a listening socket may have several fds.
- Windows: SOCK_TEMPLATE "sock%p" in win/tclWinSock.c since [fdd8917252] (2014, first in 8.6.2). In addition TclWinGenerateChannelName (win/tclWinChan.c) names file, pipe, serial and console channels file + pointer to their info structure; that has been so at least since 8.4 ("file%lx"). Not tested on Windows, read from the sources.
- Unix file, pipe and serial channels use the fd (file%d, serial%d), zipfs channels an archive offset and a counter, reflected channels a counter; stacked channels (zlib, transforms) keep the name of the base channel. Those do not leak addresses.
Because the address is reused once the structure is freed, the same name also comes back quickly after a close (three connect/close cycles above gave sock...a490 sock...a510 sock...a490), the same reuse problem as with fd-based names ([2826430fff]).
A per-process counter, as for reflected channels, would fix both. This is a narrower part of [1577730fff] (channel names given to a safe interpreter show the channel type); unlike that ticket it needs no new API.
cbb6334c93Fix [d6c1d2f78e]: socket channel names (and on Windows all channel names) contained a heap address, visible to safe interps. Use a per-process counter instead. check-in2026-10-17 12:25:502f842c82b7Ticket [1577730fff] interp transfer leaks information when transfering channels status still Open with 3 other changes ticket2026-10-17 10:53:34
Proposed fix in branch channel-names-no-address: the number in socket names on Unix, and in all channel names on Windows (TclWinGenerateChannelName()), is now a per-process counter instead of an address. Names are no longer reused after a close either. Test socket-bug-d6c1d2f78e, passes on Linux and Windows.