[FFmpeg-devel] size=0, but av_malloc(1)

Reimar Döffinger Reimar.Doeffinger at gmx.de
Wed Mar 23 21:56:14 CET 2016

On Wed, Mar 23, 2016 at 03:31:38PM +0100, Michael Niedermayer wrote:
> On Tue, Mar 22, 2016 at 11:43:50PM -0700, Chris Cunningham wrote:
> > Hey Group,
> > 
> > I'm seeing an interesting pattern [0][1] where we allocate 1 byte in places
> > where I would expect no allocation to be necessary. Why is this being done?
> > 
> > [0] https://github.com/FFmpeg/FFmpeg/blob/master/libavutil/mem.c#L136
> > [1]
> > https://github.com/FFmpeg/FFmpeg/blob/master/libavformat/oggparsevorbis.c#L286
> to add another reason why malloc(0) _can_ be a problem
> malloc(0) can return NULL or non NULL whchever way libc prefers
> this makes reproducing bugreports harder if the developer and user
> have differening libcs
> also error checks become more complex if NULL can be a non error
> return value

Since you already said that: if that code used malloc(0) - note I
don't know about av_malloc - and it returned 0, the behaviour would
be incorrect.
In this code, a NULL pointer means "no metadata update" which is very
different from "metadata update to empty metadata".
Also even if it does not return NULL it could always return the same
pointer, which could trigger yet another class of bugs (probably
not in this case though).

More information about the ffmpeg-devel mailing list