[FFmpeg-devel] [PATCH] Add and use av_fast_mallocz.

Reimar Döffinger Reimar.Doeffinger at gmx.de
Sun Jan 15 14:29:59 CET 2012


The same as av_fast_malloc but uses av_mallocz.
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) any padding is always 0
Fixes another valgrind warning about use of uninitialized data,
this time with fate-vsynth1-jpegls.

Signed-off-by: Reimar Döffinger <Reimar.Doeffinger at gmx.de>
---
 libavcodec/asv1.c     |    3 +--
 libavcodec/avcodec.h  |    9 +++++++++
 libavcodec/mjpegdec.c |   10 +++-------
 libavcodec/utils.c    |   14 ++++++++++++--
 4 files changed, 25 insertions(+), 11 deletions(-)

diff --git a/libavcodec/asv1.c b/libavcodec/asv1.c
index a2519cb..75aa0e1 100644
--- a/libavcodec/asv1.c
+++ b/libavcodec/asv1.c
@@ -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_mallocz(&a->bitstream_buffer, &a->bitstream_buffer_size, buf_size + FF_INPUT_BUFFER_PADDING_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);
diff --git a/libavcodec/avcodec.h b/libavcodec/avcodec.h
index 9a8ca81..a8ca449 100644
--- a/libavcodec/avcodec.h
+++ b/libavcodec/avcodec.h
@@ -4661,6 +4661,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 when a new buffer is allocated it
+ * will be 0'd.
+ *
+ * This mostly serves the purpose of making sure that any padding will
+ * be 0.
+ */
+void av_fast_mallocz(void *ptr, unsigned int *size, size_t min_size);
+
+/**
  * Copy image src to dst. Wraps av_picture_data_copy() above.
  */
 void av_picture_copy(AVPicture *dst, const AVPicture *src,
diff --git a/libavcodec/mjpegdec.c b/libavcodec/mjpegdec.c
index ad78b4c..8b95203 100644
--- a/libavcodec/mjpegdec.c
+++ b/libavcodec/mjpegdec.c
@@ -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_mallocz(&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) {
diff --git a/libavcodec/utils.c b/libavcodec/utils.c
index e25f1df..1a7a6c8 100644
--- a/libavcodec/utils.c
+++ b/libavcodec/utils.c
@@ -68,18 +68,28 @@ 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 void ff_fast_malloc(void *ptr, unsigned int *size, size_t min_size, int zero_realloc)
 {
     void **p = ptr;
     if (min_size < *size)
         return;
     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;
 }
 
+void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size)
+{
+    ff_fast_malloc(ptr, size, min_size, 0);
+}
+
+void av_fast_mallocz(void *ptr, unsigned int *size, size_t min_size)
+{
+    ff_fast_malloc(ptr, size, min_size, 1);
+}
+
 /* encoder management */
 static AVCodec *first_avcodec = NULL;
 
-- 
1.7.8.3



More information about the ffmpeg-devel mailing list