diff gui/util/bitmap.c @ 33135:36fdc061b4a8

Prevent segmentation faults. Check alloc pointers. Besides, issue debug messages.
author ib
date Tue, 05 Apr 2011 13:12:36 +0000
parents ffe524b8b8f3
children 07be88814256
line wrap: on
line diff
--- a/gui/util/bitmap.c	Tue Apr 05 12:50:56 2011 +0000
+++ b/gui/util/bitmap.c	Tue Apr 05 13:12:36 2011 +0000
@@ -34,7 +34,7 @@
     FILE *file;
     long len;
     void *data;
-    int decode_ok;
+    int decode_ok, bpl;
     AVCodecContext *avctx;
     AVFrame *frame;
     AVPacket pkt;
@@ -57,6 +57,12 @@
 
     data = av_malloc(len + FF_INPUT_BUFFER_PADDING_SIZE);
 
+    if (!data) {
+        fclose(file);
+        mp_dbg(MSGT_GPLAYER, MSGL_DBG2, "[bitmap] not enough memory: %lu\n", len + FF_INPUT_BUFFER_PADDING_SIZE);
+        return 3;
+    }
+
     fseek(file, 0, SEEK_SET);
     fread(data, len, 1, file);
     fclose(file);
@@ -98,14 +104,14 @@
     }
 
     if (decode_ok && bf->BPP) {
-        int bpl;
-
         bf->Width  = avctx->width;
         bf->Height = avctx->height;
         bpl = bf->Width * (bf->BPP / 8);
         bf->ImageSize = bpl * bf->Height;
         bf->Image     = malloc(bf->ImageSize);
-        memcpy_pic(bf->Image, frame->data[0], bpl, bf->Height, bpl, frame->linesize[0]);
+
+        if (bf->Image)
+            memcpy_pic(bf->Image, frame->data[0], bpl, bf->Height, bpl, frame->linesize[0]);
     }
 
     avcodec_close(avctx);
@@ -113,6 +119,11 @@
     av_freep(&avctx);
     av_freep(&data);
 
+    if (decode_ok && bf->BPP && !bf->Image) {
+        mp_dbg(MSGT_GPLAYER, MSGL_DBG2, "[bitmap] not enough memory: %lu\n", bf->ImageSize);
+        return 4;
+    }
+
     mp_dbg(MSGT_GPLAYER, MSGL_DBG2, "[bitmap] file: %s\n", fname);
     mp_dbg(MSGT_GPLAYER, MSGL_DBG2, "[bitmap]  size: %lux%lu, color depth: %u\n", bf->Width, bf->Height, bf->BPP);
     mp_dbg(MSGT_GPLAYER, MSGL_DBG2, "[bitmap]  image size: %lu\n", bf->ImageSize);