[flac-dev] Memory leaks
lvqcl
lvqcl.mail at gmail.com
Sat Jul 4 04:48:21 PDT 2015
There are several places in libFLAC like this:
if(0 == (x = realloc(x, size)))
return false;
and
if(0 == (x = safe_realloc_mul_2op_(x, size1, size2))) {
decoder_state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
return false;
}
If realloc fails, then the previous value of pointer x is lost and we have
memory leak. The simplest fix is to add new functions like this:
static inline void *realloc_noleak_(void *ptr, size_t size)
{
void *tmp = realloc(ptr, size);
if(!tmp)
free(ptr); /* no memory leak */
return tmp;
}
static inline void *safe_realloc_mul_2op_noleak_(void *ptr, size_t size1, size_t size2)
{
if(!size1 || !size2)
return realloc(ptr, 0); /* preserve POSIX realloc(ptr, 0) semantics */
if(size1 > SIZE_MAX / size2)
return 0;
return realloc_noleak_(ptr, size1*size2);
}
And use them in such places. Or maybe some better solution exists?
More information about the flac-dev
mailing list