summary refs log tree commit diff
path: root/compat
diff options
context:
space:
mode:
authorJune McEnroe <june@causal.agency>2026-06-01 14:45:02 -0400
committerJune McEnroe <june@causal.agency>2026-06-01 14:45:02 -0400
commitf39835641d4413ef327857f0dc4a24d5b9599861 (patch)
treedeec6e5701019178078d700fcb187e97c6932c43 /compat
parentImport LibreSSL 4.0.1 (diff)
downloadlibretls-f39835641d4413ef327857f0dc4a24d5b9599861.tar.gz
libretls-f39835641d4413ef327857f0dc4a24d5b9599861.zip
Import LibreSSL 4.1.0
Diffstat (limited to 'compat')
-rw-r--r--compat/posix_win.c85
1 files changed, 33 insertions, 52 deletions
diff --git a/compat/posix_win.c b/compat/posix_win.c
index 1fbfce1..572e527 100644
--- a/compat/posix_win.c
+++ b/compat/posix_win.c
@@ -22,6 +22,25 @@
 #include <string.h>
 #include <unistd.h>
 
+#include <sys/stat.h>
+
+static int
+is_socket(int fd)
+{
+	// Border case: Don't break std* file descriptors
+	if (fd < 3)
+		return 0;
+
+	// All locally-allocated file descriptors will have the high bit set
+	return (fd & 0x80000000) == 0;
+}
+
+static int
+get_real_fd(int fd)
+{
+	return (fd & 0x7fffffff);
+}
+
 void
 posix_perror(const char *s)
 {
@@ -44,6 +63,12 @@ posix_fopen(const char *path, const char *mode)
 }
 
 int
+libressl_fstat(int fd, struct stat *statbuf)
+{
+	return fstat(get_real_fd(fd), statbuf);
+}
+
+int
 posix_open(const char *path, ...)
 {
 	va_list ap;
@@ -62,7 +87,11 @@ posix_open(const char *path, ...)
 		flags |= O_NOINHERIT;
 	}
 	flags &= ~O_NONBLOCK;
-	return open(path, flags, mode);
+
+	const int fh = open(path, flags, mode);
+
+	// Set high bit to mark file descriptor as a file handle
+	return fh + 0x80000000;
 }
 
 char *
@@ -150,52 +179,6 @@ wsa_errno(int err)
 	return -1;
 }
 
-/*
- * Employ a similar trick to cpython (pycore_fileutils.h) where the CRT report
- * handler is disabled while checking if a descriptor is a socket or a file
- */
-#if defined _MSC_VER && _MSC_VER >= 1900
-
-#include <crtdbg.h>
-#include <stdlib.h>
-
-static void noop_handler(const wchar_t *expression,	const wchar_t *function,
-    const wchar_t *file, unsigned int line, uintptr_t pReserved)
-{
-	return;
-}
-
-#define BEGIN_SUPPRESS_IPH \
-	const int old_report_mode = _CrtSetReportMode(_CRT_ASSERT, 0); \
-	const _invalid_parameter_handler old_handler = _set_thread_local_invalid_parameter_handler(noop_handler)
-#define END_SUPPRESS_IPH \
-	(void)old_report_mode; /* Silence warning in release mode when _CrtSetReportMode compiles to void. */ \
-	_CrtSetReportMode(_CRT_ASSERT, old_report_mode); \
-	_set_thread_local_invalid_parameter_handler(old_handler)
-
-#else
-
-#define BEGIN_SUPPRESS_IPH
-#define END_SUPPRESS_IPH
-
-#endif
-
-static int
-is_socket(int fd)
-{
-	intptr_t hd;
-
-	BEGIN_SUPPRESS_IPH;
-	hd = _get_osfhandle(fd);
-	END_SUPPRESS_IPH;
-
-	if (hd == (intptr_t)INVALID_HANDLE_VALUE) {
-		return 1; /* fd is not file descriptor */
-	}
-
-	return 0;
-}
-
 int
 posix_connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen)
 {
@@ -209,14 +192,13 @@ int
 posix_close(int fd)
 {
 	int rc;
-
 	if (is_socket(fd)) {
 		if ((rc = closesocket(fd)) == SOCKET_ERROR) {
 			int err = WSAGetLastError();
 			rc = wsa_errno(err);
 		}
 	} else {
-		rc = close(fd);
+		rc = close(get_real_fd(fd));
 	}
 	return rc;
 }
@@ -225,14 +207,13 @@ ssize_t
 posix_read(int fd, void *buf, size_t count)
 {
 	ssize_t rc;
-
 	if (is_socket(fd)) {
 		if ((rc = recv(fd, buf, count, 0)) == SOCKET_ERROR) {
 			int err = WSAGetLastError();
 			rc = wsa_errno(err);
 		}
 	} else {
-		rc = read(fd, buf, count);
+		rc = read(get_real_fd(fd), buf, count);
 	}
 	return rc;
 }
@@ -246,7 +227,7 @@ posix_write(int fd, const void *buf, size_t count)
 			rc = wsa_errno(WSAGetLastError());
 		}
 	} else {
-		rc = write(fd, buf, count);
+		rc = write(get_real_fd(fd), buf, count);
 	}
 	return rc;
 }