diff mbox series

[FFmpeg-devel] avcodec/encode: unref the packet on AVCodec.receive_packet() failure

Message ID 20200831025954.2588-1-jamrial@gmail.com
State Accepted
Commit 0271098e6c9ff8f2a97d65087e424f6d547e53f9
Headers show
Series [FFmpeg-devel] avcodec/encode: unref the packet on AVCodec.receive_packet() failure | expand

Checks

Context Check Description
andriy/default pending
andriy/make success Make finished
andriy/make_fate success Make fate finished

Commit Message

James Almer Aug. 31, 2020, 2:59 a.m. UTC
Fixes memleaks with some encoders that don't unref the packet before
returning.
This is consistent with the behavior of AVCodec.encode()
implementations in encode_simple_internal().

Found-by: mkver
Signed-off-by: James Almer <jamrial@gmail.com>
---
 libavcodec/encode.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

Comments

Anton Khirnov Sept. 1, 2020, 12:14 p.m. UTC | #1
Quoting James Almer (2020-08-31 04:59:54)
> Fixes memleaks with some encoders that don't unref the packet before
> returning.
> This is consistent with the behavior of AVCodec.encode()
> implementations in encode_simple_internal().
> 
> Found-by: mkver
> Signed-off-by: James Almer <jamrial@gmail.com>
> ---
>  libavcodec/encode.c | 4 +++-
>  1 file changed, 3 insertions(+), 1 deletion(-)

Looks ok.
James Almer Sept. 1, 2020, 1:06 p.m. UTC | #2
On 9/1/2020 9:14 AM, Anton Khirnov wrote:
> Quoting James Almer (2020-08-31 04:59:54)
>> Fixes memleaks with some encoders that don't unref the packet before
>> returning.
>> This is consistent with the behavior of AVCodec.encode()
>> implementations in encode_simple_internal().
>>
>> Found-by: mkver
>> Signed-off-by: James Almer <jamrial@gmail.com>
>> ---
>>  libavcodec/encode.c | 4 +++-
>>  1 file changed, 3 insertions(+), 1 deletion(-)
> 
> Looks ok.

Applied, thanks.
diff mbox series

Patch

diff --git a/libavcodec/encode.c b/libavcodec/encode.c
index 8bc10c4abb..2e540baf37 100644
--- a/libavcodec/encode.c
+++ b/libavcodec/encode.c
@@ -242,7 +242,9 @@  static int encode_receive_packet_internal(AVCodecContext *avctx, AVPacket *avpkt
 
     if (avctx->codec->receive_packet) {
         ret = avctx->codec->receive_packet(avctx, avpkt);
-        if (!ret)
+        if (ret < 0)
+            av_packet_unref(avpkt);
+        else
             // Encoders must always return ref-counted buffers.
             // Side-data only packets have no data and can be not ref-counted.
             av_assert0(!avpkt->data || avpkt->buf);