From 7115ceb7756b4707781acea03fcf366b4f0e21f6 Mon Sep 17 00:00:00 2001
From: Ivan Epifanov <[EMAIL REDACTED]>
Date: Sat, 17 Sep 2022 11:52:19 +0300
Subject: [PATCH] Vita: restore sceClibMemcmp
---
CMakeLists.txt | 1 +
src/stdlib/SDL_string.c | 17 ++++++++++++++++-
2 files changed, 17 insertions(+), 1 deletion(-)
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 9cfac1de319..d0f5e6fa5f7 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -2495,6 +2495,7 @@ elseif(VITA)
target_compile_definitions(sdl-build-options INTERFACE "-Dmemcpy=sceClibMemcpy")
target_compile_definitions(sdl-build-options INTERFACE "-Dmemset=sceClibMemset")
target_compile_definitions(sdl-build-options INTERFACE "-Dmemmove=sceClibMemmove")
+ target_compile_definitions(sdl-build-options INTERFACE "-Dmemcmp=sceClibMemcmp")
# CheckPTHREAD()
diff --git a/src/stdlib/SDL_string.c b/src/stdlib/SDL_string.c
index a789f43195e..e454cf04fae 100644
--- a/src/stdlib/SDL_string.c
+++ b/src/stdlib/SDL_string.c
@@ -29,6 +29,10 @@
#include "SDL_stdinc.h"
#include "SDL_vacopy.h"
+#if defined(__vita__)
+#include <psp2/kernel/clib.h>
+#endif
+
#if !defined(HAVE_VSSCANF) || !defined(HAVE_STRTOL) || !defined(HAVE_STRTOUL) || !defined(HAVE_STRTOD) || !defined(HAVE_STRTOLL) || !defined(HAVE_STRTOULL)
#define SDL_isupperhex(X) (((X) >= 'A') && ((X) <= 'F'))
#define SDL_islowerhex(X) (((X) >= 'a') && ((X) <= 'f'))
@@ -306,7 +310,18 @@ SDL_memmove(SDL_OUT_BYTECAP(len) void *dst, SDL_IN_BYTECAP(len) const void *src,
int
SDL_memcmp(const void *s1, const void *s2, size_t len)
{
-#if defined(HAVE_MEMCMP)
+#if defined(__vita__)
+ /*
+ Using memcmp on NULL is UB per POSIX / C99 7.21.1/2.
+ But, both linux and bsd allow that, with an exception:
+ zero length strings are always identical, so NULLs are never dereferenced.
+ sceClibMemcmp on PSVita doesn't allow that, so we check ourselves.
+ */
+ if (len == 0) {
+ return 0;
+ }
+ return sceClibMemcmp(s1, s2, len);
+#elif defined(HAVE_MEMCMP)
return memcmp(s1, s2, len);
#else
char *s1p = (char *) s1;