[FFmpeg-cvslog] avutil/mem: check for max_alloc_size in av_fast_malloc()

James Almer git at videolan.org
Thu May 27 16:31:42 EEST 2021


ffmpeg | branch: master | James Almer <jamrial at gmail.com> | Sun May 23 11:29:04 2021 -0300| [918fc9a0ed4e9202341ffb3e7e4b72b4387dfe0a] | committer: James Almer

avutil/mem: check for max_alloc_size in av_fast_malloc()

This puts av_fast_malloc*() in line with av_fast_realloc().

Signed-off-by: James Almer <jamrial at gmail.com>

> http://git.videolan.org/gitweb.cgi/ffmpeg.git/?a=commit;h=918fc9a0ed4e9202341ffb3e7e4b72b4387dfe0a
---

 libavutil/mem.c | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)

diff --git a/libavutil/mem.c b/libavutil/mem.c
index 3c014f3f68..a52d33d4a6 100644
--- a/libavutil/mem.c
+++ b/libavutil/mem.c
@@ -512,6 +512,7 @@ void *av_fast_realloc(void *ptr, unsigned int *size, size_t min_size)
 
 static inline void fast_malloc(void *ptr, unsigned int *size, size_t min_size, int zero_realloc)
 {
+    size_t max_size;
     void *val;
 
     memcpy(&val, ptr, sizeof(val));
@@ -519,7 +520,15 @@ static inline void fast_malloc(void *ptr, unsigned int *size, size_t min_size, i
         av_assert0(val || !min_size);
         return;
     }
-    min_size = FFMAX(min_size + min_size / 16 + 32, min_size);
+
+    max_size = atomic_load_explicit(&max_alloc_size, memory_order_relaxed);
+
+    if (min_size > max_size) {
+        av_freep(ptr);
+        *size = 0;
+        return;
+    }
+    min_size = FFMIN(max_size, FFMAX(min_size + min_size / 16 + 32, min_size));
     av_freep(ptr);
     val = zero_realloc ? av_mallocz(min_size) : av_malloc(min_size);
     memcpy(ptr, &val, sizeof(val));



More information about the ffmpeg-cvslog mailing list