From f8ab76ba619ab46df97e97966ccfc49ac1dd58c4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=8F=A1=E7=8C=AB=E7=8C=AB?= <164346864@qq.com> Date: Wed, 26 Mar 2025 03:32:12 +0800 Subject: [PATCH] Update nativesockets.nim, `namelen` should be the len of `name` (#24810) In other places where `getsockname` is called, the size of the 'name' is used. https://github.com/nim-lang/Nim/blob/d573578b28bc4393dac7f3154b5da29b1fa75358/lib/pure/nativesockets.nim#L347-L351 https://github.com/nim-lang/Nim/blob/d573578b28bc4393dac7f3154b5da29b1fa75358/lib/pure/nativesockets.nim#L585-L595 https://github.com/nim-lang/Nim/blob/d573578b28bc4393dac7f3154b5da29b1fa75358/lib/pure/nativesockets.nim#L622-L624 https://github.com/nim-lang/Nim/blob/d573578b28bc4393dac7f3154b5da29b1fa75358/lib/pure/nativesockets.nim#L347-L350 I have checked the [Windows documentation](https://learn.microsoft.com/en-us/windows/win32/api/winsock2/nf-winsock2-getsockname#remarks), and it describes it like this: "On call, the namelen parameter contains the size of the name buffer, in bytes. On return, the namelen parameter contains the actual size in bytes of the name parameter." [https://www.man7.org/linux/man-pages/man2/getsockname.2.html](https://www.man7.org/linux/man-pages/man2/getsockname.2.html) say: The addrlen argument should be initialized to indicate the amount of space (in bytes) pointed to by addr. (cherry picked from commit 8e36fb0fec90fb3a6abdd485755471a5578f83c7) --- lib/pure/nativesockets.nim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/pure/nativesockets.nim b/lib/pure/nativesockets.nim index 2bae53d6c8..765be085d0 100644 --- a/lib/pure/nativesockets.nim +++ b/lib/pure/nativesockets.nim @@ -723,7 +723,7 @@ when useNimNetLite: ## ## Similar to POSIX's `getsockname`:idx:. template sockGetNameOrRaiseError(socket: untyped, name: untyped) = - var namelen = sizeof(socket).SockLen + var namelen = sizeof(name).SockLen if getsockname(socket, cast[ptr SockAddr](addr(name)), addr(namelen)) == -1'i32: raiseOSError(osLastError())