diff mbox

[FFmpeg-devel] avformat/vividas: Avoid allocation of AVIOContext

Message ID 20191128143133.18393-1-andreas.rheinhardt@gmail.com
State Accepted
Commit ff5c8e57e756e5b80bc32ea469495a9a89c012b6
Headers show

Commit Message

Andreas Rheinhardt Nov. 28, 2019, 2:31 p.m. UTC
Put an AVIOContext whose lifetime doesn't extend beyond the function where
it is allocated on the stack instead of allocating and freeing it. This
also avoids the need to free it, which in this case fixes possible
memleaks on error.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
---
 libavformat/vividas.c | 19 ++++---------------
 1 file changed, 4 insertions(+), 15 deletions(-)

Comments

Michael Niedermayer Nov. 28, 2019, 10:58 p.m. UTC | #1
On Thu, Nov 28, 2019 at 03:31:33PM +0100, Andreas Rheinhardt wrote:
> Put an AVIOContext whose lifetime doesn't extend beyond the function where
> it is allocated on the stack instead of allocating and freeing it. This
> also avoids the need to free it, which in this case fixes possible
> memleaks on error.
> 
> Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
> ---
>  libavformat/vividas.c | 19 ++++---------------
>  1 file changed, 4 insertions(+), 15 deletions(-)

will apply

thx

[...]
diff mbox

Patch

diff --git a/libavformat/vividas.c b/libavformat/vividas.c
index f20af3d7c2..88fa89a3cf 100644
--- a/libavformat/vividas.c
+++ b/libavformat/vividas.c
@@ -282,11 +282,9 @@  static int track_header(VividasDemuxContext *viv, AVFormatContext *s,  uint8_t *
     int64_t off;
     int val_1;
     int num_video;
-    AVIOContext *pb;
+    AVIOContext pb0, *pb = &pb0;
 
-    pb = avio_alloc_context(buf, size, 0, NULL, NULL, NULL, NULL);
-    if (!pb)
-        return AVERROR(ENOMEM);
+    ffio_init_context(pb, buf, size, 0, NULL, NULL, NULL, NULL);
 
     ffio_read_varlen(pb); // track_header_len
     avio_r8(pb); // '1'
@@ -383,7 +381,6 @@  static int track_header(VividasDemuxContext *viv, AVFormatContext *s,  uint8_t *
             for (j = 0; j < num_data; j++) {
                 uint64_t len = ffio_read_varlen(pb);
                 if (len > INT_MAX/2 - xd_size) {
-                    av_free(pb);
                     return AVERROR_INVALIDDATA;
                 }
                 data_len[j] = len;
@@ -392,7 +389,6 @@  static int track_header(VividasDemuxContext *viv, AVFormatContext *s,  uint8_t *
 
             st->codecpar->extradata_size = 64 + xd_size + xd_size / 255;
             if (ff_alloc_extradata(st->codecpar, st->codecpar->extradata_size)) {
-                av_free(pb);
                 return AVERROR(ENOMEM);
             }
 
@@ -402,7 +398,6 @@  static int track_header(VividasDemuxContext *viv, AVFormatContext *s,  uint8_t *
             for (j = 0; j < num_data - 1; j++) {
                 unsigned delta = av_xiphlacing(&p[offset], data_len[j]);
                 if (delta > data_len[j]) {
-                    av_free(pb);
                     return AVERROR_INVALIDDATA;
                 }
                 offset += delta;
@@ -423,7 +418,6 @@  static int track_header(VividasDemuxContext *viv, AVFormatContext *s,  uint8_t *
         }
     }
 
-    av_free(pb);
     return 0;
 }
 
@@ -432,13 +426,11 @@  static int track_index(VividasDemuxContext *viv, AVFormatContext *s, uint8_t *bu
     int64_t off;
     int64_t poff;
     int maxnp=0;
-    AVIOContext *pb;
+    AVIOContext pb0, *pb = &pb0;
     int i;
     int64_t filesize = avio_size(s->pb);
 
-    pb = avio_alloc_context(buf, size, 0, NULL, NULL, NULL, NULL);
-    if (!pb)
-        return AVERROR(ENOMEM);
+    ffio_init_context(pb, buf, size, 0, NULL, NULL, NULL, NULL);
 
     ffio_read_varlen(pb); // track_index_len
     avio_r8(pb); // 'c'
@@ -448,7 +440,6 @@  static int track_index(VividasDemuxContext *viv, AVFormatContext *s, uint8_t *bu
     viv->sb_blocks = av_calloc(viv->n_sb_blocks, sizeof(VIV_SB_block));
     if (!viv->sb_blocks) {
         viv->n_sb_blocks = 0;
-        av_free(pb);
         return AVERROR(ENOMEM);
     }
 
@@ -479,11 +470,9 @@  static int track_index(VividasDemuxContext *viv, AVFormatContext *s, uint8_t *bu
         goto error;
 
     viv->sb_entries = av_calloc(maxnp, sizeof(VIV_SB_entry));
-    av_free(pb);
 
     return 0;
 error:
-    av_free(pb);
     viv->n_sb_blocks = 0;
     av_freep(&viv->sb_blocks);
     return AVERROR_INVALIDDATA;