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