[FFmpeg-devel,1/2] avcodec/jpeg2000dec: Allocate lengthinc and data_start arrays as needed

Submitted by Michael Niedermayer on Dec. 17, 2017, 10:35 p.m.

Details

Message ID 20171217223526.20983-1-michael@niedermayer.cc
State New
Headers show

Commit Message

Michael Niedermayer Dec. 17, 2017, 10:35 p.m.
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 <michael@niedermayer.cc>
---
 libavcodec/jpeg2000.c    | 3 ++-
 libavcodec/jpeg2000.h    | 4 ++--
 libavcodec/jpeg2000dec.c | 7 +++++++
 3 files changed, 11 insertions(+), 3 deletions(-)

Comments

Michael Niedermayer Dec. 21, 2017, 1:03 p.m.
On Sun, Dec 17, 2017 at 11:35:25PM +0100, Michael Niedermayer wrote:
> 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 <michael@niedermayer.cc>

applied

[...]

Patch hide | download patch | download mbox

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;