[FFmpeg-devel,v1] avcodec/dnxhdenc: return error if av_malloc failed

Submitted by lance.lmwang@gmail.com on Sept. 22, 2019, 2:06 p.m.

Details

Message ID 20190922140616.2216-1-lance.lmwang@gmail.com
State New
Headers show

Commit Message

lance.lmwang@gmail.com Sept. 22, 2019, 2:06 p.m.
From: Limin Wang <lance.lmwang@gmail.com>

Signed-off-by: Limin Wang <lance.lmwang@gmail.com>
---
 libavcodec/dnxhdenc.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

Comments

James Almer Sept. 22, 2019, 2:15 p.m.
On 9/22/2019 11:06 AM, lance.lmwang@gmail.com wrote:
> From: Limin Wang <lance.lmwang@gmail.com>
> 
> Signed-off-by: Limin Wang <lance.lmwang@gmail.com>
> ---
>  libavcodec/dnxhdenc.c | 5 ++++-
>  1 file changed, 4 insertions(+), 1 deletion(-)
> 
> diff --git a/libavcodec/dnxhdenc.c b/libavcodec/dnxhdenc.c
> index 41b8079..f144406 100644
> --- a/libavcodec/dnxhdenc.c
> +++ b/libavcodec/dnxhdenc.c
> @@ -365,7 +365,7 @@ fail:
>  static av_cold int dnxhd_encode_init(AVCodecContext *avctx)
>  {
>      DNXHDEncContext *ctx = avctx->priv_data;
> -    int i, index, ret;
> +    int i = 1, index, ret;
>  
>      switch (avctx->pix_fmt) {
>      case AV_PIX_FMT_YUV422P:
> @@ -542,12 +542,15 @@ FF_ENABLE_DEPRECATION_WARNINGS
>      if (avctx->active_thread_type == FF_THREAD_SLICE) {
>          for (i = 1; i < avctx->thread_count; i++) {
>              ctx->thread[i] = av_malloc(sizeof(DNXHDEncContext));
> +            if(!ctx->thread[i])
> +                goto fail;
>              memcpy(ctx->thread[i], ctx, sizeof(DNXHDEncContext));
>          }
>      }
>  
>      return 0;
>  fail:  // for FF_ALLOCZ_OR_GOTO
> +    avctx->thread_count = i;

This is unnecessary. All ctx->thread[] array elements are zero
initialized, so the av_freep() calls in dnxhd_encode_end() are safe for
those that were never allocated.

>      return AVERROR(ENOMEM);
>  }
>  
>
lance.lmwang@gmail.com Sept. 22, 2019, 3:50 p.m.
On Sun, Sep 22, 2019 at 11:15:51AM -0300, James Almer wrote:
> On 9/22/2019 11:06 AM, lance.lmwang@gmail.com wrote:
> > From: Limin Wang <lance.lmwang@gmail.com>
> > 
> > Signed-off-by: Limin Wang <lance.lmwang@gmail.com>
> > ---
> >  libavcodec/dnxhdenc.c | 5 ++++-
> >  1 file changed, 4 insertions(+), 1 deletion(-)
> > 
> > diff --git a/libavcodec/dnxhdenc.c b/libavcodec/dnxhdenc.c
> > index 41b8079..f144406 100644
> > --- a/libavcodec/dnxhdenc.c
> > +++ b/libavcodec/dnxhdenc.c
> > @@ -365,7 +365,7 @@ fail:
> >  static av_cold int dnxhd_encode_init(AVCodecContext *avctx)
> >  {
> >      DNXHDEncContext *ctx = avctx->priv_data;
> > -    int i, index, ret;
> > +    int i = 1, index, ret;
> >  
> >      switch (avctx->pix_fmt) {
> >      case AV_PIX_FMT_YUV422P:
> > @@ -542,12 +542,15 @@ FF_ENABLE_DEPRECATION_WARNINGS
> >      if (avctx->active_thread_type == FF_THREAD_SLICE) {
> >          for (i = 1; i < avctx->thread_count; i++) {
> >              ctx->thread[i] = av_malloc(sizeof(DNXHDEncContext));
> > +            if(!ctx->thread[i])
> > +                goto fail;
> >              memcpy(ctx->thread[i], ctx, sizeof(DNXHDEncContext));
> >          }
> >      }
> >  
> >      return 0;
> >  fail:  // for FF_ALLOCZ_OR_GOTO
> > +    avctx->thread_count = i;
> 
> This is unnecessary. All ctx->thread[] array elements are zero
> initialized, so the av_freep() calls in dnxhd_encode_end() are safe for
> those that were never allocated.

After more review for the code, I think it's unneed to define the thread[i] 
totally by my study for the thread code. I have post the update patch, 
please point out if I'm misunderstand as I'm lacking the background for
it.

> 
> >      return AVERROR(ENOMEM);
> >  }
> >  
> > 
> 
> _______________________________________________
> ffmpeg-devel mailing list
> ffmpeg-devel@ffmpeg.org
> https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
> 
> To unsubscribe, visit link above, or email
> ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

Patch hide | download patch | download mbox

diff --git a/libavcodec/dnxhdenc.c b/libavcodec/dnxhdenc.c
index 41b8079..f144406 100644
--- a/libavcodec/dnxhdenc.c
+++ b/libavcodec/dnxhdenc.c
@@ -365,7 +365,7 @@  fail:
 static av_cold int dnxhd_encode_init(AVCodecContext *avctx)
 {
     DNXHDEncContext *ctx = avctx->priv_data;
-    int i, index, ret;
+    int i = 1, index, ret;
 
     switch (avctx->pix_fmt) {
     case AV_PIX_FMT_YUV422P:
@@ -542,12 +542,15 @@  FF_ENABLE_DEPRECATION_WARNINGS
     if (avctx->active_thread_type == FF_THREAD_SLICE) {
         for (i = 1; i < avctx->thread_count; i++) {
             ctx->thread[i] = av_malloc(sizeof(DNXHDEncContext));
+            if(!ctx->thread[i])
+                goto fail;
             memcpy(ctx->thread[i], ctx, sizeof(DNXHDEncContext));
         }
     }
 
     return 0;
 fail:  // for FF_ALLOCZ_OR_GOTO
+    avctx->thread_count = i;
     return AVERROR(ENOMEM);
 }