Initialize allocated memory to garbage

for debug builds.
This commit is contained in:
derselbst 2019-10-05 16:43:53 +02:00
parent 62b38b1c66
commit a94bc82a3c
3 changed files with 30 additions and 4 deletions

View file

@ -195,6 +195,29 @@ fluid_log(int level, const char *fmt, ...)
return FLUID_FAILED;
}
void* fluid_alloc(size_t len)
{
void* ptr = malloc(len);
#if defined(DEBUG) && !defined(_MSC_VER)
// garbage initialize allocated memory for debug builds to ease reproducing
// bugs like 44453ff23281b3318abbe432fda90888c373022b .
//
// MSVC++ already garbage initializes allocated memory by itself (debug-heap).
//
// 0xCC because
// * it makes pointers reliably crash when dereferencing them,
// * floating points are still some valid but insanely huge negative number, and
// * if for whatever reason this allocated memory is executed, it'll trigger
// INT3 (...at least on x86)
if(ptr != NULL)
{
memset(ptr, 0xCC, len);
}
#endif
return ptr;
}
/**
* Convenience wrapper for free() that satisfies at least C90 requirements.
* Especially useful when using fluidsynth with programming languages that do not provide malloc() and free().

View file

@ -739,4 +739,6 @@ static FLUID_INLINE void *fluid_align_ptr(const void *ptr, unsigned int alignmen
#define FLUID_DEFAULT_ALIGNMENT (64U)
void* fluid_alloc(size_t len);
#endif /* _FLUID_SYS_H */

View file

@ -126,12 +126,13 @@ typedef void (*fluid_rvoice_function_t)(void *obj, const fluid_rvoice_param_t pa
* SYSTEM INTERFACE
*/
#define FLUID_MALLOC(_n) malloc(_n)
#define FLUID_MALLOC(_n) fluid_alloc(_n)
#define FLUID_REALLOC(_p,_n) realloc(_p,_n)
#define FLUID_NEW(_t) (_t*)malloc(sizeof(_t))
#define FLUID_ARRAY_ALIGNED(_t,_n,_a) (_t*)malloc((_n)*sizeof(_t) + ((unsigned int)_a - 1u))
#define FLUID_ARRAY(_t,_n) FLUID_ARRAY_ALIGNED(_t,_n,1u)
#define FLUID_FREE(_p) fluid_free(_p)
#define FLUID_NEW(_t) (_t*)FLUID_MALLOC(sizeof(_t))
#define FLUID_ARRAY_ALIGNED(_t,_n,_a) (_t*)FLUID_MALLOC((_n)*sizeof(_t) + ((unsigned int)_a - 1u))
#define FLUID_ARRAY(_t,_n) FLUID_ARRAY_ALIGNED(_t,_n,1u)
#define FLUID_FOPEN(_f,_m) fopen(_f,_m)
#define FLUID_FCLOSE(_f) fclose(_f)
#define FLUID_FREAD(_p,_s,_n,_f) fread(_p,_s,_n,_f)