Vita: restore sceClibMemcmp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 9cfac1d..d0f5e6f 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 a789f43..e454cf0 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;