SDL: SDL_memcpy.c, SDL_memmove.c, SDL_memset.c: don't use gcc builtins if !HAVE_LIBC

From cb3a1a82d530797402eef65a5cecac33ec50c63a Mon Sep 17 00:00:00 2001
From: Ozkan Sezer <[EMAIL REDACTED]>
Date: Tue, 20 Feb 2024 01:23:18 +0300
Subject: [PATCH] SDL_memcpy.c, SDL_memmove.c, SDL_memset.c: don't use gcc
 builtins if !HAVE_LIBC

__builtin_memcpy, as well as __builtin_memset and __builtin_memmove, needn't be
inlined but emitted as a libc call, leading to infinitely recursive calls.

Fixes https://github.com/libsdl-org/SDL/issues/9090
---
 src/stdlib/SDL_memcpy.c  | 4 ++--
 src/stdlib/SDL_memmove.c | 2 +-
 src/stdlib/SDL_memset.c  | 2 +-
 3 files changed, 4 insertions(+), 4 deletions(-)

diff --git a/src/stdlib/SDL_memcpy.c b/src/stdlib/SDL_memcpy.c
index d8cc81bdcfa3..36a2bceaaaab 100644
--- a/src/stdlib/SDL_memcpy.c
+++ b/src/stdlib/SDL_memcpy.c
@@ -29,7 +29,7 @@
 #endif
 void *SDL_memcpy(SDL_OUT_BYTECAP(len) void *dst, SDL_IN_BYTECAP(len) const void *src, size_t len)
 {
-#ifdef __GNUC__
+#if defined(__GNUC__) && (defined(HAVE_LIBC) && HAVE_LIBC)
     /* Presumably this is well tuned for speed.
        On my machine this is twice as fast as the C code below.
      */
@@ -76,7 +76,7 @@ void *SDL_memcpy(SDL_OUT_BYTECAP(len) void *dst, SDL_IN_BYTECAP(len) const void
         }
     }
     return dst;
-#endif /* __GNUC__ */
+#endif /* HAVE_MEMCPY */
 }
 
 /* The optimizer on Visual Studio 2005 and later generates memcpy() and memset() calls.
diff --git a/src/stdlib/SDL_memmove.c b/src/stdlib/SDL_memmove.c
index 675869105868..38a75e0f2aef 100644
--- a/src/stdlib/SDL_memmove.c
+++ b/src/stdlib/SDL_memmove.c
@@ -29,7 +29,7 @@
 #endif
 void *SDL_memmove(SDL_OUT_BYTECAP(len) void *dst, SDL_IN_BYTECAP(len) const void *src, size_t len)
 {
-#ifdef __GNUC__
+#if defined(__GNUC__) && (defined(HAVE_LIBC) && HAVE_LIBC)
     /* Presumably this is well tuned for speed. */
     return __builtin_memmove(dst, src, len);
 #elif defined(HAVE_MEMMOVE)
diff --git a/src/stdlib/SDL_memset.c b/src/stdlib/SDL_memset.c
index 61ad5e8347df..90ecbeedd387 100644
--- a/src/stdlib/SDL_memset.c
+++ b/src/stdlib/SDL_memset.c
@@ -29,7 +29,7 @@
 #endif
 void *SDL_memset(SDL_OUT_BYTECAP(len) void *dst, int c, size_t len)
 {
-#ifdef __GNUC__
+#if defined(__GNUC__) && (defined(HAVE_LIBC) && HAVE_LIBC)
     return __builtin_memset(dst, c, len);
 #elif defined(HAVE_MEMSET)
     return memset(dst, c, len);