[Ffmpeg-devel] [PATCH] minor improvements to libavcodec/mem.c

Hervé W. H.O.W.aka.V+ffmpeg
Thu Jun 29 13:21:41 CEST 2006


Hi,

I attached 2 diffs.
minor improvements to av_alloc and av_realloc.diff
(starts with: Index: libavcodec/mem.c)
should apply without problems to the current version

(dependant on cosmetic changes patch) minor improvements.diff
(starts with: --- ffmpeg-devel/libavcodec/mem.c	Thu Jun 29 11:34:24 2006)
might require that my patch in
http://lists.mplayerhq.hu/pipermail/ffmpeg-devel/2006-June/012242.html
is applied first.

What should I do in the future when I have multiple changes to the
same file, but should be submitted in seperate patches?

The changes:
*Allocating 16 bytes more for the MEMALIGN_HACK is enough. There's no
need for 1 more extra byte.

*Checking whether the to be allocated size is larger than INT_MAX,
doesn't assure that size+16 bytes for the MEMALIGN_HACK isn't larger
than INT_MAX.

*malloc might return NULL. Checking for it before using that pointer
seems like a good idea.

-V
-------------- next part --------------
Index: libavcodec/mem.c
===================================================================
--- libavcodec/mem.c	(revision 5538)
+++ libavcodec/mem.c	(working copy)
@@ -47,14 +47,19 @@
     void *ptr;
 #ifdef MEMALIGN_HACK
     long diff;
-#endif
 
     /* lets disallow possible ambiguous cases */
+    if(size > (INT_MAX-16) )
+        return NULL;
+#else
     if(size > INT_MAX)
         return NULL;
+#endif
 
 #ifdef MEMALIGN_HACK
-    ptr = malloc(size+16+1);
+    ptr = malloc(size+16);
+    if(!ptr)
+        return ptr;
     diff= ((-(long)ptr - 1)&15) + 1;
     ptr += diff;
     ((char*)ptr)[-1]= diff;
@@ -101,11 +106,14 @@
 {
 #ifdef MEMALIGN_HACK
     int diff;
-#endif
 
     /* lets disallow possible ambiguous cases */
+    if(size > (INT_MAX-16) )
+        return NULL;
+#else
     if(size > INT_MAX)
         return NULL;
+#endif
 
 #ifdef MEMALIGN_HACK
     //FIXME this isnt aligned correctly though it probably isnt needed
-------------- next part --------------
--- ffmpeg-devel/libavcodec/mem.c	Thu Jun 29 11:34:24 2006
+++ ffmpeg-devel2/libavcodec/mem.c	Thu Jun 29 12:10:11 2006
@@ -47,14 +47,19 @@ void *av_malloc(unsigned int size)
     void *ptr;
 #ifdef MEMALIGN_HACK
     long diff;
-#endif
 
     /* let's disallow possible ambiguous cases */
+    if(size > (INT_MAX-16) )
+        return NULL;
+#else
     if(size > INT_MAX)
         return NULL;
+#endif
 
 #ifdef MEMALIGN_HACK
-    ptr = malloc(size+16+1);
+    ptr = malloc(size+16);
+    if(!ptr)
+        return ptr;
     diff= ((-(long)ptr - 1)&15) + 1;
     ptr += diff;
     ((char*)ptr)[-1]= diff;
@@ -101,11 +106,14 @@ void *av_realloc(void *ptr, unsigned int
 {
 #ifdef MEMALIGN_HACK
     long diff;
-#endif
 
     /* let's disallow possible ambiguous cases */
+    if(size > (INT_MAX-16) )
+        return NULL;
+#else
     if(size > INT_MAX)
         return NULL;
+#endif
 
 #ifdef MEMALIGN_HACK
     //FIXME this isn't aligned correctly, though it probably isn't needed



More information about the ffmpeg-devel mailing list