From 168461a3a907941ab73bcd54652516299b32a6e8 Mon Sep 17 00:00:00 2001 From: Gilles Peskine Date: Fri, 16 Jan 2026 18:55:24 +0100 Subject: [PATCH] Unify the detection of Unix-like platforms We were using slightly different guards to decide whether to include `` in different places. Unify those. Signed-off-by: Gilles Peskine --- ChangeLog.d/unistd.txt | 3 +++ library/common.h | 15 +++++++++++++++ library/net_sockets.c | 4 +--- library/platform_util.c | 15 +++++---------- library/threading.c | 4 +--- library/timing.c | 4 +--- 6 files changed, 26 insertions(+), 19 deletions(-) create mode 100644 ChangeLog.d/unistd.txt diff --git a/ChangeLog.d/unistd.txt b/ChangeLog.d/unistd.txt new file mode 100644 index 0000000000..d2e4d4301a --- /dev/null +++ b/ChangeLog.d/unistd.txt @@ -0,0 +1,3 @@ +Changes + * Tweak the detection of Unix-like platforms, which makes more system + interfaces (timing, threading) available on Haiku, QNX and Midipix. diff --git a/library/common.h b/library/common.h index c5cd241e7a..4aab94ba21 100644 --- a/library/common.h +++ b/library/common.h @@ -27,6 +27,21 @@ #define MBEDTLS_HAVE_NEON_INTRINSICS #endif +/* Decide whether we're built for a Unix-like platform. + */ +#if defined(_WIN32) +/* If Windows platform interfaces are available, we use them, even if + * a Unix-like might also to be available. */ +/* defined(_WIN32) ==> we can include */ +#elif defined(unix) || defined(__unix) || defined(__unix__) || \ + (defined(__APPLE__) && defined(__MACH__)) || \ + defined(__HAIKU__) || \ + defined(__midipix__) || \ + /* Add other Unix-like platform indicators here ^^^^ */ 0 +/* defined(MBEDTLS_PLATFORM_IS_UNIXLIKE) ==> we can include */ +#define MBEDTLS_PLATFORM_IS_UNIXLIKE +#endif + /** Helper to define a function as static except when building invasive tests. * * If a function is only used inside its own source file and should be diff --git a/library/net_sockets.c b/library/net_sockets.c index 1419c787b9..c919f6f574 100644 --- a/library/net_sockets.c +++ b/library/net_sockets.c @@ -19,9 +19,7 @@ #if defined(MBEDTLS_NET_C) -#if !defined(unix) && !defined(__unix__) && !defined(__unix) && \ - !defined(__APPLE__) && !defined(_WIN32) && !defined(__QNXNTO__) && \ - !defined(__HAIKU__) && !defined(__midipix__) +#if !defined(MBEDTLS_PLATFORM_IS_UNIXLIKE) && !defined(_WIN32) #error "This module only works on Unix and Windows, see MBEDTLS_NET_C in mbedtls_config.h" #endif diff --git a/library/platform_util.c b/library/platform_util.c index 19ef07aead..1cab42ffc0 100644 --- a/library/platform_util.c +++ b/library/platform_util.c @@ -147,12 +147,9 @@ void mbedtls_zeroize_and_free(void *buf, size_t len) #if defined(MBEDTLS_HAVE_TIME_DATE) && !defined(MBEDTLS_PLATFORM_GMTIME_R_ALT) #include -#if !defined(_WIN32) && (defined(unix) || \ - defined(__unix) || defined(__unix__) || (defined(__APPLE__) && \ - defined(__MACH__)) || defined(__midipix__)) +#if defined(MBEDTLS_PLATFORM_IS_UNIXLIKE) #include -#endif /* !_WIN32 && (unix || __unix || __unix__ || - * (__APPLE__ && __MACH__) || __midipix__) */ +#endif #if !((defined(_POSIX_VERSION) && _POSIX_VERSION >= 200809L) || \ (defined(_POSIX_THREAD_SAFE_FUNCTIONS) && \ @@ -218,12 +215,10 @@ void (*mbedtls_test_hook_test_fail)(const char *, int, const char *); #if defined(MBEDTLS_HAVE_TIME) && !defined(MBEDTLS_PLATFORM_MS_TIME_ALT) #include -#if !defined(_WIN32) && \ - (defined(unix) || defined(__unix) || defined(__unix__) || \ - (defined(__APPLE__) && defined(__MACH__)) || defined(__HAIKU__) || defined(__midipix__)) +#if defined(MBEDTLS_PLATFORM_IS_UNIXLIKE) #include -#endif \ - /* !_WIN32 && (unix || __unix || __unix__ || (__APPLE__ && __MACH__) || __HAIKU__ || __midipix__) */ +#endif + #if (defined(_POSIX_VERSION) && _POSIX_VERSION >= 199309L) || defined(__HAIKU__) mbedtls_ms_time_t mbedtls_ms_time(void) { diff --git a/library/threading.c b/library/threading.c index ff8183ed15..8764b3edce 100644 --- a/library/threading.c +++ b/library/threading.c @@ -21,9 +21,7 @@ #if defined(MBEDTLS_HAVE_TIME_DATE) && !defined(MBEDTLS_PLATFORM_GMTIME_R_ALT) -#if !defined(_WIN32) && (defined(unix) || \ - defined(__unix) || defined(__unix__) || (defined(__APPLE__) && \ - defined(__MACH__))) +#if defined(MBEDTLS_PLATFORM_IS_UNIXLIKE) #include #endif /* !_WIN32 && (unix || __unix || __unix__ || * (__APPLE__ && __MACH__)) */ diff --git a/library/timing.c b/library/timing.c index 58f1c1ec2e..e3e9da52ef 100644 --- a/library/timing.c +++ b/library/timing.c @@ -13,9 +13,7 @@ #if !defined(MBEDTLS_TIMING_ALT) -#if !defined(unix) && !defined(__unix__) && !defined(__unix) && \ - !defined(__APPLE__) && !defined(_WIN32) && !defined(__QNXNTO__) && \ - !defined(__HAIKU__) && !defined(__midipix__) +#if !defined(_WIN32) && !defined(MBEDTLS_PLATFORM_IS_UNIXLIKE) #error "This module only works on Unix and Windows, see MBEDTLS_TIMING_C in mbedtls_config.h" #endif