diff mbox series

[FFmpeg-devel,6/6] avutil/frame: use av_buffer_replace() to simplify code

Message ID 20201118130517.585-7-git@gpost.dk
State Accepted
Headers show
Series Use av_buffer_replace()
Related show

Checks

Context Check Description
andriy/x86_make success Make finished
andriy/x86_make_fate success Make fate finished
andriy/PPC64_make success Make finished
andriy/PPC64_make_fate success Make fate finished

Commit Message

Gil Pedersen Nov. 18, 2020, 1:05 p.m. UTC
Also fixes potential leaks in failing av_frame_ref().

Signed-off-by: Gil Pedersen <git@gpost.dk>
---
 libavutil/frame.c | 37 ++++++++++++-------------------------
 1 file changed, 12 insertions(+), 25 deletions(-)

Comments

Anton Khirnov Nov. 19, 2020, 7:38 a.m. UTC | #1
Quoting Gil Pedersen (2020-11-18 14:05:17)
> Also fixes potential leaks in failing av_frame_ref().
> 
> Signed-off-by: Gil Pedersen <git@gpost.dk>
> ---

Looks ok
James Almer Nov. 19, 2020, 1:14 p.m. UTC | #2
On 11/19/2020 4:38 AM, Anton Khirnov wrote:
> Quoting Gil Pedersen (2020-11-18 14:05:17)
>> Also fixes potential leaks in failing av_frame_ref().
>>
>> Signed-off-by: Gil Pedersen <git@gpost.dk>
>> ---
> 
> Looks ok

Split in two and applied.
diff mbox series

Patch

diff --git a/libavutil/frame.c b/libavutil/frame.c
index 3ab1aa3242..b019779b1a 100644
--- a/libavutil/frame.c
+++ b/libavutil/frame.c
@@ -349,7 +349,7 @@  int av_frame_get_buffer(AVFrame *frame, int align)
 
 static int frame_copy_props(AVFrame *dst, const AVFrame *src, int force_copy)
 {
-    int i;
+    int ret, i;
 
     dst->key_frame              = src->key_frame;
     dst->pict_type              = src->pict_type;
@@ -426,31 +426,18 @@  FF_DISABLE_DEPRECATION_WARNINGS
     dst->qscale_table = NULL;
     dst->qstride      = 0;
     dst->qscale_type  = 0;
-    av_buffer_unref(&dst->qp_table_buf);
-    if (src->qp_table_buf) {
-        dst->qp_table_buf = av_buffer_ref(src->qp_table_buf);
-        if (dst->qp_table_buf) {
-            dst->qscale_table = dst->qp_table_buf->data;
-            dst->qstride      = src->qstride;
-            dst->qscale_type  = src->qscale_type;
-        }
+    av_buffer_replace(&dst->qp_table_buf, src->qp_table_buf);
+    if (dst->qp_table_buf) {
+        dst->qscale_table = dst->qp_table_buf->data;
+        dst->qstride      = src->qstride;
+        dst->qscale_type  = src->qscale_type;
     }
 FF_ENABLE_DEPRECATION_WARNINGS
 #endif
 
-    av_buffer_unref(&dst->opaque_ref);
-    av_buffer_unref(&dst->private_ref);
-    if (src->opaque_ref) {
-        dst->opaque_ref = av_buffer_ref(src->opaque_ref);
-        if (!dst->opaque_ref)
-            return AVERROR(ENOMEM);
-    }
-    if (src->private_ref) {
-        dst->private_ref = av_buffer_ref(src->private_ref);
-        if (!dst->private_ref)
-            return AVERROR(ENOMEM);
-    }
-    return 0;
+    ret = av_buffer_replace(&dst->opaque_ref, src->opaque_ref);
+    ret |= av_buffer_replace(&dst->private_ref, src->private_ref);
+    return ret;
 }
 
 int av_frame_ref(AVFrame *dst, const AVFrame *src)
@@ -469,17 +456,17 @@  int av_frame_ref(AVFrame *dst, const AVFrame *src)
 
     ret = frame_copy_props(dst, src, 0);
     if (ret < 0)
-        return ret;
+        goto fail;
 
     /* duplicate the frame data if it's not refcounted */
     if (!src->buf[0]) {
         ret = av_frame_get_buffer(dst, 0);
         if (ret < 0)
-            return ret;
+            goto fail;
 
         ret = av_frame_copy(dst, src);
         if (ret < 0)
-            av_frame_unref(dst);
+            goto fail;
 
         return ret;
     }