[vlc-devel] [PATCH] realloc: use realloc_or_free() to avoid memory leak

Arjun Sreedharan arjun024 at gmail.com
Wed Dec 10 15:35:24 CET 2014


overwriting pointer with realloc()'s return value can leak memory
when realloc() fails. realloc_or_free() manages this and frees memory
on failure.

Signed-off-by: Arjun Sreedharan <arjun024 at gmail.com>
---
 modules/access/linsys/linsys_sdi.c  |  7 ++++++-
 modules/codec/opus_header.c         |  5 +++--
 modules/control/globalhotkeys/xcb.c |  3 ++-
 modules/demux/mkv/util.cpp          |  6 ++++--
 modules/services_discovery/sap.c    | 13 +++++++++++--
 modules/video_output/xcb/window.c   |  3 ++-
 src/modules/cache.c                 |  3 ++-
 7 files changed, 30 insertions(+), 10 deletions(-)

diff --git a/modules/access/linsys/linsys_sdi.c b/modules/access/linsys/linsys_sdi.c
index d2f9184..fd42a3a 100644
--- a/modules/access/linsys/linsys_sdi.c
+++ b/modules/access/linsys/linsys_sdi.c
@@ -35,6 +35,7 @@
 #include <errno.h>
 
 #include <vlc_common.h>
+#include <vlc_memory.h>
 #include <vlc_plugin.h>
 
 #include <vlc_input.h>
@@ -513,7 +514,11 @@ static int StartDecode( demux_t *p_demux )
                         break;
                     i_page = strtol( psz_parser, NULL, 0 );
                     i_dr_size += 5;
-                    p_dr = realloc( p_dr, i_dr_size );
+                    p_dr = realloc_or_free( p_dr, i_dr_size );
+                    if ( !p_dr )
+                    {
+                        return VLC_ENOMEM;
+                    }
                     p_dr[i_dr_size - 5] = *psz_next++;
                     p_dr[i_dr_size - 4] = *psz_next++;
                     p_dr[i_dr_size - 3] = *psz_next++;
diff --git a/modules/codec/opus_header.c b/modules/codec/opus_header.c
index 101a33d..c266fe9 100644
--- a/modules/codec/opus_header.c
+++ b/modules/codec/opus_header.c
@@ -35,6 +35,7 @@
 #include <stdlib.h>
 
 #include <vlc_common.h>
+#include <vlc_memory.h>
 #include "../demux/xiph.h"
 
 /* Header contents:
@@ -267,7 +268,7 @@ static int comment_add(char **comments, size_t *length, const char *tag,
     size_t val_len = strlen(val);
     size_t len = (*length) + 4 + tag_len + val_len;
 
-    p = realloc(p, len);
+    p = realloc_or_free(p, len);
     if (p == NULL)
         return 1;
 
@@ -289,7 +290,7 @@ static int comment_pad(char **comments, size_t *length)
     /* Make sure there is at least "padding" worth of padding free, and
        round up to the maximum that fits in the current ogg segments. */
     size_t newlen = ((*length + padding) / 255 + 1) * 255 - 1;
-    p = realloc(p, newlen);
+    p = realloc_or_free(p, newlen);
     if (p == NULL)
         return 1;
 
diff --git a/modules/control/globalhotkeys/xcb.c b/modules/control/globalhotkeys/xcb.c
index c6a43de..12f2b7f 100644
--- a/modules/control/globalhotkeys/xcb.c
+++ b/modules/control/globalhotkeys/xcb.c
@@ -24,6 +24,7 @@
 # include "config.h"
 #endif
 #include <vlc_common.h>
+#include <vlc_memory.h>
 #include <vlc_plugin.h>
 #include <vlc_interface.h>
 #include <vlc_keys.h>
@@ -327,7 +328,7 @@ static bool Mapping( intf_thread_t *p_intf )
                 continue;
 
             hotkey_mapping_t *p_map_old = p_sys->p_map;
-            p_sys->p_map = realloc( p_sys->p_map,
+            p_sys->p_map = realloc_or_free( p_sys->p_map,
                     sizeof(*p_sys->p_map) * (p_sys->i_map+1) );
             if( !p_sys->p_map )
             {
diff --git a/modules/demux/mkv/util.cpp b/modules/demux/mkv/util.cpp
index 9608b95..0fce499 100644
--- a/modules/demux/mkv/util.cpp
+++ b/modules/demux/mkv/util.cpp
@@ -26,6 +26,8 @@
 #include "demux.hpp"
 
 #include <stdint.h>
+
+#include <vlc_memory.h>
 /*****************************************************************************
  * Local prototypes
  *****************************************************************************/
@@ -57,7 +59,7 @@ int32_t zlib_decompress_extra( demux_t * p_demux, mkv_track_t * tk )
     do
     {
         n++;
-        p_new_extra = (uint8_t *) realloc(p_new_extra, n*1024);
+        p_new_extra = (uint8_t *) realloc_or_free(p_new_extra, n*1024);
         if( !p_new_extra )
         {
             msg_Err( p_demux, "Couldn't allocate buffer to inflate data, ignore track %d",
@@ -85,7 +87,7 @@ int32_t zlib_decompress_extra( demux_t * p_demux, mkv_track_t * tk )
 
     free( tk->p_extra_data );
     tk->i_extra_data = d_stream.total_out;
-    p_new_extra = (uint8_t *) realloc(p_new_extra, tk->i_extra_data);
+    p_new_extra = (uint8_t *) realloc_or_free(p_new_extra, tk->i_extra_data);
     if( !p_new_extra )
     {
         msg_Err( p_demux, "Couldn't allocate buffer to inflate data, ignore track %d",
diff --git a/modules/services_discovery/sap.c b/modules/services_discovery/sap.c
index 8888215..dd1d1fb 100644
--- a/modules/services_discovery/sap.c
+++ b/modules/services_discovery/sap.c
@@ -31,6 +31,7 @@
 #endif
 
 #include <vlc_common.h>
+#include <vlc_memory.h>
 #include <vlc_plugin.h>
 #include <assert.h>
 
@@ -727,7 +728,10 @@ static int ParseSAP( services_discovery_t *p_sd, const uint8_t *buf,
             return VLC_EGENERIC;
         }
 
-        decomp = realloc (decomp, newsize + 1);
+        decomp = realloc_or_free (decomp, newsize + 1);
+        if( !decomp )
+            return VLC_EGENERIC;
+
         decomp[newsize] = '\0';
         psz_sdp = (const char *)decomp;
         len = newsize;
@@ -1497,7 +1501,12 @@ static int Decompress( const unsigned char *psz_src, unsigned char **_dst, int i
     do
     {
         n++;
-        psz_dst = (unsigned char *)realloc( psz_dst, n * 1000 );
+        psz_dst = (unsigned char *)realloc_or_free( psz_dst, n * 1000 );
+        if( !psz_dst )
+        {
+            inflateEnd( &d_stream );
+            return( -1 );
+        }
         d_stream.next_out = (Bytef *)&psz_dst[(n - 1) * 1000];
         d_stream.avail_out = 1000;
 
diff --git a/modules/video_output/xcb/window.c b/modules/video_output/xcb/window.c
index 91a7096..6040cd7 100644
--- a/modules/video_output/xcb/window.c
+++ b/modules/video_output/xcb/window.c
@@ -35,6 +35,7 @@ typedef xcb_atom_t Atom;
 #include <X11/Xatom.h> /* XA_WM_NAME */
 
 #include <vlc_common.h>
+#include <vlc_memory.h>
 #include <vlc_plugin.h>
 #include <vlc_vout_window.h>
 
@@ -536,7 +537,7 @@ static int AcquireDrawable (vlc_object_t *obj, xcb_window_t window)
         }
     }
 
-    used = realloc (used, sizeof (*used) * (n + 2));
+    used = realloc_or_free (used, sizeof (*used) * (n + 2));
     if (used != NULL)
     {
         used[n] = window;
diff --git a/src/modules/cache.c b/src/modules/cache.c
index f94d091..358109d 100644
--- a/src/modules/cache.c
+++ b/src/modules/cache.c
@@ -37,6 +37,7 @@
 #include <assert.h>
 
 #include <vlc_common.h>
+#include <vlc_memory.h>
 #include "libvlc.h"
 
 #include <vlc_plugin.h>
@@ -680,7 +681,7 @@ int CacheAdd (module_cache_t **cachep, size_t *countp,
     module_cache_t *cache = *cachep;
     const size_t count = *countp;
 
-    cache = realloc (cache, (count + 1) * sizeof (*cache));
+    cache = realloc_or_free (cache, (count + 1) * sizeof (*cache));
     if (unlikely(cache == NULL))
         return -1;
     *cachep = cache;
-- 
1.7.11.7




More information about the vlc-devel mailing list