[FFmpeg-devel] [PATCH 3/3] avutil/mem: check for max_alloc_size in av_fast_malloc()
James Almer
jamrial at gmail.com
Sun May 23 17:54:33 EEST 2021
This puts av_fast_malloc*() in line with av_fast_realloc().
Signed-off-by: James Almer <jamrial at gmail.com>
---
libavutil/mem.c | 11 ++++++++++-
1 file changed, 10 insertions(+), 1 deletion(-)
diff --git a/libavutil/mem.c b/libavutil/mem.c
index 471b49f8e7..1b0e39eb89 100644
--- a/libavutil/mem.c
+++ b/libavutil/mem.c
@@ -513,6 +513,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));
@@ -520,7 +521,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));
--
2.31.1
More information about the ffmpeg-devel
mailing list