Clang static analysis builds should use C runtime directly.
authorRyan C. Gordon <icculus@icculus.org>
Mon, 05 Jan 2015 01:41:42 -0500
changeset 9306817656bd36ec
parent 9305 0c66e0df64f6
child 9307 47a5c5dae9b6
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!
include/SDL_stdinc.h
src/stdlib/SDL_getenv.c
src/stdlib/SDL_iconv.c
src/stdlib/SDL_malloc.c
src/stdlib/SDL_qsort.c
src/stdlib/SDL_stdlib.c
src/stdlib/SDL_string.c
     1.1 --- a/include/SDL_stdinc.h	Mon Jan 05 01:07:36 2015 -0500
     1.2 +++ b/include/SDL_stdinc.h	Mon Jan 05 01:41:42 2015 -0500
     1.3 @@ -379,11 +379,6 @@
     1.4  
     1.5  extern DECLSPEC void *SDLCALL SDL_memcpy(SDL_OUT_BYTECAP(len) void *dst, SDL_IN_BYTECAP(len) const void *src, size_t len);
     1.6  
     1.7 -SDL_FORCE_INLINE void *SDL_memcpy4(SDL_OUT_BYTECAP(dwords*4) void *dst, SDL_IN_BYTECAP(dwords*4) const void *src, size_t dwords)
     1.8 -{
     1.9 -    return SDL_memcpy(dst, src, dwords * 4);
    1.10 -}
    1.11 -
    1.12  extern DECLSPEC void *SDLCALL SDL_memmove(SDL_OUT_BYTECAP(len) void *dst, SDL_IN_BYTECAP(len) const void *src, size_t len);
    1.13  extern DECLSPEC int SDLCALL SDL_memcmp(const void *s1, const void *s2, size_t len);
    1.14  
    1.15 @@ -480,6 +475,39 @@
    1.16  #define SDL_iconv_utf8_ucs2(S)      (Uint16 *)SDL_iconv_string("UCS-2-INTERNAL", "UTF-8", S, SDL_strlen(S)+1)
    1.17  #define SDL_iconv_utf8_ucs4(S)      (Uint32 *)SDL_iconv_string("UCS-4-INTERNAL", "UTF-8", S, SDL_strlen(S)+1)
    1.18  
    1.19 +/* force builds using Clang's static analysis tools to use literal C runtime
    1.20 +   here, since there are possibly tests that are ineffective otherwise. */
    1.21 +#if defined(__clang_analyzer__) && !defined(SDL_DISABLE_ANALYZE_MACROS)
    1.22 +#define SDL_malloc malloc
    1.23 +#define SDL_calloc calloc
    1.24 +#define SDL_realloc realloc
    1.25 +#define SDL_free free
    1.26 +#define SDL_memset memset
    1.27 +#define SDL_memcpy memcpy
    1.28 +#define SDL_memmove memmove
    1.29 +#define SDL_memcmp memcmp
    1.30 +#define SDL_strlen strlen
    1.31 +#define SDL_strlcpy strlcpy
    1.32 +#define SDL_strlcat strlcat
    1.33 +#define SDL_strdup strdup
    1.34 +#define SDL_strchr strchr
    1.35 +#define SDL_strrchr strrchr
    1.36 +#define SDL_strstr strstr
    1.37 +#define SDL_strcmp strcmp
    1.38 +#define SDL_strncmp strncmp
    1.39 +#define SDL_strcasecmp strcasecmp
    1.40 +#define SDL_strncasecmp strncasecmp
    1.41 +#define SDL_sscanf sscanf
    1.42 +#define SDL_vsscanf vsscanf
    1.43 +#define SDL_snprintf snprintf
    1.44 +#define SDL_vsnprintf vsnprintf
    1.45 +#endif
    1.46 +
    1.47 +SDL_FORCE_INLINE void *SDL_memcpy4(SDL_OUT_BYTECAP(dwords*4) void *dst, SDL_IN_BYTECAP(dwords*4) const void *src, size_t dwords)
    1.48 +{
    1.49 +    return SDL_memcpy(dst, src, dwords * 4);
    1.50 +}
    1.51 +
    1.52  /* Ends C function definitions when using C++ */
    1.53  #ifdef __cplusplus
    1.54  }
     2.1 --- a/src/stdlib/SDL_getenv.c	Mon Jan 05 01:07:36 2015 -0500
     2.2 +++ b/src/stdlib/SDL_getenv.c	Mon Jan 05 01:41:42 2015 -0500
     2.3 @@ -18,6 +18,11 @@
     2.4       misrepresented as being the original software.
     2.5    3. This notice may not be removed or altered from any source distribution.
     2.6  */
     2.7 +
     2.8 +#if defined(__clang_analyzer__) && !defined(SDL_DISABLE_ANALYZE_MACROS)
     2.9 +#define SDL_DISABLE_ANALYZE_MACROS 1
    2.10 +#endif
    2.11 +
    2.12  #include "../SDL_internal.h"
    2.13  
    2.14  #if defined(__WIN32__)
     3.1 --- a/src/stdlib/SDL_iconv.c	Mon Jan 05 01:07:36 2015 -0500
     3.2 +++ b/src/stdlib/SDL_iconv.c	Mon Jan 05 01:41:42 2015 -0500
     3.3 @@ -18,6 +18,11 @@
     3.4       misrepresented as being the original software.
     3.5    3. This notice may not be removed or altered from any source distribution.
     3.6  */
     3.7 +
     3.8 +#if defined(__clang_analyzer__) && !defined(SDL_DISABLE_ANALYZE_MACROS)
     3.9 +#define SDL_DISABLE_ANALYZE_MACROS 1
    3.10 +#endif
    3.11 +
    3.12  #include "../SDL_internal.h"
    3.13  
    3.14  /* This file contains portable iconv functions for SDL */
     4.1 --- a/src/stdlib/SDL_malloc.c	Mon Jan 05 01:07:36 2015 -0500
     4.2 +++ b/src/stdlib/SDL_malloc.c	Mon Jan 05 01:41:42 2015 -0500
     4.3 @@ -18,6 +18,11 @@
     4.4       misrepresented as being the original software.
     4.5    3. This notice may not be removed or altered from any source distribution.
     4.6  */
     4.7 +
     4.8 +#if defined(__clang_analyzer__) && !defined(SDL_DISABLE_ANALYZE_MACROS)
     4.9 +#define SDL_DISABLE_ANALYZE_MACROS 1
    4.10 +#endif
    4.11 +
    4.12  #include "../SDL_internal.h"
    4.13  
    4.14  /* This file contains portable memory management functions for SDL */
     5.1 --- a/src/stdlib/SDL_qsort.c	Mon Jan 05 01:07:36 2015 -0500
     5.2 +++ b/src/stdlib/SDL_qsort.c	Mon Jan 05 01:41:42 2015 -0500
     5.3 @@ -41,6 +41,11 @@
     5.4   *
     5.5   * Gareth McCaughan   Peterhouse   Cambridge   1998
     5.6   */
     5.7 +
     5.8 +#if defined(__clang_analyzer__) && !defined(SDL_DISABLE_ANALYZE_MACROS)
     5.9 +#define SDL_DISABLE_ANALYZE_MACROS 1
    5.10 +#endif
    5.11 +
    5.12  #include "../SDL_internal.h"
    5.13  
    5.14  /*
     6.1 --- a/src/stdlib/SDL_stdlib.c	Mon Jan 05 01:07:36 2015 -0500
     6.2 +++ b/src/stdlib/SDL_stdlib.c	Mon Jan 05 01:41:42 2015 -0500
     6.3 @@ -18,6 +18,11 @@
     6.4       misrepresented as being the original software.
     6.5    3. This notice may not be removed or altered from any source distribution.
     6.6  */
     6.7 +
     6.8 +#if defined(__clang_analyzer__) && !defined(SDL_DISABLE_ANALYZE_MACROS)
     6.9 +#define SDL_DISABLE_ANALYZE_MACROS 1
    6.10 +#endif
    6.11 +
    6.12  #include "../SDL_internal.h"
    6.13  
    6.14  /* This file contains portable stdlib functions for SDL */
     7.1 --- a/src/stdlib/SDL_string.c	Mon Jan 05 01:07:36 2015 -0500
     7.2 +++ b/src/stdlib/SDL_string.c	Mon Jan 05 01:41:42 2015 -0500
     7.3 @@ -18,6 +18,11 @@
     7.4       misrepresented as being the original software.
     7.5    3. This notice may not be removed or altered from any source distribution.
     7.6  */
     7.7 +
     7.8 +#if defined(__clang_analyzer__) && !defined(SDL_DISABLE_ANALYZE_MACROS)
     7.9 +#define SDL_DISABLE_ANALYZE_MACROS 1
    7.10 +#endif
    7.11 +
    7.12  #include "../SDL_internal.h"
    7.13  
    7.14  /* This file contains portable string manipulation functions for SDL */