From patchwork Sun Dec 17 22:35:25 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Niedermayer X-Patchwork-Id: 6842 Delivered-To: ffmpegpatchwork@gmail.com Received: by 10.2.161.94 with SMTP id m30csp2024026jah; Sun, 17 Dec 2017 14:35:51 -0800 (PST) X-Google-Smtp-Source: ACJfBot2K3r3wFaioQDwFJDjikCsO0J+7QMLe68DdFj9VYcWMiNKXaNMw5Q8YY3GcV/kBtG9NVrF X-Received: by 10.28.190.12 with SMTP id o12mr10226359wmf.148.1513550151584; Sun, 17 Dec 2017 14:35:51 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1513550151; cv=none; d=google.com; s=arc-20160816; b=q5cmFO8l9GCu0e8kKGZxLS8Hoy+p+uyQT7Oit9Nm4QKui3gIAspxbqJArY/Z/9eI3W 2l56M8zc6oTFVhj8W1ewL6RX5qrwpSt5G2ZyO0inJgFpgtFbl9bRTq3/VDdFbvgJMIbm PoVXAE2v01RxoGD1LXB3M5SU3akOwRhP98TxZwraVDuwXA9SBuzoNtm5gn3jzG/FJb3i u0XdWSXbUGGewFfDJOaCyF0SMcS6JlpO98Q/8EQA8PdCapNLa8pU0320zhGrxe1WgtFI yNaiC2DptSKVeMAFXBJfY7fT9wMbjYvFYwduMLpwWbEZBlToerjUycKBEj9vZJZN+2oI eBjg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:mime-version:reply-to :list-subscribe:list-help:list-post:list-archive:list-unsubscribe :list-id:precedence:subject:message-id:date:to:from:delivered-to :arc-authentication-results; bh=C+ITCL2xeYlHV8WtZIUsn9Ci+G1ZcKJCdxH7uf4wEJ8=; b=ps/AlMcVk5HA7jwWgE7GJcIEtzdTLy4xfCS0Fi3TlbpDtD72RLz6sNSAgq39oJ2Yy/ hFBfwH5tyEvJZk7MdrVxBHAcM5AQsvARRwSrrmc5becrW+oqfvYer9nddhhN77D8idAV CMTFHaEKwV0yyYT7NainnIVE18xj9BtRKN1Sr1UBWaaRtpN/kxGGAfP/8n9w9ynVtnd0 mV5ZJoafqkDVJ7iGgBtDhaeUqSWibvplJmyS+4yRQ5iuLKk/w3FkfcIclNT4IEy1OuE6 cemWO+r6RsSoFRZfBvjPBBaHsx2zOgGF1hbxuyXADBudcaQr52fEKMU3nIAiOPWT9mwB wi3g== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id h26si8970782wrf.158.2017.12.17.14.35.51; Sun, 17 Dec 2017 14:35:51 -0800 (PST) Received-SPF: pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) client-ip=79.124.17.100; Authentication-Results: mx.google.com; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 9AFB2689B06; Mon, 18 Dec 2017 00:35:40 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from vie01a-qmta-pe02-2.mx.upcmail.net (vie01a-qmta-pe02-2.mx.upcmail.net [62.179.121.182]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 8A4B8680463 for ; Mon, 18 Dec 2017 00:35:33 +0200 (EET) Received: from [172.31.218.51] (helo=vie01a-dmta-pe07-3.mx.upcmail.net) by vie01a-pqmta-pe02.mx.upcmail.net with esmtp (Exim 4.88) (envelope-from ) id 1eQhX5-0008QM-36 for ffmpeg-devel@ffmpeg.org; Sun, 17 Dec 2017 23:35:43 +0100 Received: from [172.31.216.43] (helo=vie01a-pemc-psmtp-pe01) by vie01a-dmta-pe07.mx.upcmail.net with esmtp (Exim 4.88) (envelope-from ) id 1eQhWy-0008Vy-VM for ffmpeg-devel@ffmpeg.org; Sun, 17 Dec 2017 23:35:36 +0100 Received: from localhost ([213.47.41.20]) by vie01a-pemc-psmtp-pe01 with SMTP @ mailcloud.upcmail.net id nAbU1w01B0S5wYM01AbVqN; Sun, 17 Dec 2017 23:35:29 +0100 X-SourceIP: 213.47.41.20 From: Michael Niedermayer To: FFmpeg development discussions and patches Date: Sun, 17 Dec 2017 23:35:25 +0100 Message-Id: <20171217223526.20983-1-michael@niedermayer.cc> X-Mailer: git-send-email 2.15.1 Subject: [FFmpeg-devel] [PATCH 1/2] avcodec/jpeg2000dec: Allocate lengthinc and data_start arrays as needed X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches MIME-Version: 1.0 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Decreases memory requirements Fixes: OOM Fixes: 4525/clusterfuzz-testcase-minimized-6400713073623040 Found-by: continuous fuzzing process https://github.com/google/oss-fuzz/tree/master/projects/ffmpeg Signed-off-by: Michael Niedermayer --- libavcodec/jpeg2000.c | 3 ++- libavcodec/jpeg2000.h | 4 ++-- libavcodec/jpeg2000dec.c | 7 +++++++ 3 files changed, 11 insertions(+), 3 deletions(-) diff --git a/libavcodec/jpeg2000.c b/libavcodec/jpeg2000.c index 5f3965047f..e7f03bd0df 100644 --- a/libavcodec/jpeg2000.c +++ b/libavcodec/jpeg2000.c @@ -359,7 +359,6 @@ static int init_prec(Jpeg2000Band *band, cblk->lblock = 3; cblk->length = 0; - memset(cblk->lengthinc, 0, sizeof(cblk->lengthinc)); cblk->npasses = 0; } @@ -607,6 +606,8 @@ void ff_jpeg2000_cleanup(Jpeg2000Component *comp, Jpeg2000CodingStyle *codsty) Jpeg2000Cblk *cblk = &prec->cblk[cblkno]; av_freep(&cblk->data); av_freep(&cblk->passes); + av_freep(&cblk->lengthinc); + av_freep(&cblk->data_start); } av_freep(&prec->cblk); } diff --git a/libavcodec/jpeg2000.h b/libavcodec/jpeg2000.h index 752feae96b..c429ca5996 100644 --- a/libavcodec/jpeg2000.h +++ b/libavcodec/jpeg2000.h @@ -165,14 +165,14 @@ typedef struct Jpeg2000Cblk { uint8_t ninclpasses; // number coding of passes included in codestream uint8_t nonzerobits; uint16_t length; - uint16_t lengthinc[JPEG2000_MAX_PASSES]; + uint16_t *lengthinc; uint8_t nb_lengthinc; uint8_t lblock; uint8_t *data; size_t data_allocated; int nb_terminations; int nb_terminationsinc; - int data_start[JPEG2000_MAX_PASSES]; + int *data_start; Jpeg2000Pass *passes; int coord[2][2]; // border coordinates {{x0, x1}, {y0, y1}} } Jpeg2000Cblk; // code block diff --git a/libavcodec/jpeg2000dec.c b/libavcodec/jpeg2000dec.c index 0309b1f6fb..a9633fb9d1 100644 --- a/libavcodec/jpeg2000dec.c +++ b/libavcodec/jpeg2000dec.c @@ -949,6 +949,7 @@ static int jpeg2000_decode_packet(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile, for (cblkno = 0; cblkno < nb_code_blocks; cblkno++) { Jpeg2000Cblk *cblk = prec->cblk + cblkno; int incl, newpasses, llen; + void *tmp; if (cblk->npasses) incl = get_bits(s, 1); @@ -988,6 +989,12 @@ static int jpeg2000_decode_packet(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile, cblk->nb_lengthinc = 0; cblk->nb_terminationsinc = 0; + av_free(cblk->lengthinc); + cblk->lengthinc = av_mallocz_array(newpasses , sizeof(*cblk->lengthinc)); + tmp = av_realloc_array(cblk->data_start, cblk->nb_terminations + newpasses + 1, sizeof(*cblk->data_start)); + if (!tmp || !cblk->lengthinc) + return AVERROR(ENOMEM); + cblk->data_start = tmp; do { int newpasses1 = 0;