diff --git a/libavcodec/adxdec.c b/libavcodec/adxdec.c index 4300dede..1dfc4da6 100644 --- a/libavcodec/adxdec.c +++ b/libavcodec/adxdec.c @@ -172,6 +172,7 @@ static int adx_decode_frame(AVCodecContext *avctx, AVFrame *frame, new_extradata = av_packet_get_side_data(avpkt, AV_PKT_DATA_NEW_EXTRADATA, &new_extradata_size); if (new_extradata && new_extradata_size > 0) { + int old_channels = c->channels; int header_size; if ((ret = adx_decode_header(avctx, new_extradata, new_extradata_size, &header_size, @@ -180,6 +181,10 @@ static int adx_decode_frame(AVCodecContext *avctx, AVFrame *frame, return AVERROR_INVALIDDATA; } + c->channels = avctx->ch_layout.nb_channels; + c->header_parsed = 1; + if (old_channels != c->channels) + memset(c->prev, 0, sizeof(c->prev)); c->eof = 0; } diff --git a/libavcodec/av1dec.c b/libavcodec/av1dec.c index 4dcde234..31fba728 100644 --- a/libavcodec/av1dec.c +++ b/libavcodec/av1dec.c @@ -96,12 +96,11 @@ static int32_t decode_signed_subexp_with_ref(uint32_t sub_exp, int low, static void read_global_param(AV1DecContext *s, int type, int ref, int idx) { - uint8_t primary_frame, prev_frame; + int primary_frame; uint32_t abs_bits, prec_bits, round, prec_diff, sub, mx; int32_t r, prev_gm_param; primary_frame = s->raw_frame_header->primary_ref_frame; - prev_frame = s->raw_frame_header->ref_frame_idx[primary_frame]; abs_bits = AV1_GM_ABS_ALPHA_BITS; prec_bits = AV1_GM_ALPHA_PREC_BITS; @@ -111,8 +110,10 @@ static void read_global_param(AV1DecContext *s, int type, int ref, int idx) */ if (s->raw_frame_header->primary_ref_frame == AV1_PRIMARY_REF_NONE) prev_gm_param = s->cur_frame.gm_params[ref][idx]; - else + else { + int prev_frame = s->raw_frame_header->ref_frame_idx[primary_frame]; prev_gm_param = s->ref[prev_frame].gm_params[ref][idx]; + } if (idx < 2) { if (type == AV1_WARP_MODEL_TRANSLATION) { diff --git a/libavcodec/exr.c b/libavcodec/exr.c index b30d3b74..26544017 100644 --- a/libavcodec/exr.c +++ b/libavcodec/exr.c @@ -987,6 +987,7 @@ static int dwa_uncompress(const EXRContext *s, const uint8_t *src, int compresse const int dc_h = td->ysize >> 3; GetByteContext gb, agb; int skip, ret; + int have_rle = 0; if (compressed_size <= 88) return AVERROR_INVALIDDATA; @@ -1011,6 +1012,11 @@ static int dwa_uncompress(const EXRContext *s, const uint8_t *src, int compresse ) return AVERROR_INVALIDDATA; + if ((uint64_t)rle_raw_size > INT_MAX) { + avpriv_request_sample(s->avctx, "Too big rle_raw_size"); + return AVERROR_INVALIDDATA; + } + bytestream2_init(&gb, src + 88, compressed_size - 88); skip = bytestream2_get_le16(&gb); if (skip < 2) @@ -1081,6 +1087,9 @@ static int dwa_uncompress(const EXRContext *s, const uint8_t *src, int compresse if (rle_raw_size > 0 && rle_csize > 0 && rle_usize > 0) { unsigned long dest_len = rle_usize; + if (2LL * td->xsize * td->ysize > rle_raw_size) + return AVERROR_INVALIDDATA; + av_fast_padded_malloc(&td->rle_data, &td->rle_size, rle_usize); if (!td->rle_data) return AVERROR(ENOMEM); @@ -1097,6 +1106,8 @@ static int dwa_uncompress(const EXRContext *s, const uint8_t *src, int compresse if (ret < 0) return ret; bytestream2_skip(&gb, rle_csize); + + have_rle = 1; } bytestream2_init(&agb, td->ac_data, ac_count * 2); @@ -1152,7 +1163,7 @@ static int dwa_uncompress(const EXRContext *s, const uint8_t *src, int compresse if (s->nb_channels < 4) return 0; - for (int y = 0; y < td->ysize && td->rle_raw_data; y++) { + for (int y = 0; y < td->ysize && have_rle; y++) { uint32_t *ao = ((uint32_t *)td->uncompressed_data) + y * td->xsize * s->nb_channels; uint8_t *ai0 = td->rle_raw_data + y * td->xsize; uint8_t *ai1 = td->rle_raw_data + y * td->xsize + rle_raw_size / 2; diff --git a/libavcodec/nvdec.c b/libavcodec/nvdec.c index 0ec8e896..a84d08c3 100644 --- a/libavcodec/nvdec.c +++ b/libavcodec/nvdec.c @@ -614,8 +614,7 @@ int ff_nvdec_start_frame_sep_ref(AVCodecContext *avctx, AVFrame *frame, int has_ cf->ref_idx_ref = av_buffer_pool_get(ctx->decoder_pool); if (!cf->ref_idx_ref) { av_log(avctx, AV_LOG_ERROR, "No decoder surfaces left\n"); - ret = AVERROR(ENOMEM); - goto fail; + return AVERROR(ENOMEM); } } cf->ref_idx = *(unsigned int*)cf->ref_idx_ref->data; @@ -625,9 +624,6 @@ int ff_nvdec_start_frame_sep_ref(AVCodecContext *avctx, AVFrame *frame, int has_ } return 0; -fail: - nvdec_fdd_priv_free(cf); - return ret; } int ff_nvdec_end_frame(AVCodecContext *avctx) diff --git a/libavcodec/rscc.c b/libavcodec/rscc.c index ace9aeeb..f2383e38 100644 --- a/libavcodec/rscc.c +++ b/libavcodec/rscc.c @@ -310,6 +310,12 @@ static int rscc_decode_frame(AVCodecContext *avctx, AVFrame *frame, ret = AVERROR_UNKNOWN; goto end; } + if (len < pixel_size) { + av_log(avctx, AV_LOG_WARNING, "Deflated %lu bytes, but %d are needed\n", + len, pixel_size); + memset(ctx->inflated_buf + len, 0, pixel_size - len); + pixel_size = len; + } pixels = ctx->inflated_buf; } diff --git a/libavcodec/screenpresso.c b/libavcodec/screenpresso.c index b2715499..5864253d 100644 --- a/libavcodec/screenpresso.c +++ b/libavcodec/screenpresso.c @@ -137,6 +137,9 @@ static int screenpresso_decode_frame(AVCodecContext *avctx, AVFrame *frame, return AVERROR_INVALIDDATA; } + /* Codec has aligned strides */ + src_linesize = FFALIGN(avctx->width * component_size, 4); + /* Inflate the frame after the 2 byte header */ ret = uncompress(ctx->inflated_buf, &length, avpkt->data + 2, avpkt->size - 2); @@ -144,14 +147,16 @@ static int screenpresso_decode_frame(AVCodecContext *avctx, AVFrame *frame, av_log(avctx, AV_LOG_ERROR, "Deflate error %d.\n", ret); return AVERROR_UNKNOWN; } + if (length < src_linesize * avctx->height) { + av_log(avctx, AV_LOG_ERROR, "Deflated %lu bytes, but %d are needed\n", + length, src_linesize * avctx->height); + return AVERROR_INVALIDDATA; + } ret = ff_reget_buffer(avctx, ctx->current, 0); if (ret < 0) return ret; - /* Codec has aligned strides */ - src_linesize = FFALIGN(avctx->width * component_size, 4); - /* When a keyframe is found, copy it (flipped) */ if (keyframe) av_image_copy_plane(ctx->current->data[0] + diff --git a/libavcodec/tdsc.c b/libavcodec/tdsc.c index 739738d9..e29eb55c 100644 --- a/libavcodec/tdsc.c +++ b/libavcodec/tdsc.c @@ -242,7 +242,6 @@ static int tdsc_load_cursor(AVCodecContext *avctx) bits <<= 1; } } - dst += ctx->cursor_stride - ctx->cursor_w * 4; } dst = ctx->cursor; @@ -274,7 +273,6 @@ static int tdsc_load_cursor(AVCodecContext *avctx) bits <<= 1; } } - dst += ctx->cursor_stride - ctx->cursor_w * 4; } break; case CUR_FMT_BGRA: diff --git a/libavcodec/tiff.c b/libavcodec/tiff.c index adb49e45..a4df893a 100644 --- a/libavcodec/tiff.c +++ b/libavcodec/tiff.c @@ -525,6 +525,7 @@ static int tiff_unpack_zlib(TiffContext *s, AVFrame *p, uint8_t *dst, int stride uint8_t *zbuf; unsigned long outlen; int ret, line; + int rows = is_yuv ? (lines + s->subsampling[1] - 1) / s->subsampling[1] : lines; outlen = width * lines; zbuf = av_malloc(outlen); if (!zbuf) @@ -544,6 +545,12 @@ static int tiff_unpack_zlib(TiffContext *s, AVFrame *p, uint8_t *dst, int stride av_free(zbuf); return AVERROR_UNKNOWN; } + if (outlen < (unsigned long)width * rows) { + av_log(s->avctx, AV_LOG_ERROR, "Deflated %lu bytes, but %lu are needed\n", + outlen, (unsigned long)width * rows); + av_free(zbuf); + return AVERROR_INVALIDDATA; + } src = zbuf; for (line = 0; line < lines; line++) { if (s->bpp < 8 && s->avctx->pix_fmt == AV_PIX_FMT_PAL8) { @@ -591,6 +598,7 @@ static int tiff_unpack_lzma(TiffContext *s, AVFrame *p, uint8_t *dst, int stride { uint64_t outlen = width * (uint64_t)lines; int ret, line; + int rows = is_yuv ? (lines + s->subsampling[1] - 1) / s->subsampling[1] : lines; uint8_t *buf = av_malloc(outlen); if (!buf) return AVERROR(ENOMEM); @@ -609,6 +617,12 @@ static int tiff_unpack_lzma(TiffContext *s, AVFrame *p, uint8_t *dst, int stride av_free(buf); return AVERROR_UNKNOWN; } + if (outlen < (uint64_t)width * rows) { + av_log(s->avctx, AV_LOG_ERROR, "Uncompressed %"PRIu64" bytes, but %"PRIu64" are needed\n", + outlen, (uint64_t)width * rows); + av_free(buf); + return AVERROR_INVALIDDATA; + } src = buf; for (line = 0; line < lines; line++) { if (s->bpp < 8 && s->avctx->pix_fmt == AV_PIX_FMT_PAL8) { diff --git a/libavfilter/vf_scale.c b/libavfilter/vf_scale.c index 23335cef..ef8d00d7 100644 --- a/libavfilter/vf_scale.c +++ b/libavfilter/vf_scale.c @@ -535,8 +535,8 @@ static int config_props(AVFilterLink *outlink) if (outlink->w > INT_MAX || outlink->h > INT_MAX || - (outlink->h * inlink->w) > INT_MAX || - (outlink->w * inlink->h) > INT_MAX) + (outlink->h * (uint64_t)inlink->w) > INT_MAX || + (outlink->w * (uint64_t)inlink->h) > INT_MAX) av_log(ctx, AV_LOG_ERROR, "Rescaled value for width or height is too big.\n"); /* TODO: make algorithm configurable */ diff --git a/libavformat/dashdec.c b/libavformat/dashdec.c index 1215407f..03d5178b 100644 --- a/libavformat/dashdec.c +++ b/libavformat/dashdec.c @@ -1470,8 +1470,11 @@ static void move_segments(struct representation *rep_src, struct representation free_fragment_list(rep_dest); if (rep_src->start_number > (rep_dest->start_number + rep_dest->n_fragments)) rep_dest->cur_seq_no = 0; - else + else { rep_dest->cur_seq_no += rep_src->start_number - rep_dest->start_number; + if (rep_dest->cur_seq_no < 0) + rep_dest->cur_seq_no = 0; + } rep_dest->fragments = rep_src->fragments; rep_dest->n_fragments = rep_src->n_fragments; rep_dest->parent = rep_src->parent; @@ -1590,7 +1593,7 @@ static struct fragment *get_current_fragment(struct representation *pls) DASHContext *c = pls->parent->priv_data; while (( !ff_check_interrupt(c->interrupt_callback)&& pls->n_fragments > 0)) { - if (pls->cur_seq_no < pls->n_fragments) { + if (pls->cur_seq_no >= 0 && pls->cur_seq_no < pls->n_fragments) { seg_ptr = pls->fragments[pls->cur_seq_no]; seg = av_mallocz(sizeof(struct fragment)); if (!seg) { diff --git a/libavformat/mov.c b/libavformat/mov.c index 606fe22f..b0a42cb1 100644 --- a/libavformat/mov.c +++ b/libavformat/mov.c @@ -7155,7 +7155,7 @@ static int cenc_scheme_decrypt(MOVContext *c, MOVStreamContext *sc, AVEncryption } for (i = 0; i < sample->subsample_count; i++) { - if (sample->subsamples[i].bytes_of_clear_data + sample->subsamples[i].bytes_of_protected_data > size) { + if (sample->subsamples[i].bytes_of_clear_data + (int64_t)sample->subsamples[i].bytes_of_protected_data > size) { av_log(c->fc, AV_LOG_ERROR, "subsample size exceeds the packet size left\n"); return AVERROR_INVALIDDATA; } @@ -7210,7 +7210,7 @@ static int cbc1_scheme_decrypt(MOVContext *c, MOVStreamContext *sc, AVEncryption } for (i = 0; i < sample->subsample_count; i++) { - if (sample->subsamples[i].bytes_of_clear_data + sample->subsamples[i].bytes_of_protected_data > size) { + if (sample->subsamples[i].bytes_of_clear_data + (int64_t)sample->subsamples[i].bytes_of_protected_data > size) { av_log(c->fc, AV_LOG_ERROR, "subsample size exceeds the packet size left\n"); return AVERROR_INVALIDDATA; } @@ -7272,7 +7272,7 @@ static int cens_scheme_decrypt(MOVContext *c, MOVStreamContext *sc, AVEncryption } for (i = 0; i < sample->subsample_count; i++) { - if (sample->subsamples[i].bytes_of_clear_data + sample->subsamples[i].bytes_of_protected_data > size) { + if (sample->subsamples[i].bytes_of_clear_data + (int64_t)sample->subsamples[i].bytes_of_protected_data > size) { av_log(c->fc, AV_LOG_ERROR, "subsample size exceeds the packet size left\n"); return AVERROR_INVALIDDATA; } @@ -7337,7 +7337,7 @@ static int cbcs_scheme_decrypt(MOVContext *c, MOVStreamContext *sc, AVEncryption } for (i = 0; i < sample->subsample_count; i++) { - if (sample->subsamples[i].bytes_of_clear_data + sample->subsamples[i].bytes_of_protected_data > size) { + if (sample->subsamples[i].bytes_of_clear_data + (int64_t)sample->subsamples[i].bytes_of_protected_data > size) { av_log(c->fc, AV_LOG_ERROR, "subsample size exceeds the packet size left\n"); return AVERROR_INVALIDDATA; } diff --git a/libavformat/mpeg.c b/libavformat/mpeg.c index 781c3162..cc7ecbd6 100644 --- a/libavformat/mpeg.c +++ b/libavformat/mpeg.c @@ -829,6 +829,20 @@ static int vobsub_read_header(AVFormatContext *s) } if (!st || st->id != stream_id) { + st = NULL; + for (i = 0; i < s->nb_streams; i++) { + if (s->streams[i]->id == stream_id) { + st = s->streams[i]; + break; + } + } + } + if (!st) { + if (s->nb_streams >= FF_ARRAY_ELEMS(vobsub->q)) { + av_log(s, AV_LOG_ERROR, "Maximum number of subtitle streams reached\n"); + ret = AVERROR_INVALIDDATA; + goto end; + } st = avformat_new_stream(s, NULL); if (!st) { ret = AVERROR(ENOMEM); @@ -853,14 +867,14 @@ static int vobsub_read_header(AVFormatContext *s) timestamp = (hh*3600LL + mm*60LL + ss) * 1000LL + ms + delay; timestamp = av_rescale_q(timestamp, av_make_q(1, 1000), st->time_base); - sub = ff_subtitles_queue_insert(&vobsub->q[s->nb_streams - 1], "", 0, 0); + sub = ff_subtitles_queue_insert(&vobsub->q[st->index], "", 0, 0); if (!sub) { ret = AVERROR(ENOMEM); goto end; } sub->pos = pos; sub->pts = timestamp; - sub->stream_index = s->nb_streams - 1; + sub->stream_index = st->index; } else if (!strncmp(line, "alt:", 4)) { const char *p = line + 4; diff --git a/libavformat/mpegenc.c b/libavformat/mpegenc.c index 12d9fb33..2c25abb8 100644 --- a/libavformat/mpegenc.c +++ b/libavformat/mpegenc.c @@ -471,6 +471,16 @@ static av_cold int mpeg_mux_init(AVFormatContext *ctx) if (!stream->fifo) return AVERROR(ENOMEM); } + + /* The system header is emitted, right after the pack header (which is at + * most 14 bytes), into the fixed 128-byte buffer used by flush_packet(). + * Reject configurations whose system header would not fit. */ + if (get_system_header_size(ctx) > 128 - 14) { + av_log(ctx, AV_LOG_ERROR, + "Too many streams to fit the MPEG program stream system header\n"); + return AVERROR(EINVAL); + } + bitrate = 0; audio_bitrate = 0; video_bitrate = 0; diff --git a/libavformat/rtpdec_asf.c b/libavformat/rtpdec_asf.c index 20ca8295..7e4b6eab 100644 --- a/libavformat/rtpdec_asf.c +++ b/libavformat/rtpdec_asf.c @@ -55,6 +55,8 @@ static int rtp_asf_fix_header(uint8_t *buf, int len) uint64_t chunksize = AV_RL64(p + sizeof(ff_asf_guid)); int skip = 6 * 8 + 3 * 4 + sizeof(ff_asf_guid) * 2; if (memcmp(p, ff_asf_file_header, sizeof(ff_asf_guid))) { + if (chunksize < sizeof(ff_asf_guid) + 8) + return -1; if (chunksize > end - p) return -1; p += chunksize; diff --git a/libavformat/spdifenc.c b/libavformat/spdifenc.c index ca386746..52df9437 100644 --- a/libavformat/spdifenc.c +++ b/libavformat/spdifenc.c @@ -224,7 +224,7 @@ static int spdif_header_dts4(AVFormatContext *s, AVPacket *pkt, int core_size, * (dtshd_fallback == 0) */ ctx->dtshd_skip = 1; } - if (ctx->dtshd_skip && core_size) { + if (ctx->dtshd_skip && core_size && core_size <= pkt->size) { pkt_size = core_size; if (ctx->dtshd_fallback >= 0) --ctx->dtshd_skip; diff --git a/libswscale/output.c b/libswscale/output.c index 8849a320..4dd0d5a1 100644 --- a/libswscale/output.c +++ b/libswscale/output.c @@ -1203,7 +1203,7 @@ yuv2rgba64_1_c_template(SwsContext *c, const int32_t *buf0, { const int32_t *ubuf0 = ubuf[0], *vbuf0 = vbuf[0]; int i; - int A1 = 0xffff<<14, A2= 0xffff<<14; + SUINT A1 = 0xffff<<14, A2= 0xffff<<14; if (uvalpha < 2048) { for (i = 0; i < ((dstW + 1) >> 1); i++) { @@ -1221,8 +1221,8 @@ yuv2rgba64_1_c_template(SwsContext *c, const int32_t *buf0, Y2 += (1 << 13) - (1 << 29); if (hasAlpha) { - A1 = abuf0[i * 2 ] << 11; - A2 = abuf0[i * 2 + 1] << 11; + A1 = abuf0[i * 2 ] * (SUINT)(1 << 11); + A2 = abuf0[i * 2 + 1] * (SUINT)(1 << 11); A1 += 1 << 13; A2 += 1 << 13; diff --git a/libswscale/utils.c b/libswscale/utils.c index e1ad6859..e516341e 100644 --- a/libswscale/utils.c +++ b/libswscale/utils.c @@ -1375,8 +1375,8 @@ static av_cold int sws_init_single_context(SwsContext *c, SwsFilter *srcFilter, if (!srcFilter) srcFilter = &dummyFilter; - c->lumXInc = (((int64_t)srcW << 16) + (dstW >> 1)) / dstW; - c->lumYInc = (((int64_t)srcH << 16) + (dstH >> 1)) / dstH; + int64_t lumXInc = (((int64_t)srcW << 16) + (dstW >> 1)) / dstW; + int64_t lumYInc = (((int64_t)srcH << 16) + (dstH >> 1)) / dstH; c->dstFormatBpp = av_get_bits_per_pixel(desc_dst); c->srcFormatBpp = av_get_bits_per_pixel(desc_src); c->vRounder = 4 * 0x0001000100010001ULL; @@ -1547,8 +1547,8 @@ static av_cold int sws_init_single_context(SwsContext *c, SwsFilter *srcFilter, } else c->canMMXEXTBeUsed = 0; - c->chrXInc = (((int64_t)c->chrSrcW << 16) + (c->chrDstW >> 1)) / c->chrDstW; - c->chrYInc = (((int64_t)c->chrSrcH << 16) + (c->chrDstH >> 1)) / c->chrDstH; + int64_t chrXInc = (((int64_t)c->chrSrcW << 16) + (c->chrDstW >> 1)) / c->chrDstW; + int64_t chrYInc = (((int64_t)c->chrSrcH << 16) + (c->chrDstH >> 1)) / c->chrDstH; /* Match pixel 0 of the src to pixel 0 of dst and match pixel n-2 of src * to pixel n-2 of dst, but only for the FAST_BILINEAR mode otherwise do @@ -1559,15 +1559,26 @@ static av_cold int sws_init_single_context(SwsContext *c, SwsFilter *srcFilter, * some special code for the first and last pixel */ if (flags & SWS_FAST_BILINEAR) { if (c->canMMXEXTBeUsed) { - c->lumXInc += 20; - c->chrXInc += 20; + lumXInc += 20; + chrXInc += 20; } // we don't use the x86 asm scaler if MMX is available else if (INLINE_MMX(cpu_flags) && c->dstBpc <= 14) { - c->lumXInc = ((int64_t)(srcW - 2) << 16) / (dstW - 2) - 20; - c->chrXInc = ((int64_t)(c->chrSrcW - 2) << 16) / (c->chrDstW - 2) - 20; + lumXInc = ((int64_t)(srcW - 2) << 16) / (dstW - 2) - 20; + chrXInc = ((int64_t)(c->chrSrcW - 2) << 16) / (c->chrDstW - 2) - 20; } } + if (chrXInc < 10 || chrXInc > INT_MAX || + chrYInc < 10 || chrYInc > INT_MAX || + lumXInc < 10 || lumXInc > INT_MAX || + lumYInc < 10 || lumYInc > INT_MAX) + return AVERROR_PATCHWELCOME; + + c->lumXInc = lumXInc; + c->lumYInc = lumYInc; + c->chrXInc = chrXInc; + c->chrYInc = chrYInc; + // hardcoded for now c->gamma_value = 2.2;