[FFmpeg-devel] [PATCH] Avoid running malloc/free for 0-sized allocs and return a pointer to const memory so we will still detect invalid writes to malloc(0) areas. This also fixes av_realloc to treat size == 0 specially.
Reimar Döffinger
Reimar.Doeffinger at gmx.de
Sat May 7 09:56:31 CEST 2011
---
libavutil/mem.c | 10 +++++++++-
1 files changed, 9 insertions(+), 1 deletions(-)
diff --git a/libavutil/mem.c b/libavutil/mem.c
index f0f18d1..134fcba 100644
--- a/libavutil/mem.c
+++ b/libavutil/mem.c
@@ -57,6 +57,8 @@ void free(void *ptr);
#endif /* MALLOC_PREFIX */
+static const int dummy_alloc;
+
/* You can redefine av_malloc and av_free in your project to use your
memory allocator. You do not need to suppress this file because the
linker will do it automatically. */
@@ -72,7 +74,7 @@ void *av_malloc(size_t size)
if(size > (INT_MAX-32) )
return NULL;
else if(!size)
- size= 1;
+ return (void *)&dummy_alloc;
#if CONFIG_MEMALIGN_HACK
ptr = malloc(size+32);
@@ -125,6 +127,10 @@ void *av_realloc(void *ptr, size_t size)
/* let's disallow possible ambiguous cases */
if(size > (INT_MAX-16) )
return NULL;
+ else if (!size) {
+ av_freep(ptr);
+ return (void *)&dummy_alloc;
+ }
#if CONFIG_MEMALIGN_HACK
//FIXME this isn't aligned correctly, though it probably isn't needed
@@ -138,6 +144,8 @@ void *av_realloc(void *ptr, size_t size)
void av_free(void *ptr)
{
+ if (ptr == &dummy_alloc)
+ return;
#if CONFIG_MEMALIGN_HACK
if (ptr)
free((char*)ptr - ((char*)ptr)[-1]);
--
1.7.4.4
More information about the ffmpeg-devel
mailing list