[FFmpeg-devel] [PATCH 10/10] avformat/nutenc: Don't allocate array with zero entries

Andreas Rheinhardt andreas.rheinhardt at gmail.com
Sat May 9 07:52:35 EEST 2020


Michael Niedermayer:
> On Mon, May 04, 2020 at 08:22:50PM +0200, Andreas Rheinhardt wrote:
>> Allocating an array with zero entries is both unnecessary as well as
>> potentially troublesome because the behaviour in this case is not really
>> well defined.
> 
> What is not well defined ?
> 
The behaviour of our memory-allocation functions in case one requests to
allocate zero bytes is not well-defined (does it return NULL or not?).
E.g. although the documentation of av_mallocz_array() (which applies to
av_calloc, too) treats the size and nmemb parameters symmetrically, the
actual implementation differed until commit
b7d9507bb8c4d1b8bf99158d6859a5b2ecd73298 (up until then, a size of zero
would have resulted in NULL being returned). In the discussion [1]
around this patch there were devs favouring returning NULL upon any
request to allocate zero bytes. The NUT muxer is btw one of the two main
reasons for failing fate-tests in case one returned NULL when an
allocation of zero bytes is requested.

What happens on allocation of zero bytes is implementation-defined for
the analogous C functions; in our implementation the only function that
explicitly documents what happens when the size fed to it is zero is
av_realloc() which does not abide by its documentation (it does not free
anything when size is zero, but rather reallocates the buffer to a size
of one).

- Andreas

[1]: https://ffmpeg.org/pipermail/ffmpeg-devel/2020-April/260493.html


More information about the ffmpeg-devel mailing list