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
282 int64_t off;282 int64_t off;
283 int val_1;283 int val_1;
284 int num_video;284 int num_video;
285 AVIOContext *pb;285 AVIOContext pb0, *pb = &pb0;
286 286
287 pb = avio_alloc_context(buf, size, 0, NULL, NULL, NULL, NULL);287 ffio_init_context(pb, buf, size, 0, NULL, NULL, NULL, NULL);
288 if (!pb)
289 return AVERROR(ENOMEM);
290 288
291 ffio_read_varlen(pb); // track_header_len289 ffio_read_varlen(pb); // track_header_len
292 avio_r8(pb); // '1'290 avio_r8(pb); // '1'
@@ -383,7 +381,6 @@ static int track_header(VividasDemuxContext *viv, AVFormatContext *s, uint8_t * · 90 unmodified lines
383 for (j = 0; j < num_data; j++) {381 for (j = 0; j < num_data; j++) {
384 uint64_t len = ffio_read_varlen(pb);382 uint64_t len = ffio_read_varlen(pb);
385 if (len > INT_MAX/2 - xd_size) {383 if (len > INT_MAX/2 - xd_size) {
386 av_free(pb);
387 return AVERROR_INVALIDDATA;384 return AVERROR_INVALIDDATA;
388 }385 }
389 data_len[j] = len;386 data_len[j] = len;
@@ -392,7 +389,6 @@ static int track_header(VividasDemuxContext *viv, AVFormatContext *s, uint8_t * · 2 unmodified lines
392 389
393 st->codecpar->extradata_size = 64 + xd_size + xd_size / 255;390 st->codecpar->extradata_size = 64 + xd_size + xd_size / 255;
394 if (ff_alloc_extradata(st->codecpar, st->codecpar->extradata_size)) {391 if (ff_alloc_extradata(st->codecpar, st->codecpar->extradata_size)) {
395 av_free(pb);
396 return AVERROR(ENOMEM);392 return AVERROR(ENOMEM);
397 }393 }
398 394
@@ -402,7 +398,6 @@ static int track_header(VividasDemuxContext *viv, AVFormatContext *s, uint8_t * · 3 unmodified lines
402 for (j = 0; j < num_data - 1; j++) {398 for (j = 0; j < num_data - 1; j++) {
403 unsigned delta = av_xiphlacing(&p[offset], data_len[j]);399 unsigned delta = av_xiphlacing(&p[offset], data_len[j]);
404 if (delta > data_len[j]) {400 if (delta > data_len[j]) {
405 av_free(pb);
406 return AVERROR_INVALIDDATA;401 return AVERROR_INVALIDDATA;
407 }402 }
408 offset += delta;403 offset += delta;
@@ -423,7 +418,6 @@ static int track_header(VividasDemuxContext *viv, AVFormatContext *s, uint8_t * · 14 unmodified lines
423 }418 }
424 }419 }
425 420
426 av_free(pb);
427 return 0;421 return 0;
428}422}
429 423
@@ -432,13 +426,11 @@ static int track_index(VividasDemuxContext *viv, AVFormatContext *s, uint8_t *bu · 2 unmodified lines
432 int64_t off;426 int64_t off;
433 int64_t poff;427 int64_t poff;
434 int maxnp=0;428 int maxnp=0;
435 AVIOContext *pb;429 AVIOContext pb0, *pb = &pb0;
436 int i;430 int i;
437 int64_t filesize = avio_size(s->pb);431 int64_t filesize = avio_size(s->pb);
438 432
439 pb = avio_alloc_context(buf, size, 0, NULL, NULL, NULL, NULL);433 ffio_init_context(pb, buf, size, 0, NULL, NULL, NULL, NULL);
440 if (!pb)
441 return AVERROR(ENOMEM);
442 434
443 ffio_read_varlen(pb); // track_index_len435 ffio_read_varlen(pb); // track_index_len
444 avio_r8(pb); // 'c'436 avio_r8(pb); // 'c'
@@ -448,7 +440,6 @@ static int track_index(VividasDemuxContext *viv, AVFormatContext *s, uint8_t *bu · 3 unmodified lines
448 viv->sb_blocks = av_calloc(viv->n_sb_blocks, sizeof(VIV_SB_block));440 viv->sb_blocks = av_calloc(viv->n_sb_blocks, sizeof(VIV_SB_block));
449 if (!viv->sb_blocks) {441 if (!viv->sb_blocks) {
450 viv->n_sb_blocks = 0;442 viv->n_sb_blocks = 0;
451 av_free(pb);
452 return AVERROR(ENOMEM);443 return AVERROR(ENOMEM);
453 }444 }
454 445
@@ -479,11 +470,9 @@ static int track_index(VividasDemuxContext *viv, AVFormatContext *s, uint8_t *bu · 24 unmodified lines
479 goto error;470 goto error;
480 471
481 viv->sb_entries = av_calloc(maxnp, sizeof(VIV_SB_entry));472 viv->sb_entries = av_calloc(maxnp, sizeof(VIV_SB_entry));
482 av_free(pb);
483 473
484 return 0;474 return 0;
485error:475error:
486 av_free(pb);
487 viv->n_sb_blocks = 0;476 viv->n_sb_blocks = 0;
488 av_freep(&viv->sb_blocks);477 av_freep(&viv->sb_blocks);
489 return AVERROR_INVALIDDATA;478 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