diff mbox series

[FFmpeg-devel,17/23] dnn/dnn_backend_native_layer_conv2d: Fix memleak on error

Message ID 20210310215446.1396386-8-andreas.rheinhardt@gmail.com
State Accepted
Headers show
Series [FFmpeg-devel,1/8] avcodec/cbs: Remove redundant checks for CodedBitstreamContext.codec | expand

Commit Message

Andreas Rheinhardt March 10, 2021, 9:54 p.m. UTC
If an error happens when preparing the output data buffer, an already
allocated array would leak. Fix this by postponing its allocation.
Also zero-allocate the allocated array for safety.

Fixes Coverity issue #1473531.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
---
 libavfilter/dnn/dnn_backend_native_layer_conv2d.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

Comments

Guo, Yejun March 11, 2021, 6:59 a.m. UTC | #1
> -----Original Message-----
> From: ffmpeg-devel <ffmpeg-devel-bounces@ffmpeg.org> On Behalf Of
> Andreas Rheinhardt
> Sent: 2021年3月11日 5:55
> To: ffmpeg-devel@ffmpeg.org
> Cc: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
> Subject: [FFmpeg-devel] [PATCH 17/23]
> dnn/dnn_backend_native_layer_conv2d: Fix memleak on error
> 
> If an error happens when preparing the output data buffer, an already
> allocated array would leak. Fix this by postponing its allocation.
> Also zero-allocate the allocated array for safety.
> 
> Fixes Coverity issue #1473531.
> 
> Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
> ---
>  libavfilter/dnn/dnn_backend_native_layer_conv2d.c | 3 ++-
>  1 file changed, 2 insertions(+), 1 deletion(-)
> 
> diff --git a/libavfilter/dnn/dnn_backend_native_layer_conv2d.c
> b/libavfilter/dnn/dnn_backend_native_layer_conv2d.c
> index cdf65974b6..c2073a02de 100644
> --- a/libavfilter/dnn/dnn_backend_native_layer_conv2d.c
> +++ b/libavfilter/dnn/dnn_backend_native_layer_conv2d.c
> @@ -191,7 +191,7 @@ int ff_dnn_execute_layer_conv2d(DnnOperand
> *operands, const int32_t *input_opera
>      int thread_num = (ctx->options.conv2d_threads <= 0 ||
> ctx->options.conv2d_threads > av_cpu_count())
>          ? (av_cpu_count() + 1) : (ctx->options.conv2d_threads);
>      int thread_stride;
> -    ThreadParam **thread_param = av_malloc_array(thread_num,
> sizeof(*thread_param));
> +    ThreadParam **thread_param;
>  #else
>      ThreadParam thread_param = { 0 };
>  #endif
> @@ -227,6 +227,7 @@ int ff_dnn_execute_layer_conv2d(DnnOperand
> *operands, const int32_t *input_opera
>      thread_common_param.ctx = ctx;
> 
>  #if HAVE_PTHREAD_CANCEL
> +    thread_param = av_calloc(thread_num, sizeof(*thread_param));

Just think the zero-allocation is not needed, others LGTM.

Also notice that the original code does not check the return value, it is
handled by latter patches in the patch set.

>      thread_stride = (height - pad_size * 2) / thread_num;
>      //create threads
>      for (int i = 0; i < thread_num; i++){
> --
> 2.27.0
> 
> _______________________________________________
> 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".
diff mbox series

Patch

diff --git a/libavfilter/dnn/dnn_backend_native_layer_conv2d.c b/libavfilter/dnn/dnn_backend_native_layer_conv2d.c
index cdf65974b6..c2073a02de 100644
--- a/libavfilter/dnn/dnn_backend_native_layer_conv2d.c
+++ b/libavfilter/dnn/dnn_backend_native_layer_conv2d.c
@@ -191,7 +191,7 @@  int ff_dnn_execute_layer_conv2d(DnnOperand *operands, const int32_t *input_opera
     int thread_num = (ctx->options.conv2d_threads <= 0 || ctx->options.conv2d_threads > av_cpu_count())
         ? (av_cpu_count() + 1) : (ctx->options.conv2d_threads);
     int thread_stride;
-    ThreadParam **thread_param = av_malloc_array(thread_num, sizeof(*thread_param));
+    ThreadParam **thread_param;
 #else
     ThreadParam thread_param = { 0 };
 #endif
@@ -227,6 +227,7 @@  int ff_dnn_execute_layer_conv2d(DnnOperand *operands, const int32_t *input_opera
     thread_common_param.ctx = ctx;
 
 #if HAVE_PTHREAD_CANCEL
+    thread_param = av_calloc(thread_num, sizeof(*thread_param));
     thread_stride = (height - pad_size * 2) / thread_num;
     //create threads
     for (int i = 0; i < thread_num; i++){