[FFmpeg-devel] [PATCH] av_malloc() workaround for QNX platform

Mike Gorchak mike.gorchak.qnx at gmail.com
Thu Feb 7 16:22:16 CET 2013

>> After malloc() there is needed a check for ptr which can be ALREADY
>> aligned. In this case we have not to execute this line: ((char
>> *)ptr)[-1] = diff; which can cause a memory trashing. But better to
>> allocate (size+2*ALIGN) bytes and then move ptr forward for at least ALIGN
>> bytes to make sure that buffer has spare space for writting a 'diff'
>> value.
> I looked through the memalign hack code, and it seems to work as expected.
> If ptr returned by malloc is already aligned, diff will equal ALIGN,
> and the returned pointer is advanced by ALIGN, and there is plenty
> space to write the diff.

Yes, diff will be equal to align but what to do with *(ptr)[-1] ? It
writes to non-allocated memory.

> Regarding your patch, it indeed looks terribly hacky. Such
> inconsistent behavior should maybe simply use the memalign hack and be
> done with it?

With memalign hack mplayer segfaults due to memory corruption of
malloc internal data, which is located right before allocated memory.
And [-1] write damages malloc's internal data in case of already
aligned memory region.

More information about the ffmpeg-devel mailing list