aboutsummaryrefslogtreecommitdiff
path: root/crypto/compat/posix_win.c
diff options
context:
space:
mode:
authordatadiode <datadiode@users.noreply.github.com>2024-07-17 10:40:05 +0200
committerBrent Cook <busterb@gmail.com>2025-04-12 15:58:07 -0500
commit2c5e2c60d92a0741b6035e3dbc7d9ec6cee7ac9f (patch)
tree99fb325a397808e9d1fc1724754f81912234567d /crypto/compat/posix_win.c
parent73779a46bf49c4f53cc4b81993135a7408a01963 (diff)
downloadportable-2c5e2c60d92a0741b6035e3dbc7d9ec6cee7ac9f.tar.gz
portable-2c5e2c60d92a0741b6035e3dbc7d9ec6cee7ac9f.tar.bz2
portable-2c5e2c60d92a0741b6035e3dbc7d9ec6cee7ac9f.zip
Issue #1069 - Make file descriptors created through posix_open() distinguishable from sockets by having them take odd values only
Diffstat (limited to 'crypto/compat/posix_win.c')
-rw-r--r--crypto/compat/posix_win.c54
1 files changed, 12 insertions, 42 deletions
diff --git a/crypto/compat/posix_win.c b/crypto/compat/posix_win.c
index 1fbfce1..3e7d227 100644
--- a/crypto/compat/posix_win.c
+++ b/crypto/compat/posix_win.c
@@ -43,6 +43,16 @@ posix_fopen(const char *path, const char *mode)
43 return fopen(path, mode); 43 return fopen(path, mode);
44} 44}
45 45
46static int
47oddify_fd(int fd)
48{
49 if (fd & 1) /* also catches an eventual -1 from using up all descriptors */
50 return fd;
51 int clone = oddify_fd(dup(fd));
52 close(fd);
53 return clone;
54}
55
46int 56int
47posix_open(const char *path, ...) 57posix_open(const char *path, ...)
48{ 58{
@@ -62,7 +72,7 @@ posix_open(const char *path, ...)
62 flags |= O_NOINHERIT; 72 flags |= O_NOINHERIT;
63 } 73 }
64 flags &= ~O_NONBLOCK; 74 flags &= ~O_NONBLOCK;
65 return open(path, flags, mode); 75 return oddify_fd(open(path, flags, mode));
66} 76}
67 77
68char * 78char *
@@ -150,50 +160,10 @@ wsa_errno(int err)
150 return -1; 160 return -1;
151} 161}
152 162
153/*
154 * Employ a similar trick to cpython (pycore_fileutils.h) where the CRT report
155 * handler is disabled while checking if a descriptor is a socket or a file
156 */
157#if defined _MSC_VER && _MSC_VER >= 1900
158
159#include <crtdbg.h>
160#include <stdlib.h>
161
162static void noop_handler(const wchar_t *expression, const wchar_t *function,
163 const wchar_t *file, unsigned int line, uintptr_t pReserved)
164{
165 return;
166}
167
168#define BEGIN_SUPPRESS_IPH \
169 const int old_report_mode = _CrtSetReportMode(_CRT_ASSERT, 0); \
170 const _invalid_parameter_handler old_handler = _set_thread_local_invalid_parameter_handler(noop_handler)
171#define END_SUPPRESS_IPH \
172 (void)old_report_mode; /* Silence warning in release mode when _CrtSetReportMode compiles to void. */ \
173 _CrtSetReportMode(_CRT_ASSERT, old_report_mode); \
174 _set_thread_local_invalid_parameter_handler(old_handler)
175
176#else
177
178#define BEGIN_SUPPRESS_IPH
179#define END_SUPPRESS_IPH
180
181#endif
182
183static int 163static int
184is_socket(int fd) 164is_socket(int fd)
185{ 165{
186 intptr_t hd; 166 return (fd & 1) == 0; /* daringly assumes that any valid socket is even */
187
188 BEGIN_SUPPRESS_IPH;
189 hd = _get_osfhandle(fd);
190 END_SUPPRESS_IPH;
191
192 if (hd == (intptr_t)INVALID_HANDLE_VALUE) {
193 return 1; /* fd is not file descriptor */
194 }
195
196 return 0;
197} 167}
198 168
199int 169int