From 683c4874f88e2726518a548ce4ed72311d456af3 Mon Sep 17 00:00:00 2001 From: Alibek Omarov Date: Sat, 9 Dec 2023 19:00:25 +0300 Subject: [PATCH] common: add ALLOC_CHECK macro that enables attribute alloc_size on GCC that allows to prevent suspicious allocation sizes --- common/render_api.h | 2 +- common/xash3d_types.h | 3 +++ engine/common/common.h | 4 ++-- engine/menu_int.h | 2 +- engine/physint.h | 2 +- engine/ref_api.h | 4 ++-- filesystem/filesystem.h | 4 ++-- 7 files changed, 12 insertions(+), 9 deletions(-) diff --git a/common/render_api.h b/common/render_api.h index 25897a85..40129c1d 100644 --- a/common/render_api.h +++ b/common/render_api.h @@ -229,7 +229,7 @@ typedef struct render_api_s void (*R_Reserved0)( void ); // static allocations - void *(*pfnMemAlloc)( size_t cb, const char *filename, const int fileline ); + void *(*pfnMemAlloc)( size_t cb, const char *filename, const int fileline ) ALLOC_CHECK( 1 ); void (*pfnMemFree)( void *mem, const char *filename, const int fileline ); // engine utils (not related with render API but placed here) diff --git a/common/xash3d_types.h b/common/xash3d_types.h index ad46778e..1c45b8ee 100644 --- a/common/xash3d_types.h +++ b/common/xash3d_types.h @@ -88,18 +88,21 @@ typedef uint64_t longtime_t; #define _format(x) __attribute__((format(printf, x, x+1))) #define NORETURN __attribute__((noreturn)) #define NONNULL __attribute__((nonnull)) + #define ALLOC_CHECK(x) __attribute__((alloc_size(x))) #elif defined(_MSC_VER) #define EXPORT __declspec( dllexport ) #define GAME_EXPORT #define _format(x) #define NORETURN #define NONNULL + #define ALLOC_CHECK(x) #else #define EXPORT #define GAME_EXPORT #define _format(x) #define NORETURN #define NONNULL + #define ALLOC_CHECK(x) #endif #if ( __GNUC__ >= 3 ) diff --git a/engine/common/common.h b/engine/common/common.h index 3ae43936..45b1d19c 100644 --- a/engine/common/common.h +++ b/engine/common/common.h @@ -422,8 +422,8 @@ void Cmd_Escape( char *newCommand, const char *oldCommand, int len ); // zone.c // void Memory_Init( void ); -void *_Mem_Realloc( poolhandle_t poolptr, void *memptr, size_t size, qboolean clear, const char *filename, int fileline ); -void *_Mem_Alloc( poolhandle_t poolptr, size_t size, qboolean clear, const char *filename, int fileline ); +void *_Mem_Realloc( poolhandle_t poolptr, void *memptr, size_t size, qboolean clear, const char *filename, int fileline ) ALLOC_CHECK( 3 ); +void *_Mem_Alloc( poolhandle_t poolptr, size_t size, qboolean clear, const char *filename, int fileline ) ALLOC_CHECK( 2 ); poolhandle_t _Mem_AllocPool( const char *name, const char *filename, int fileline ); void _Mem_FreePool( poolhandle_t *poolptr, const char *filename, int fileline ); void _Mem_EmptyPool( poolhandle_t poolptr, const char *filename, int fileline ); diff --git a/engine/menu_int.h b/engine/menu_int.h index d32c4bb3..97d081b2 100644 --- a/engine/menu_int.h +++ b/engine/menu_int.h @@ -143,7 +143,7 @@ typedef struct ui_enginefuncs_s void *(*pfnKeyGetState)( const char *name ); // for mlook, klook etc // engine memory manager - void* (*pfnMemAlloc)( size_t cb, const char *filename, const int fileline ); + void* (*pfnMemAlloc)( size_t cb, const char *filename, const int fileline ) ALLOC_CHECK( 1 ); void (*pfnMemFree)( void *mem, const char *filename, const int fileline ); // collect info from engine diff --git a/engine/physint.h b/engine/physint.h index 9c246023..d5f68461 100644 --- a/engine/physint.h +++ b/engine/physint.h @@ -86,7 +86,7 @@ typedef struct server_physics_api_s const byte *(*pfnGetTextureData)( unsigned int texnum ); // static allocations - void *(*pfnMemAlloc)( size_t cb, const char *filename, const int fileline ); + void *(*pfnMemAlloc)( size_t cb, const char *filename, const int fileline ) ALLOC_CHECK( 1 ); void (*pfnMemFree)( void *mem, const char *filename, const int fileline ); // trace & contents diff --git a/engine/ref_api.h b/engine/ref_api.h index 15d6c477..707e43fe 100644 --- a/engine/ref_api.h +++ b/engine/ref_api.h @@ -361,8 +361,8 @@ typedef struct ref_api_s // memory poolhandle_t (*_Mem_AllocPool)( const char *name, const char *filename, int fileline ); void (*_Mem_FreePool)( poolhandle_t *poolptr, const char *filename, int fileline ); - void *(*_Mem_Alloc)( poolhandle_t poolptr, size_t size, qboolean clear, const char *filename, int fileline ); - void *(*_Mem_Realloc)( poolhandle_t poolptr, void *memptr, size_t size, qboolean clear, const char *filename, int fileline ); + void *(*_Mem_Alloc)( poolhandle_t poolptr, size_t size, qboolean clear, const char *filename, int fileline ) ALLOC_CHECK( 2 ); + void *(*_Mem_Realloc)( poolhandle_t poolptr, void *memptr, size_t size, qboolean clear, const char *filename, int fileline ) ALLOC_CHECK( 3 ); void (*_Mem_Free)( void *data, const char *filename, int fileline ); // library management diff --git a/filesystem/filesystem.h b/filesystem/filesystem.h index 4a06c31e..9a312adc 100644 --- a/filesystem/filesystem.h +++ b/filesystem/filesystem.h @@ -210,8 +210,8 @@ typedef struct fs_interface_t // memory poolhandle_t (*_Mem_AllocPool)( const char *name, const char *filename, int fileline ); void (*_Mem_FreePool)( poolhandle_t *poolptr, const char *filename, int fileline ); - void *(*_Mem_Alloc)( poolhandle_t poolptr, size_t size, qboolean clear, const char *filename, int fileline ); - void *(*_Mem_Realloc)( poolhandle_t poolptr, void *memptr, size_t size, qboolean clear, const char *filename, int fileline ); + void *(*_Mem_Alloc)( poolhandle_t poolptr, size_t size, qboolean clear, const char *filename, int fileline ) ALLOC_CHECK( 2 ); + void *(*_Mem_Realloc)( poolhandle_t poolptr, void *memptr, size_t size, qboolean clear, const char *filename, int fileline ) ALLOC_CHECK( 3 ); void (*_Mem_Free)( void *data, const char *filename, int fileline ); // platform