]> git.sesse.net Git - ffmpeg/commitdiff
Add and use av_fast_padded_malloc.
authorReimar Döffinger <Reimar.Doeffinger@gmx.de>
Sun, 15 Jan 2012 13:26:55 +0000 (14:26 +0100)
committerReimar Döffinger <Reimar.Doeffinger@gmx.de>
Tue, 17 Jan 2012 19:16:35 +0000 (20:16 +0100)
The same as av_fast_malloc but uses av_mallocz and keeps extra
always-0 padding.
This does not mean the memory will be 0-initialized after each call,
but actually only after each growth of the buffer.
However this makes sure that
a) all data anywhere in the buffer is always initialized
b) the padding is always 0
c) the user does not have to bother with adding the padding themselves
Fixes another valgrind warning about use of uninitialized data,
this time with fate-vsynth1-jpegls.

Signed-off-by: Reimar Döffinger <Reimar.Doeffinger@gmx.de>
libavcodec/asv1.c
libavcodec/avcodec.h
libavcodec/eatqi.c
libavcodec/mjpegdec.c
libavcodec/utils.c
libavcodec/version.h

index a2519cb5c2a88431f37cffdffe96a4711519e229..9eeec2675a73924984c6b97355a2afe3c9a15381 100644 (file)
@@ -408,10 +408,9 @@ static int decode_frame(AVCodecContext *avctx,
     p->pict_type= AV_PICTURE_TYPE_I;
     p->key_frame= 1;
 
-    av_fast_malloc(&a->bitstream_buffer, &a->bitstream_buffer_size, buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
+    av_fast_padded_malloc(&a->bitstream_buffer, &a->bitstream_buffer_size, buf_size);
     if (!a->bitstream_buffer)
         return AVERROR(ENOMEM);
-    memset(a->bitstream_buffer + buf_size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
 
     if(avctx->codec_id == CODEC_ID_ASV1)
         a->dsp.bswap_buf((uint32_t*)a->bitstream_buffer, (const uint32_t*)buf, buf_size/4);
index e690c81e12d3430c5bf0d9a90bc2e010a76a2a31..215bfc9bb4e54a045c7b29c254aabe5572ddb14f 100644 (file)
@@ -4753,6 +4753,15 @@ void *av_fast_realloc(void *ptr, unsigned int *size, size_t min_size);
  */
 void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size);
 
+/**
+ * Same behaviour av_fast_malloc but the buffer has additional
+ * FF_INPUT_PADDING_SIZE at the end which will will always be 0.
+ *
+ * In addition the whole buffer will initially and after resizes
+ * be 0-initialized so that no uninitialized data will ever appear.
+ */
+void av_fast_padded_malloc(void *ptr, unsigned int *size, size_t min_size);
+
 /**
  * Copy image src to dst. Wraps av_picture_data_copy() above.
  */
index 245c143c8813922eaf5a1353546621bb1a6d30dd..d650a71713454b7bcd6597215a2957a72501fffd 100644 (file)
@@ -127,7 +127,7 @@ static int tqi_decode_frame(AVCodecContext *avctx,
         return -1;
     }
 
-    av_fast_malloc(&t->bitstream_buf, &t->bitstream_buf_size, (buf_end-buf) + FF_INPUT_BUFFER_PADDING_SIZE);
+    av_fast_padded_malloc(&t->bitstream_buf, &t->bitstream_buf_size, buf_end-buf);
     if (!t->bitstream_buf)
         return AVERROR(ENOMEM);
     s->dsp.bswap_buf(t->bitstream_buf, (const uint32_t*)buf, (buf_end-buf)/4);
index ad78b4cc0dacfc5dad62c29c16abfd295a686ccf..2abfea0d6e42942c5769f1464428e166d700160e 100644 (file)
@@ -1474,13 +1474,9 @@ int ff_mjpeg_find_marker(MJpegDecodeContext *s,
     int start_code;
     start_code = find_marker(buf_ptr, buf_end);
 
-    if ((buf_end - *buf_ptr) > s->buffer_size) {
-        av_free(s->buffer);
-        s->buffer_size = buf_end - *buf_ptr;
-        s->buffer      = av_malloc(s->buffer_size + FF_INPUT_BUFFER_PADDING_SIZE);
-        av_log(s->avctx, AV_LOG_DEBUG,
-               "buffer too small, expanding to %d bytes\n", s->buffer_size);
-    }
+    av_fast_padded_malloc(&s->buffer, &s->buffer_size, buf_end - *buf_ptr);
+    if (!s->buffer)
+        return AVERROR(ENOMEM);
 
     /* unescape buffer of SOS, use special treatment for JPEG-LS */
     if (start_code == SOS && !s->ls) {
index 657eb5b932d4c6c0c946462d6366da1ad1823d8d..ccccd54accb4da3f40ef57a9e4d5e61f9f6fd390 100644 (file)
@@ -69,16 +69,34 @@ void *av_fast_realloc(void *ptr, unsigned int *size, size_t min_size)
     return ptr;
 }
 
-void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size)
+static inline int ff_fast_malloc(void *ptr, unsigned int *size, size_t min_size, int zero_realloc)
 {
     void **p = ptr;
     if (min_size < *size)
-        return;
+        return 0;
     min_size= FFMAX(17*min_size/16 + 32, min_size);
     av_free(*p);
-    *p = av_malloc(min_size);
+    *p = zero_realloc ? av_mallocz(min_size) : av_malloc(min_size);
     if (!*p) min_size = 0;
     *size= min_size;
+    return 1;
+}
+
+void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size)
+{
+    ff_fast_malloc(ptr, size, min_size, 0);
+}
+
+void av_fast_padded_malloc(void *ptr, unsigned int *size, size_t min_size)
+{
+    uint8_t **p = ptr;
+    if (min_size > SIZE_MAX - FF_INPUT_BUFFER_PADDING_SIZE) {
+        *p = NULL;
+        *size = 0;
+        return;
+    }
+    if (!ff_fast_malloc(p, size, min_size + FF_INPUT_BUFFER_PADDING_SIZE, 1))
+        memset(*p + min_size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
 }
 
 /* encoder management */
index f8bb5c69ae819b26fd0438b10a198c00099f940f..24de77c4160b1b06c6c53bb1fdcba609866ae59e 100644 (file)
@@ -21,8 +21,8 @@
 #define AVCODEC_VERSION_H
 
 #define LIBAVCODEC_VERSION_MAJOR 53
-#define LIBAVCODEC_VERSION_MINOR 56
-#define LIBAVCODEC_VERSION_MICRO 105
+#define LIBAVCODEC_VERSION_MINOR 57
+#define LIBAVCODEC_VERSION_MICRO 100
 
 #define LIBAVCODEC_VERSION_INT  AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \
                                                LIBAVCODEC_VERSION_MINOR, \