[FFmpeg-devel] [PATCH v4 2/9] avformat/utils: unref packet on AVInputFormat.read_packet() failure

James Almer jamrial at gmail.com
Sun Sep 29 01:45:35 EEST 2019


On 9/20/2019 5:39 PM, Andreas Rheinhardt wrote:
> From: James Almer <jamrial at gmail.com>
> 
> Demuxers may have allocated a packet before encountering an error and aborting.

Fun thing about this, the doxy states that packets should be freed on
error before returning from the demuxer, so technically the apng demuxer
is misbehaving.

Still, i think the generic code doing some cleaning is a good idea. But
of course, if people prefer to enforce the expected behavior from
documentation, we could replace the unref call introduced by this patch
with an assert.

> 
> Fixes ticket #8150
> 
> Signed-off-by: James Almer <jamrial at gmail.com>
> ---
> Earlier versions of this patchset used av_init_packet() together with
> setting the size and data to zero/NULL. This would have caused memleaks
> when the packet contained data that needs to be freed/unreferenced,
> whereas this approach here runs the risk of double freeing. I consider
> the risk of the latter much lower than the risk of the former and have
> therefore included this patch in this patchset.
> 
>  libavformat/utils.c | 2 ++
>  1 file changed, 2 insertions(+)
> 
> diff --git a/libavformat/utils.c b/libavformat/utils.c
> index 652642a71b..e348df3269 100644
> --- a/libavformat/utils.c
> +++ b/libavformat/utils.c
> @@ -854,6 +854,8 @@ int ff_read_packet(AVFormatContext *s, AVPacket *pkt)
>          av_init_packet(pkt);
>          ret = s->iformat->read_packet(s, pkt);
>          if (ret < 0) {
> +            av_packet_unref(pkt);
> +
>              /* Some demuxers return FFERROR_REDO when they consume
>                 data and discard it (ignored streams, junk, extradata).
>                 We must re-call the demuxer to get the real packet. */
> 



More information about the ffmpeg-devel mailing list