Clang static analysis builds should use C runtime directly. This is a little macro magic to use malloc() directly instead of SDL_malloc(), etc, so static analysis tests that know about the C runtime can function properly, and understand that we are dealing with heap allocations, etc. This changed our static analysis report from 5 outstanding bugs to 30. 5x as many bugs were hidden by SDL_malloc() not being recognized as malloc() by the static analyzer!
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 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
diff --git a/include/SDL_stdinc.h b/include/SDL_stdinc.h
index 853a440..7a061a9 100644
--- a/include/SDL_stdinc.h
+++ b/include/SDL_stdinc.h
@@ -379,11 +379,6 @@ SDL_FORCE_INLINE void SDL_memset4(void *dst, Uint32 val, size_t dwords)
extern DECLSPEC void *SDLCALL SDL_memcpy(SDL_OUT_BYTECAP(len) void *dst, SDL_IN_BYTECAP(len) const void *src, size_t len);
-SDL_FORCE_INLINE void *SDL_memcpy4(SDL_OUT_BYTECAP(dwords*4) void *dst, SDL_IN_BYTECAP(dwords*4) const void *src, size_t dwords)
-{
- return SDL_memcpy(dst, src, dwords * 4);
-}
-
extern DECLSPEC void *SDLCALL SDL_memmove(SDL_OUT_BYTECAP(len) void *dst, SDL_IN_BYTECAP(len) const void *src, size_t len);
extern DECLSPEC int SDLCALL SDL_memcmp(const void *s1, const void *s2, size_t len);
@@ -480,6 +475,39 @@ extern DECLSPEC char *SDLCALL SDL_iconv_string(const char *tocode,
#define SDL_iconv_utf8_ucs2(S) (Uint16 *)SDL_iconv_string("UCS-2-INTERNAL", "UTF-8", S, SDL_strlen(S)+1)
#define SDL_iconv_utf8_ucs4(S) (Uint32 *)SDL_iconv_string("UCS-4-INTERNAL", "UTF-8", S, SDL_strlen(S)+1)
+/* force builds using Clang's static analysis tools to use literal C runtime
+ here, since there are possibly tests that are ineffective otherwise. */
+#if defined(__clang_analyzer__) && !defined(SDL_DISABLE_ANALYZE_MACROS)
+#define SDL_malloc malloc
+#define SDL_calloc calloc
+#define SDL_realloc realloc
+#define SDL_free free
+#define SDL_memset memset
+#define SDL_memcpy memcpy
+#define SDL_memmove memmove
+#define SDL_memcmp memcmp
+#define SDL_strlen strlen
+#define SDL_strlcpy strlcpy
+#define SDL_strlcat strlcat
+#define SDL_strdup strdup
+#define SDL_strchr strchr
+#define SDL_strrchr strrchr
+#define SDL_strstr strstr
+#define SDL_strcmp strcmp
+#define SDL_strncmp strncmp
+#define SDL_strcasecmp strcasecmp
+#define SDL_strncasecmp strncasecmp
+#define SDL_sscanf sscanf
+#define SDL_vsscanf vsscanf
+#define SDL_snprintf snprintf
+#define SDL_vsnprintf vsnprintf
+#endif
+
+SDL_FORCE_INLINE void *SDL_memcpy4(SDL_OUT_BYTECAP(dwords*4) void *dst, SDL_IN_BYTECAP(dwords*4) const void *src, size_t dwords)
+{
+ return SDL_memcpy(dst, src, dwords * 4);
+}
+
/* Ends C function definitions when using C++ */
#ifdef __cplusplus
}
diff --git a/src/stdlib/SDL_getenv.c b/src/stdlib/SDL_getenv.c
index 782b8cc..a2905c4 100644
--- a/src/stdlib/SDL_getenv.c
+++ b/src/stdlib/SDL_getenv.c
@@ -18,6 +18,11 @@
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
+
+#if defined(__clang_analyzer__) && !defined(SDL_DISABLE_ANALYZE_MACROS)
+#define SDL_DISABLE_ANALYZE_MACROS 1
+#endif
+
#include "../SDL_internal.h"
#if defined(__WIN32__)
diff --git a/src/stdlib/SDL_iconv.c b/src/stdlib/SDL_iconv.c
index 5b9c202..fdb5e26 100644
--- a/src/stdlib/SDL_iconv.c
+++ b/src/stdlib/SDL_iconv.c
@@ -18,6 +18,11 @@
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
+
+#if defined(__clang_analyzer__) && !defined(SDL_DISABLE_ANALYZE_MACROS)
+#define SDL_DISABLE_ANALYZE_MACROS 1
+#endif
+
#include "../SDL_internal.h"
/* This file contains portable iconv functions for SDL */
diff --git a/src/stdlib/SDL_malloc.c b/src/stdlib/SDL_malloc.c
index 0245622..71f5f69 100644
--- a/src/stdlib/SDL_malloc.c
+++ b/src/stdlib/SDL_malloc.c
@@ -18,6 +18,11 @@
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
+
+#if defined(__clang_analyzer__) && !defined(SDL_DISABLE_ANALYZE_MACROS)
+#define SDL_DISABLE_ANALYZE_MACROS 1
+#endif
+
#include "../SDL_internal.h"
/* This file contains portable memory management functions for SDL */
diff --git a/src/stdlib/SDL_qsort.c b/src/stdlib/SDL_qsort.c
index 8329a35..0d19784 100644
--- a/src/stdlib/SDL_qsort.c
+++ b/src/stdlib/SDL_qsort.c
@@ -41,6 +41,11 @@
*
* Gareth McCaughan Peterhouse Cambridge 1998
*/
+
+#if defined(__clang_analyzer__) && !defined(SDL_DISABLE_ANALYZE_MACROS)
+#define SDL_DISABLE_ANALYZE_MACROS 1
+#endif
+
#include "../SDL_internal.h"
/*
diff --git a/src/stdlib/SDL_stdlib.c b/src/stdlib/SDL_stdlib.c
index b1de63d..c2c1421 100644
--- a/src/stdlib/SDL_stdlib.c
+++ b/src/stdlib/SDL_stdlib.c
@@ -18,6 +18,11 @@
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
+
+#if defined(__clang_analyzer__) && !defined(SDL_DISABLE_ANALYZE_MACROS)
+#define SDL_DISABLE_ANALYZE_MACROS 1
+#endif
+
#include "../SDL_internal.h"
/* This file contains portable stdlib functions for SDL */
diff --git a/src/stdlib/SDL_string.c b/src/stdlib/SDL_string.c
index 8428586..ce8b2fd 100644
--- a/src/stdlib/SDL_string.c
+++ b/src/stdlib/SDL_string.c
@@ -18,6 +18,11 @@
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
+
+#if defined(__clang_analyzer__) && !defined(SDL_DISABLE_ANALYZE_MACROS)
+#define SDL_DISABLE_ANALYZE_MACROS 1
+#endif
+
#include "../SDL_internal.h"
/* This file contains portable string manipulation functions for SDL */