History

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.

Andreas Rheinhardt committed 6 years ago

Commit ff5c8e51 parent 16fa5131 file · +4 −15Browse files

Unified · Split

libavformat/vividas.c+4−15
@@ -282,11 +282,9 @@ static int track_header(VividasDemuxContext *viv, AVFormatContext *s, uint8_t * · 281 unmodified lines
282282 int64_t off;
283283 int val_1;
284284 int num_video;
285− AVIOContext *pb;
285+ AVIOContext pb0, *pb = &pb0;
286286
287− pb = avio_alloc_context(buf, size, 0, NULL, NULL, NULL, NULL);
288− if (!pb)
289− return AVERROR(ENOMEM);
287+ ffio_init_context(pb, buf, size, 0, NULL, NULL, NULL, NULL);
290288
291289 ffio_read_varlen(pb); // track_header_len
292290 avio_r8(pb); // '1'
@@ -383,7 +381,6 @@ static int track_header(VividasDemuxContext *viv, AVFormatContext *s, uint8_t * · 90 unmodified lines
383381 for (j = 0; j < num_data; j++) {
384382 uint64_t len = ffio_read_varlen(pb);
385383 if (len > INT_MAX/2 - xd_size) {
386− av_free(pb);
387384 return AVERROR_INVALIDDATA;
388385 }
389386 data_len[j] = len;
@@ -392,7 +389,6 @@ static int track_header(VividasDemuxContext *viv, AVFormatContext *s, uint8_t * · 2 unmodified lines
392389
393390 st->codecpar->extradata_size = 64 + xd_size + xd_size / 255;
394391 if (ff_alloc_extradata(st->codecpar, st->codecpar->extradata_size)) {
395− av_free(pb);
396392 return AVERROR(ENOMEM);
397393 }
398394
@@ -402,7 +398,6 @@ static int track_header(VividasDemuxContext *viv, AVFormatContext *s, uint8_t * · 3 unmodified lines
402398 for (j = 0; j < num_data - 1; j++) {
403399 unsigned delta = av_xiphlacing(&p[offset], data_len[j]);
404400 if (delta > data_len[j]) {
405− av_free(pb);
406401 return AVERROR_INVALIDDATA;
407402 }
408403 offset += delta;
@@ -423,7 +418,6 @@ static int track_header(VividasDemuxContext *viv, AVFormatContext *s, uint8_t * · 14 unmodified lines
423418 }
424419 }
425420
426− av_free(pb);
427421 return 0;
428422 }
429423
@@ -432,13 +426,11 @@ static int track_index(VividasDemuxContext *viv, AVFormatContext *s, uint8_t *bu · 2 unmodified lines
432426 int64_t off;
433427 int64_t poff;
434428 int maxnp=0;
435− AVIOContext *pb;
429+ AVIOContext pb0, *pb = &pb0;
436430 int i;
437431 int64_t filesize = avio_size(s->pb);
438432
439− pb = avio_alloc_context(buf, size, 0, NULL, NULL, NULL, NULL);
440− if (!pb)
441− return AVERROR(ENOMEM);
433+ ffio_init_context(pb, buf, size, 0, NULL, NULL, NULL, NULL);
442434
443435 ffio_read_varlen(pb); // track_index_len
444436 avio_r8(pb); // 'c'
@@ -448,7 +440,6 @@ static int track_index(VividasDemuxContext *viv, AVFormatContext *s, uint8_t *bu · 3 unmodified lines
448440 viv->sb_blocks = av_calloc(viv->n_sb_blocks, sizeof(VIV_SB_block));
449441 if (!viv->sb_blocks) {
450442 viv->n_sb_blocks = 0;
451− av_free(pb);
452443 return AVERROR(ENOMEM);
453444 }
454445
@@ -479,11 +470,9 @@ static int track_index(VividasDemuxContext *viv, AVFormatContext *s, uint8_t *bu · 24 unmodified lines
479470 goto error;
480471
481472 viv->sb_entries = av_calloc(maxnp, sizeof(VIV_SB_entry));
482− av_free(pb);
483473
484474 return 0;
485475 error:
486− av_free(pb);
487476 viv->n_sb_blocks = 0;
488477 av_freep(&viv->sb_blocks);
489478 return AVERROR_INVALIDDATA;

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t