]> git.sesse.net Git - ffmpeg/commit
avcodec/mpegvideo: Fix memleak upon allocation error
authorAndreas Rheinhardt <andreas.rheinhardt@gmail.com>
Fri, 25 Dec 2020 13:17:10 +0000 (14:17 +0100)
committerAndreas Rheinhardt <andreas.rheinhardt@outlook.com>
Wed, 7 Apr 2021 23:09:09 +0000 (01:09 +0200)
commitff0706cde8b1a1f483e26c0ccac117c23b23d604
treebd0d4da1a3118346b27a666684ed701f7b33efed
parentd4b9e117ceb6356cbcdc9ca81ec9c6c4b90efdae
avcodec/mpegvideo: Fix memleak upon allocation error

When slice-threading is used, ff_mpv_common_init() duplicates
the first MpegEncContext and allocates some buffers for each
MpegEncContext (the first as well as the copies). But the count of
allocated MpegEncContexts is not updated until after everything has
been allocated and if an error happens after the first one has been
allocated, only the first one is freed; the others leak.

This commit fixes this: The count is now set before the copies are
allocated. Furthermore, the copies are now created and initialized
before the first MpegEncContext, so that the buffers exclusively owned
by each MpegEncContext are still NULL in the src MpegEncContext so
that no double-free happens upon allocation failure.

Given that this effectively touches every line of the init code,
it has also been factored out in a function of its own in order to
remove code duplication with the same code in
ff_mpv_common_frame_size_change() (which was never called when using
more than one slice (and if it were, there would be potential
double-frees)).

Reviewed-by: Michael Niedermayer <michael@niedermayer.cc>
Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
libavcodec/mpegvideo.c