diff options
| -rw-r--r-- | crypto/compat/posix_win.c | 54 |
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 | ||
| 46 | static int | ||
| 47 | oddify_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 | |||
| 46 | int | 56 | int |
| 47 | posix_open(const char *path, ...) | 57 | posix_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 | ||
| 68 | char * | 78 | char * |
| @@ -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 | |||
| 162 | static 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 | |||
| 183 | static int | 163 | static int |
| 184 | is_socket(int fd) | 164 | is_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 | ||
| 199 | int | 169 | int |
