From 3e1fa90d301428ace65d5c8b371e93d2c59c3d65 Mon Sep 17 00:00:00 2001
From: Simon McVittie <[EMAIL REDACTED]>
Date: Mon, 15 Jun 2026 12:17:45 +0100
Subject: [PATCH] Use Standard C strlen(), etc., if we're using libc anyway
On some platforms (notably Windows) we don't want to use the system C
library, so we avoid using Standard C strlen() and use our own
implementation. On other platforms (notably Linux) we require the system
C library anyway, for functions like setenv(), so we might as well make
full use of it.
In particular this means that our fallback implementations only need
to work on Windows, so it's now OK if the workarounds we use to avoid the
compiler replacing our open-coded strlen() with a call to the library
strlen() (see #340) are not 100% portable.
Signed-off-by: Simon McVittie <smcv@collabora.com>
---
src/sdl2_compat.c | 13 +++++++++++++
1 file changed, 13 insertions(+)
diff --git a/src/sdl2_compat.c b/src/sdl2_compat.c
index e80837af..d29e8f6d 100644
--- a/src/sdl2_compat.c
+++ b/src/sdl2_compat.c
@@ -272,6 +272,7 @@ SDL2COMPAT_itoa(char *dst, int val)
/* you can use SDL3_strlen once we're past startup. */
static size_t SDL2Compat_strlen(const char *str)
{
+#ifdef SDL_PLATFORM_WINDOWS
/* volatile prevents gcc from optimizing this into a call to the
* strlen() library function, which we are intentionally avoiding on
* Windows: see #340 */
@@ -280,11 +281,16 @@ static size_t SDL2Compat_strlen(const char *str)
++ptr;
}
return (size_t)(ptr - str);
+#else
+ /* On other platforms we rely on libc */
+ return strlen (str);
+#endif
}
/* you can use SDL3_strcmp once we're past startup. */
static bool SDL2Compat_strequal(const char *a, const char *b)
{
+#ifdef SDL_PLATFORM_WINDOWS
while (true) {
const char cha = *a;
if (cha != *b) {
@@ -296,11 +302,15 @@ static bool SDL2Compat_strequal(const char *a, const char *b)
b++;
}
return true;
+#else
+ return (strcmp (a, b) == 0);
+#endif
}
/* you can use SDL3_strrchr once we're past startup. */
static char *SDL2Compat_strrchr(const char *string, int c)
{
+#ifdef SDL_PLATFORM_WINDOWS
const char *bufp = string + SDL2Compat_strlen(string);
while (bufp >= string) {
if (*bufp == c) {
@@ -309,6 +319,9 @@ static char *SDL2Compat_strrchr(const char *string, int c)
--bufp;
}
return NULL;
+#else
+ return (char *) strrchr (string, c);
+#endif
}
/* log a string using platform-specific code for before SDL3 is fully available. */