avformat/utils: Fix memleaks

ff_read_packet had potential memleaks:
1. If av_packet_make_refcounted fails, it means that the packet is not
refcounted, but it could nevertheless carry side data and therefore
needs to be unreferenced.
2. If putting a packet on a packet list fails, it wasn't unreferenced.

Furthermore, read_frame_internal leaked a packet's (side) data if a
context update was required and failed.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
Signed-off-by: James Almer <jamrial@gmail.com>
(cherry picked from commit 66d5e43b99)
This commit is contained in:
Andreas Rheinhardt 2019-09-24 18:31:44 +02:00
parent 2c88423857
commit f2117e1e9c
1 changed files with 12 additions and 4 deletions

View File

@ -878,8 +878,10 @@ int ff_read_packet(AVFormatContext *s, AVPacket *pkt)
}
err = av_packet_make_refcounted(pkt);
if (err < 0)
if (err < 0) {
av_packet_unref(pkt);
return err;
}
if ((s->flags & AVFMT_FLAG_DISCARD_CORRUPT) &&
(pkt->flags & AV_PKT_FLAG_CORRUPT)) {
@ -922,8 +924,10 @@ int ff_read_packet(AVFormatContext *s, AVPacket *pkt)
err = ff_packet_list_put(&s->internal->raw_packet_buffer,
&s->internal->raw_packet_buffer_end,
pkt, 0);
if (err)
if (err < 0) {
av_packet_unref(pkt);
return err;
}
s->internal->raw_packet_buffer_remaining_size -= pkt->size;
if ((err = probe_codec(s, st, pkt)) < 0)
@ -1616,15 +1620,19 @@ static int read_frame_internal(AVFormatContext *s, AVPacket *pkt)
}
ret = avcodec_parameters_to_context(st->internal->avctx, st->codecpar);
if (ret < 0)
if (ret < 0) {
av_packet_unref(&cur_pkt);
return ret;
}
#if FF_API_LAVF_AVCTX
FF_DISABLE_DEPRECATION_WARNINGS
/* update deprecated public codec context */
ret = avcodec_parameters_to_context(st->codec, st->codecpar);
if (ret < 0)
if (ret < 0) {
av_packet_unref(&cur_pkt);
return ret;
}
FF_ENABLE_DEPRECATION_WARNINGS
#endif