[vlc-commits] input: fix vlc_attachment_New() error handling

Rémi Denis-Courmont git at videolan.org
Sat Aug 15 12:24:01 CEST 2015


vlc | branch: master | Rémi Denis-Courmont <remi at remlab.net> | Sat Aug 15 13:18:55 2015 +0300| [502e723f495ddea356902e5d544f1d96fe5766a6] | committer: Rémi Denis-Courmont

input: fix vlc_attachment_New() error handling

Also use size_t for the size.

> http://git.videolan.org/gitweb.cgi/vlc.git/?a=commit;h=502e723f495ddea356902e5d544f1d96fe5766a6
---

 include/vlc_input.h |   46 ++++++++++++++++++++++++++--------------------
 1 file changed, 26 insertions(+), 20 deletions(-)

diff --git a/include/vlc_input.h b/include/vlc_input.h
index 075c694..06d64c7 100644
--- a/include/vlc_input.h
+++ b/include/vlc_input.h
@@ -161,48 +161,54 @@ struct input_attachment_t
     char *psz_mime;
     char *psz_description;
 
-    int  i_data;
+    size_t i_data;
     void *p_data;
 };
 
+static inline void vlc_input_attachment_Delete( input_attachment_t *a )
+{
+    if( !a )
+        return;
+
+    free( a->p_data );
+    free( a->psz_description );
+    free( a->psz_mime );
+    free( a->psz_name );
+    free( a );
+}
+
 static inline input_attachment_t *vlc_input_attachment_New( const char *psz_name,
                                                             const char *psz_mime,
                                                             const char *psz_description,
                                                             const void *p_data,
-                                                            int i_data )
+                                                            size_t i_data )
 {
-    input_attachment_t *a =
-        (input_attachment_t*)malloc( sizeof(input_attachment_t) );
-    if( !a )
+    input_attachment_t *a = (input_attachment_t *)malloc( sizeof (*a) );
+    if( unlikely(a == NULL) )
         return NULL;
+
     a->psz_name = strdup( psz_name ? psz_name : "" );
     a->psz_mime = strdup( psz_mime ? psz_mime : "" );
     a->psz_description = strdup( psz_description ? psz_description : "" );
     a->i_data = i_data;
-    a->p_data = NULL;
-    if( i_data > 0 )
+    a->p_data = malloc( i_data );
+    if( i_data > 0 && likely(p_data != NULL) )
+        memcpy( a->p_data, p_data, i_data );
+
+    if( unlikely(a->psz_name == NULL || a->psz_mime == NULL
+              || a->psz_description || (i_data > 0 && a->p_data == NULL)) )
     {
-        a->p_data = malloc( i_data );
-        if( a->p_data && p_data )
-            memcpy( a->p_data, p_data, i_data );
+        vlc_input_attachment_Delete( a );
+        a = NULL;
     }
     return a;
 }
+
 static inline input_attachment_t *vlc_input_attachment_Duplicate( const input_attachment_t *a )
 {
     return vlc_input_attachment_New( a->psz_name, a->psz_mime, a->psz_description,
                                      a->p_data, a->i_data );
 }
-static inline void vlc_input_attachment_Delete( input_attachment_t *a )
-{
-    if( !a )
-        return;
-    free( a->psz_name );
-    free( a->psz_mime );
-    free( a->psz_description );
-    free( a->p_data );
-    free( a );
-}
 
 /*****************************************************************************
  * input defines/constants.



More information about the vlc-commits mailing list