Skip to contentSkip to conversation

Improve format specifier for socket handle in Windows

PATCH In review Lev Stipakov 6 participants 7 messages Feb 10, 2023 – Feb 14, 2023

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Improve format specifier for socket handle in Windows

From: Lev Stipakov <[email protected]...>

Socket is a handle on Windows, which is usually logged in hex. Also an interesting value is INVALID_SOCKET, which is ~0.

PRIuPTR prints decimals, and for INVALID_SOCKET it prints something like

  2023-02-10 14:45:21 us=906000 write to TUN/TAP : Jrjestelmkutsulle

annettu data-alue on liian pieni. (fd=18446744073709551615,code=122)

PRIxPTR prints hex, and INVALID_SOCKET looks a bit nicer:

  2023-02-10 15:17:11 us=828000 write to TUN/TAP : Jrjestelmkutsulle

annettu data-alue on liian pieni. (fd=ffffffffffffffff,code=122)

---
 src/openvpn/syshead.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
src/openvpn/syshead.h +1 -1
index 12ccf2f4..53359225 100644
@@ -441,7 +441,7 @@ typedef unsigned short sa_family_t;
  */
 #ifdef _WIN32
 #define SOCKET_UNDEFINED (INVALID_SOCKET)
-#define SOCKET_PRINTF "%" PRIuPTR
+#define SOCKET_PRINTF "%" PRIxPTR
 typedef SOCKET socket_descriptor_t;
 #else
 #define SOCKET_UNDEFINED (-1)
Signature · 1 line
2.23.0.windows.1
Acked-by on v1 1/1
#1 lore raw
Quoted 7 lines · Lev Stipakov

From: Lev Stipakov <[email protected]...>

Socket is a handle on Windows, which is usually logged in hex. Also an interesting value is INVALID_SOCKET, which is ~0.

PRIuPTR prints decimals, and for INVALID_SOCKET it prints something like

Quoted 8 lines
  2023-02-10 14:45:21 us=906000 write to TUN/TAP : Jrjestelmkutsulle

annettu data-alue on liian pieni. (fd=18446744073709551615,code=122)

PRIxPTR prints hex, and INVALID_SOCKET looks a bit nicer:

  2023-02-10 15:17:11 us=828000 write to TUN/TAP : Jrjestelmkutsulle

annettu data-alue on liian pieni. (fd=ffffffffffffffff,code=122)

Regards,

Signature · 1 line
  Frank Lichtenheld
Acked-by on v1 1/1
#2 lore raw
In reply to Lev Stipakov

Hi,

Quoted 10 lines · Lev Stipakov

From: Lev Stipakov <[email protected]...>

Socket is a handle on Windows, which is usually logged in hex. Also an interesting value is INVALID_SOCKET, which is ~0.

PRIuPTR prints decimals, and for INVALID_SOCKET it prints something like

   2023-02-10 14:45:21 us=906000 write to TUN/TAP : Jrjestelmkutsulle

annettu data-alue on liian pieni. (fd=18446744073709551615,code=122)

Quoted 7 lines

PRIxPTR prints hex, and INVALID_SOCKET looks a bit nicer:

   2023-02-10 15:17:11 us=828000 write to TUN/TAP : Jrjestelmkutsulle

annettu data-alue on liian pieni. (fd=ffffffffffffffff,code=122)

I also discussed this with Lev and, despite this being different from what we do in the *nix world (where decimal representations make sense for file descriptors), it seems to be the right hting to do on Windows when using HANDLEs (shrug).

Signature · 1 line
Antonio Quartulli
#3 lore raw

Hi

Quoted 18 lines · Antonio Quartulli

Hi,

Quoted 15 lines · Lev Stipakov

From: Lev Stipakov <[email protected]...>

Socket is a handle on Windows, which is usually logged in hex. Also an interesting value is INVALID_SOCKET, which is ~0.

PRIuPTR prints decimals, and for INVALID_SOCKET it prints something like

   2023-02-10 14:45:21 us=906000 write to TUN/TAP : Jrjestelmkutsulle

annettu data-alue on liian pieni. (fd=18446744073709551615,code=122)

PRIxPTR prints hex, and INVALID_SOCKET looks a bit nicer:

   2023-02-10 15:17:11 us=828000 write to TUN/TAP : Jrjestelmkutsulle

annettu data-alue on liian pieni. (fd=ffffffffffffffff,code=122)

Quoted 8 lines
Quoted 2 lines

I also discussed this with Lev and, despite this being different from what we do in the *nix world (where decimal representations make sense for file descriptors), it seems to be the right hting to do on Windows when using HANDLEs (shrug).

Windows kernel handles are very much "like" fds though the handle table also covers processes, threads, semaphores etc. They are not pointers, are generally smallish numbers or ~0 (== -1). But not as small as fds in the Unix/Linux world --- mainly because several objects share the name space and they have to be multiples of 4 for some arcane reason.

The problem here was not printing as "decimal" but as "unsigned decimal". Use of the latter would have caused the same issue on Linux too.

Using hex is a compromise here as handles are defined as "unsigned ints" of the same size as pointers. So PRIdPTR somehow looks improper though I would have chosen that.

Selva

#4 lore raw

Hi,

Quoted 7 lines · Selva Nair

I also discussed this with Lev and, despite this being different from what we do in the *nix world (where decimal representations make sense for file descriptors), it seems to be the right hting to do on Windows when using HANDLEs (shrug).

Quoted 5 lines

Windows kernel handles are very much "like" fds though the handle table also covers processes, threads,  semaphores etc. They are not pointers, are generally smallish numbers or ~0 (== -1). But not as small as fds in the Unix/Linux world --- mainly because several objects share the name space and they have to be multiples of 4 for some arcane reason.

The problem here was not printing as "decimal" but as "unsigned decimal". Use of the latter would have caused the same issue on Linux too.

Using hex is a compromise here as handles are defined as "unsigned ints" of the same size as pointers. So PRIdPTR somehow looks improper though I would have chosen that.

Given your explanation then it sounds like we should truly use PRIdPTR. Wouldn't you agree Lev?

Cheers,

Quoted 8 lines

Selva

_______________________________________________
Openvpn-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/openvpn-devel

Signature · 1 line
Antonio Quartulli
#5 lore raw

Am 10.02.23 um 23:07 schrieb Antonio Quartulli:

Quoted 10 lines

Hi,

Quoted 7 lines · Selva Nair

    I also discussed this with Lev and, despite this being different from what we do in the *nix world (where decimal representations make sense for file descriptors), it seems to be the right hting to do on Windows when using HANDLEs (shrug).

Quoted 8 lines
Quoted 5 lines

Windows kernel handles are very much "like" fds though the handle table also covers processes, threads,  semaphores etc. They are not pointers, are generally smallish numbers or ~0 (== -1). But not as small as fds in the Unix/Linux world --- mainly because several objects share the name space and they have to be multiples of 4 for some arcane reason.

The problem here was not printing as "decimal" but as "unsigned decimal". Use of the latter would have caused the same issue on Linux too.

Using hex is a compromise here as handles are defined as "unsigned ints" of the same size as pointers. So PRIdPTR somehow looks improper though I would have chosen that.

Given your explanation then it sounds like we should truly use PRIdPTR. Wouldn't you agree Lev?

Not really. The type is still an unsigned type and while -1 is used to denote an invalid HANDLE, the type itself is unsigned, so using a signed printf format specifier like PRIdPTR will probably give us a warning about using about signed vs unsigned.

Arne

#6 lore raw
In reply to Lev Stipakov

I'm so not testing this... but I do not need to, the change is trivial (ok, I cheated, I did test-compile on MinGW).

Your patch has been applied to the master branch.

commit 6731314a82d1a3c76b5497749985ee20c0c7d8eb (master) commit 8e3331a901dbececc8622e97ed0592ddadf56996 (release/2.6)

Author: Lev Stipakov
Date:   Fri Feb 10 15:31:59 2023 +0200
     Improve format specifier for socket handle in Windows

     Signed-off-by: Lev Stipakov <[email protected]...>
     Acked-by: Frank Lichtenheld <[email protected]...>
     Acked-by: Antonio Quartulli <[email protected]...>
     Message-Id: <[email protected]...>
     URL: https://www.mail-archive.com/[email protected]/msg26220.html
     Signed-off-by: Gert Doering <[email protected]...>

--
kind regards,

Gert Doering