From ff5c8e57e756e5b80bc32ea469495a9a89c012b6 Mon Sep 17 00:00:00 2001 From: Andreas Rheinhardt Date: Thu, 28 Nov 2019 15:31:33 +0100 Subject: avformat/vividas: Avoid allocation of AVIOContext 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 Signed-off-by: Michael Niedermayer --- libavformat/vividas.c | 19 ++++--------------- 1 file changed, 4 insertions(+), 15 deletions(-) (limited to 'libavformat/vividas.c') 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; -- cgit v1.2.3