diff mbox series

[FFmpeg-devel,01/30] avcodec/flashsvenc: Avoid allocation of buffer, fix memleak

Message ID 20200915074000.102622-1-andreas.rheinhardt@gmail.com
State Accepted
Commit ec6f4c51586cc213d16749f930970ba8afd0e38a
Headers show
Series [FFmpeg-devel,01/30] avcodec/flashsvenc: Avoid allocation of buffer, fix memleak
Related show

Checks

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

Commit Message

Andreas Rheinhardt Sept. 15, 2020, 7:39 a.m. UTC
Up until now, the flashsv encoder tried to allocate two buffers in its
init function; if only one of these allocations succeeds, the other
buffer leaks. Fix this by making one of these buffers part of the
context (its size is a compile-time constant).

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
---
 libavcodec/flashsvenc.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)

Comments

Andreas Rheinhardt Sept. 18, 2020, 2:23 a.m. UTC | #1
Andreas Rheinhardt:
> Up until now, the flashsv encoder tried to allocate two buffers in its
> init function; if only one of these allocations succeeds, the other
> buffer leaks. Fix this by making one of these buffers part of the
> context (its size is a compile-time constant).
> 
> Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
> ---
>  libavcodec/flashsvenc.c | 6 ++----
>  1 file changed, 2 insertions(+), 4 deletions(-)
> 
> diff --git a/libavcodec/flashsvenc.c b/libavcodec/flashsvenc.c
> index f7f98efde3..4ac643c036 100644
> --- a/libavcodec/flashsvenc.c
> +++ b/libavcodec/flashsvenc.c
> @@ -59,11 +59,11 @@ typedef struct FlashSVContext {
>      uint8_t        *previous_frame;
>      int             image_width, image_height;
>      int             block_width, block_height;
> -    uint8_t        *tmpblock;
>      uint8_t        *encbuffer;
>      int             block_size;
>      z_stream        zstream;
>      int             last_key_frame;
> +    uint8_t         tmpblock[3 * 256 * 256];
>  } FlashSVContext;
>  
>  static int copy_region_enc(uint8_t *sptr, uint8_t *dptr, int dx, int dy,
> @@ -96,7 +96,6 @@ static av_cold int flashsv_encode_end(AVCodecContext *avctx)
>  
>      av_freep(&s->encbuffer);
>      av_freep(&s->previous_frame);
> -    av_freep(&s->tmpblock);
>  
>      return 0;
>  }
> @@ -121,10 +120,9 @@ static av_cold int flashsv_encode_init(AVCodecContext *avctx)
>      s->image_width  = avctx->width;
>      s->image_height = avctx->height;
>  
> -    s->tmpblock  = av_mallocz(3 * 256 * 256);
>      s->encbuffer = av_mallocz(s->image_width * s->image_height * 3);
>  
> -    if (!s->tmpblock || !s->encbuffer) {
> +    if (!s->encbuffer) {
>          av_log(avctx, AV_LOG_ERROR, "Memory allocation failed.\n");
>          return AVERROR(ENOMEM);
>      }
> 
Will apply the rest of this patchset tomorrow unless there are objections.

- Andreas
diff mbox series

Patch

diff --git a/libavcodec/flashsvenc.c b/libavcodec/flashsvenc.c
index f7f98efde3..4ac643c036 100644
--- a/libavcodec/flashsvenc.c
+++ b/libavcodec/flashsvenc.c
@@ -59,11 +59,11 @@  typedef struct FlashSVContext {
     uint8_t        *previous_frame;
     int             image_width, image_height;
     int             block_width, block_height;
-    uint8_t        *tmpblock;
     uint8_t        *encbuffer;
     int             block_size;
     z_stream        zstream;
     int             last_key_frame;
+    uint8_t         tmpblock[3 * 256 * 256];
 } FlashSVContext;
 
 static int copy_region_enc(uint8_t *sptr, uint8_t *dptr, int dx, int dy,
@@ -96,7 +96,6 @@  static av_cold int flashsv_encode_end(AVCodecContext *avctx)
 
     av_freep(&s->encbuffer);
     av_freep(&s->previous_frame);
-    av_freep(&s->tmpblock);
 
     return 0;
 }
@@ -121,10 +120,9 @@  static av_cold int flashsv_encode_init(AVCodecContext *avctx)
     s->image_width  = avctx->width;
     s->image_height = avctx->height;
 
-    s->tmpblock  = av_mallocz(3 * 256 * 256);
     s->encbuffer = av_mallocz(s->image_width * s->image_height * 3);
 
-    if (!s->tmpblock || !s->encbuffer) {
+    if (!s->encbuffer) {
         av_log(avctx, AV_LOG_ERROR, "Memory allocation failed.\n");
         return AVERROR(ENOMEM);
     }