diff mbox

[FFmpeg-devel,1/2] avcodec/pcx: Check for end of input in pcx_rle_decode() and propagate error

Message ID 20180916002738.7513-1-michael@niedermayer.cc
State Accepted
Commit 7ed8d5cfd94a53cca3599d40d2d9d2a8ee6f1bb8
Headers show

Commit Message

Michael Niedermayer Sept. 16, 2018, 12:27 a.m. UTC
Fixes: Timeout
Fixes: 10279/clusterfuzz-testcase-minimized-ffmpeg_AV_CODEC_ID_PCX_fuzzer-5729311395414016

Found-by: continuous fuzzing process https://github.com/google/oss-fuzz/tree/master/projects/ffmpeg
Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>
---
 libavcodec/pcx.c | 22 +++++++++++++++++-----
 1 file changed, 17 insertions(+), 5 deletions(-)

Comments

Michael Niedermayer Sept. 19, 2018, 9:12 p.m. UTC | #1
On Sun, Sep 16, 2018 at 02:27:37AM +0200, Michael Niedermayer wrote:
> Fixes: Timeout
> Fixes: 10279/clusterfuzz-testcase-minimized-ffmpeg_AV_CODEC_ID_PCX_fuzzer-5729311395414016
> 
> Found-by: continuous fuzzing process https://github.com/google/oss-fuzz/tree/master/projects/ffmpeg
> Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>
> ---
>  libavcodec/pcx.c | 22 +++++++++++++++++-----
>  1 file changed, 17 insertions(+), 5 deletions(-)

will apply

[...]
diff mbox

Patch

diff --git a/libavcodec/pcx.c b/libavcodec/pcx.c
index 58a5e1e068..4505db75b8 100644
--- a/libavcodec/pcx.c
+++ b/libavcodec/pcx.c
@@ -30,7 +30,7 @@ 
 
 #define PCX_HEADER_SIZE 128
 
-static void pcx_rle_decode(GetByteContext *gb,
+static int pcx_rle_decode(GetByteContext *gb,
                            uint8_t *dst,
                            unsigned int bytes_per_scanline,
                            int compressed)
@@ -38,6 +38,9 @@  static void pcx_rle_decode(GetByteContext *gb,
     unsigned int i = 0;
     unsigned char run, value;
 
+    if (bytestream2_get_bytes_left(gb) < 1)
+        return AVERROR_INVALIDDATA;
+
     if (compressed) {
         while (i < bytes_per_scanline && bytestream2_get_bytes_left(gb)>0) {
             run   = 1;
@@ -52,6 +55,7 @@  static void pcx_rle_decode(GetByteContext *gb,
     } else {
         bytestream2_get_buffer(gb, dst, bytes_per_scanline);
     }
+    return 0;
 }
 
 static void pcx_palette(GetByteContext *gb, uint32_t *dst, int pallen)
@@ -153,7 +157,9 @@  static int pcx_decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
 
     if (nplanes == 3 && bits_per_pixel == 8) {
         for (y = 0; y < h; y++) {
-            pcx_rle_decode(&gb, scanline, bytes_per_scanline, compressed);
+            ret = pcx_rle_decode(&gb, scanline, bytes_per_scanline, compressed);
+            if (ret < 0)
+                goto end;
 
             for (x = 0; x < w; x++) {
                 ptr[3 * x]     = scanline[x];
@@ -174,7 +180,9 @@  static int pcx_decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
         }
 
         for (y = 0; y < h; y++, ptr += stride) {
-            pcx_rle_decode(&gb, scanline, bytes_per_scanline, compressed);
+            ret = pcx_rle_decode(&gb, scanline, bytes_per_scanline, compressed);
+            if (ret < 0)
+                goto end;
             memcpy(ptr, scanline, w);
         }
 
@@ -194,7 +202,9 @@  static int pcx_decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
         for (y = 0; y < h; y++) {
             init_get_bits8(&s, scanline, bytes_per_scanline);
 
-            pcx_rle_decode(&gb, scanline, bytes_per_scanline, compressed);
+            ret = pcx_rle_decode(&gb, scanline, bytes_per_scanline, compressed);
+            if (ret < 0)
+                goto end;
 
             for (x = 0; x < w; x++)
                 ptr[x] = get_bits(&s, bits_per_pixel);
@@ -204,7 +214,9 @@  static int pcx_decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
         int i;
 
         for (y = 0; y < h; y++) {
-            pcx_rle_decode(&gb, scanline, bytes_per_scanline, compressed);
+            ret = pcx_rle_decode(&gb, scanline, bytes_per_scanline, compressed);
+            if (ret < 0)
+                goto end;
 
             for (x = 0; x < w; x++) {
                 int m = 0x80 >> (x & 7), v = 0;