This looks dumb as fuck, but it tells static analysis tools that our memory allocation functions literally never return a null pointer

git-svn-id: https://svn.eduke32.com/eduke32@7095 1a8010ca-5511-0410-912e-c29ae57300e0
This commit is contained in:
terminx 2018-10-25 23:30:22 +00:00
parent b65120200a
commit 175ac6dc6b
2 changed files with 9 additions and 14 deletions

View file

@ -1174,27 +1174,24 @@ uint32_t Bgetsysmemsize(void);
extern void xalloc_set_location(int32_t line, const char *file, const char *func); extern void xalloc_set_location(int32_t line, const char *file, const char *func);
#endif #endif
void set_memerr_handler(void (*handlerfunc)(int32_t, const char *, const char *)); void set_memerr_handler(void (*handlerfunc)(int32_t, const char *, const char *));
void handle_memerr(void); void *handle_memerr(void *);
static FORCE_INLINE char *xstrdup(const char *s) static FORCE_INLINE char *xstrdup(const char *s)
{ {
char *ptr = Bstrdup(s); char *ptr = Bstrdup(s);
if (ptr == NULL) handle_memerr(); return (ptr == NULL) ? (char *)handle_memerr(ptr) : ptr;
return ptr;
} }
static FORCE_INLINE void *xmalloc(const bsize_t size) static FORCE_INLINE void *xmalloc(const bsize_t size)
{ {
void *ptr = Bmalloc(size); void *ptr = Bmalloc(size);
if (ptr == NULL) handle_memerr(); return (ptr == NULL) ? handle_memerr(ptr) : ptr;
return ptr;
} }
static FORCE_INLINE void *xcalloc(const bsize_t nmemb, const bsize_t size) static FORCE_INLINE void *xcalloc(const bsize_t nmemb, const bsize_t size)
{ {
void *ptr = Bcalloc(nmemb, size); void *ptr = Bcalloc(nmemb, size);
if (ptr == NULL) handle_memerr(); return (ptr == NULL) ? handle_memerr(ptr) : ptr;
return ptr;
} }
static FORCE_INLINE void *xrealloc(void * const ptr, const bsize_t size) static FORCE_INLINE void *xrealloc(void * const ptr, const bsize_t size)
@ -1205,18 +1202,14 @@ static FORCE_INLINE void *xrealloc(void * const ptr, const bsize_t size)
// - ptr == NULL makes realloc() behave like malloc() // - ptr == NULL makes realloc() behave like malloc()
// - size == 0 make it behave like free() if ptr != NULL // - size == 0 make it behave like free() if ptr != NULL
// Since we want to catch an out-of-mem in the first case, this leaves: // Since we want to catch an out-of-mem in the first case, this leaves:
if (newptr == NULL && size != 0) return (newptr == NULL && size != 0) ? handle_memerr(ptr) : newptr;
handle_memerr();
return newptr;
} }
#if !defined NO_ALIGNED_MALLOC #if !defined NO_ALIGNED_MALLOC
static FORCE_INLINE void *xaligned_alloc(const bsize_t alignment, const bsize_t size) static FORCE_INLINE void *xaligned_alloc(const bsize_t alignment, const bsize_t size)
{ {
void *ptr = Baligned_alloc(alignment, size); void *ptr = Baligned_alloc(alignment, size);
if (ptr == NULL) handle_memerr(); return (ptr == NULL) ? handle_memerr(ptr) : ptr;
return ptr;
} }
#else #else
# define xaligned_alloc(alignment, size) xmalloc(size) # define xaligned_alloc(alignment, size) xmalloc(size)

View file

@ -48,8 +48,9 @@ void xalloc_set_location(int32_t line, const char *file, const char *func)
} }
#endif #endif
void handle_memerr(void) void *handle_memerr(void *p)
{ {
UNREFERENCED_PARAMETER(p);
debug_break(); debug_break();
if (g_MemErrHandler) if (g_MemErrHandler)
@ -62,6 +63,7 @@ void handle_memerr(void)
} }
Bexit(EXIT_FAILURE); Bexit(EXIT_FAILURE);
EDUKE32_UNREACHABLE_SECTION(return &handle_memerr);
} }
void set_memerr_handler(void(*handlerfunc)(int32_t, const char *, const char *)) void set_memerr_handler(void(*handlerfunc)(int32_t, const char *, const char *))