Use asprintf instead of malloc+sprintf.
authorRémi Duraffort <ivoire@videolan.org>
Fri, 31 Oct 2008 21:10:16 +0000 (22:10 +0100)
committerRémi Duraffort <ivoire@videolan.org>
Fri, 31 Oct 2008 21:10:16 +0000 (22:10 +0100)
modules/access/rtmp/rtmp_amf_flv.c
modules/access/vcd/cdrom.c
modules/control/http/macro.c
modules/control/http/rpn.c
modules/control/ntservice.c
modules/demux/mkv/demux.cpp
modules/demux/playlist/gvp.c
modules/demux/playlist/shoutcast.c
modules/gui/ncurses.c

index 10022a9..4added1 100644 (file)
@@ -429,14 +429,12 @@ rtmp_connect_active( rtmp_control_thread_t *p_thread )
         AMF_DATATYPE_SIZE_STRING + strlen( "file:///mac.flv" ) );
     free( tmp_buffer );
 
-    tmp_url = (char *) malloc( strlen( "rtmp://") + strlen( p_thread->url.psz_buffer ) + 1 );
-    if( !tmp_url )
+    if( asprintf( &tmp_url, "rtmp://%s", p_thread->url.psz_buffer ) == -1 )
     {
         free( rtmp_body->body );
         free( rtmp_body );
         return -1;
     }
-    sprintf( tmp_url, "rtmp://%s", p_thread->url.psz_buffer );
     tmp_buffer = amf_encode_object_variable( "tcUrl",
         AMF_DATATYPE_STRING, tmp_url );
     rtmp_body_append( rtmp_body, tmp_buffer,
@@ -1858,14 +1856,12 @@ rtmp_encode_NetStream_play_reset_onStatus( rtmp_control_thread_t *p_thread, char
         AMF_DATATYPE_SIZE_STRING + strlen( "NetStream.Play.Reset" ) );
     free( tmp_buffer );
 
-    description = (char *) malloc( strlen( "Playing and resetting ") + strlen( psz_media ) + strlen( "." ) + 1 );
-    if( !description )
+    if( asprintf( &description, "Playing and resetting %s.", psz_media ) == -1 )
     {
         free( rtmp_body->body );
         free( rtmp_body );
         return NULL;
     }
-    sprintf( description, "Playing and resetting %s.", psz_media );
     tmp_buffer = amf_encode_object_variable( "description",
         AMF_DATATYPE_STRING, description );
     rtmp_body_append( rtmp_body, tmp_buffer,
@@ -1944,15 +1940,13 @@ rtmp_encode_NetStream_play_start_onStatus( rtmp_control_thread_t *p_thread, char
         AMF_DATATYPE_SIZE_STRING + strlen( "NetStream.Play.Start" ) );
     free( tmp_buffer );
 
-    description = (char *) malloc( strlen( "Started playing ") + strlen( psz_media ) + strlen( "." ) + 1 );
-    if( !description )
+    if( asprintf( &description, "Started playing %s.", psz_media ) == -1 )
     {
         free( rtmp_body->body );
         free( rtmp_body );
         return NULL;
     }
 
-    sprintf( description, "Started playing %s.", psz_media );
     tmp_buffer = amf_encode_object_variable( "description",
         AMF_DATATYPE_STRING, description );
     rtmp_body_append( rtmp_body, tmp_buffer,
index b411c44..3457d12 100644 (file)
@@ -846,8 +846,8 @@ static int OpenVCDImage( vlc_object_t * p_this, const char *psz_dev,
         }
         else
         {
-            psz_cuefile = malloc( strlen(psz_dev) + 5 /* ".cue" */ );
-            sprintf( psz_cuefile, "%s.cue", psz_dev );
+            if( asprintf( &psz_cuefile, "%s.cue", psz_dev ) == -1 )
+                psz_cuefile = NULL;
         }
         /* If we need to look up the .cue file, then we don't have to look for the vcd */
         psz_vcdfile = strdup( psz_dev );
index 92ba529..7ecf1da 100644 (file)
@@ -587,11 +587,10 @@ static void MacroDo( httpd_file_sys_t *p_args,
                     }
                     else
                     {
-                        vlm_error = malloc( strlen(vlm_answer->psz_name) +
-                                            strlen(vlm_answer->psz_value) +
-                                            strlen( " : ") + 1 );
-                        sprintf( vlm_error , "%s : %s" , vlm_answer->psz_name,
-                                                         vlm_answer->psz_value );
+                        if( asprintf( &vlm_error , "%s : %s" ,
+                                      vlm_answer->psz_name,
+                                      vlm_answer->psz_value ) == -1 )
+                            vlm_error = NULL;
                     }
 
                     mvar_AppendNewVar( p_args->vars, "vlm_error", vlm_error );
index 32f715b..fa4e6d3 100644 (file)
@@ -1087,9 +1087,9 @@ void EvaluateRPN( intf_thread_t *p_intf, mvar_t  *vars,
             while( strcmp( psz_elt = SSPop( st ), "" )
                    && strcmp( psz_elt, ";" ) )
             {
-                char *psz_buf =
-                    (char *)malloc( strlen( psz_cmd ) + strlen( psz_elt ) + 2 );
-                sprintf( psz_buf, "%s %s", psz_cmd, psz_elt );
+                char* psz_buf;
+                if( asprintf( &psz_buf, "%s %s", psz_cmd, psz_elt ) == -1 )
+                    psz_buf = NULL;
                 free( psz_cmd );
                 free( psz_elt );
                 psz_cmd = psz_buf;
@@ -1104,11 +1104,9 @@ void EvaluateRPN( intf_thread_t *p_intf, mvar_t  *vars,
             }
             else
             {
-                psz_error = malloc( strlen(vlm_answer->psz_name) +
-                                    strlen(vlm_answer->psz_value) +
-                                    strlen( " : ") + 1 );
-                sprintf( psz_error , "%s : %s" , vlm_answer->psz_name,
-                                                 vlm_answer->psz_value );
+                if( asprintf( &psz_error , "%s : %s" , vlm_answer->psz_name,
+                              vlm_answer->psz_value ) == -1 )
+                    psz_error = NULL;
             }
 
             mvar_AppendNewVar( vars, "vlm_error", psz_error );
index e7bd289..1df3c5e 100644 (file)
@@ -315,11 +315,10 @@ static void WINAPI ServiceDispatch( DWORD numArgs, char **args )
             *psz_parser = '\0';
             psz_parser++;
         }
-        psz_temp = (char *)malloc( strlen(psz_module) + sizeof(",none") );
-        if( psz_temp )
+
+        if( asprintf( &psz_temp, "%s,none", psz_module ) != -1 )
         {
             intf_thread_t *p_new_intf;
-            sprintf( psz_temp, "%s,none", psz_module );
 
             /* Try to create the interface */
             p_new_intf = intf_Create( p_intf, psz_temp );
index 87e5b34..2057f34 100644 (file)
@@ -604,7 +604,6 @@ void demux_sys_t::PreloadLinked( matroska_segment_c *p_segment )
         p_seg = used_segments[i];
         if ( p_seg->p_editions != NULL )
         {
-            std::string sz_name;
             input_title_t *p_title = vlc_input_title_New();
             p_seg->i_sys_title = i;
             int i_chapters;
@@ -614,9 +613,9 @@ void demux_sys_t::PreloadLinked( matroska_segment_c *p_segment )
             {
                 if ( p_title->psz_name == NULL )
                 {
-                    sz_name = (*p_seg->p_editions)[j]->GetMainName();
-                    if ( sz_name != "" )
-                        p_title->psz_name = strdup( sz_name.c_str() );
+                    const char* psz_tmp = (*p_seg->p_editions)[j]->GetMainName().c_str();
+                    if( *psz_tmp != '\0' )
+                        p_title->psz_name = strdup( psz_tmp );
                 }
 
                 chapter_edition_c *p_edition = (*p_seg->p_editions)[j];
@@ -628,11 +627,8 @@ void demux_sys_t::PreloadLinked( matroska_segment_c *p_segment )
             // create a name if there is none
             if ( p_title->psz_name == NULL )
             {
-                sz_name = N_("Segment");
-                char psz_str[6];
-                sprintf( psz_str, " %d", (int)i );
-                sz_name += psz_str;
-                p_title->psz_name = strdup( sz_name.c_str() );
+                if( asprintf(&(p_title->psz_name), "%s %d", N_("Segment"), (int)i) == -1 )
+                    p_title->psz_name = NULL;
             }
 
             titles.push_back( p_title );
index 089284d..19d6d37 100644 (file)
@@ -178,9 +178,8 @@ static int Demux( demux_t *p_demux )
             else
             {
                 /* handle multi-line descriptions */
-                buf = malloc( strlen( psz_description )
-                            + strlen( psz_attrvalue ) + 2 );
-                sprintf( buf, "%s\n%s", psz_description, psz_attrvalue );
+                if( asprintf( &buf, "%s\n%s", psz_description, psz_attrvalue ) == -1 )
+                    buf = NULL;
                 free( psz_description );
                 psz_description = buf;
             }
index b972199..d3686c1 100644 (file)
@@ -388,19 +388,16 @@ static int DemuxStation( demux_t *p_demux )
                     if( psz_rt || psz_load )
                     {
                         /* tv */
-                        psz_mrl = malloc( strlen( SHOUTCAST_TV_TUNEIN_URL )
-                                          + strlen( psz_id ) + 1 );
-                        sprintf( psz_mrl, SHOUTCAST_TV_TUNEIN_URL "%s",
-                                 psz_id );
+                        if( asprintf( &psz_mrl, SHOUTCAST_TV_TUNEIN_URL "%s",
+                                 psz_id ) == -1)
+                            psz_mrl = NULL;
                     }
                     else
                     {
                         /* radio */
-                        psz_mrl = malloc( strlen( SHOUTCAST_TUNEIN_BASE_URL )
-                            + strlen( psz_base ) + strlen( "?id=" )
-                            + strlen( psz_id ) + 1 );
-                        sprintf( psz_mrl, SHOUTCAST_TUNEIN_BASE_URL "%s?id=%s",
-                             psz_base, psz_id );
+                        if( asprintf( &psz_mrl, SHOUTCAST_TUNEIN_BASE_URL "%s?id=%s",
+                             psz_base, psz_id ) == -1 )
+                            psz_mrl = NULL;
                     }
                     p_input = input_item_NewExt( p_demux, psz_mrl,
                                                 psz_name , 0, NULL, -1 );
index 48c3b18..6262e1d 100644 (file)
@@ -778,12 +778,9 @@ static int HandleKey( intf_thread_t *p_intf, int i_key )
             case ' ':
                 if( p_sys->pp_dir_entries[p_sys->i_box_bidx]->b_file || i_key == ' ' )
                 {
-                    int i_size_entry = strlen( "directory://" ) +
-                                       strlen( p_sys->psz_current_dir ) +
-                                       strlen( p_sys->pp_dir_entries[p_sys->i_box_bidx]->psz_path ) + 2;
-                    char *psz_uri = (char *)malloc( sizeof(char)*i_size_entry);
-
-                    sprintf( psz_uri, "directory://%s/%s", p_sys->psz_current_dir, p_sys->pp_dir_entries[p_sys->i_box_bidx]->psz_path );
+                    char* psz_uri;
+                    if( asprintf( &psz_uri, "directory://%s/%s", p_sys->psz_current_dir, p_sys->pp_dir_entries[p_sys->i_box_bidx]->psz_path ) == -1 )
+                        psz_uri = NULL;
 
                     playlist_item_t *p_parent = p_sys->p_node;
                     if( !p_parent )
@@ -805,15 +802,9 @@ static int HandleKey( intf_thread_t *p_intf, int i_key )
                 }
                 else
                 {
-                    int i_size_entry = strlen( p_sys->psz_current_dir ) +
-                                       strlen( p_sys->pp_dir_entries[p_sys->i_box_bidx]->psz_path ) + 2;
-                    char *psz_uri = (char *)malloc( sizeof(char)*i_size_entry);
-
-                    sprintf( psz_uri, "%s/%s", p_sys->psz_current_dir, p_sys->pp_dir_entries[p_sys->i_box_bidx]->psz_path );
-
-                    p_sys->psz_current_dir = strdup( psz_uri );
-                    ReadDir( p_intf );
-                    free( psz_uri );
+                    if( asprintf( &(p_sys->psz_current_dir), "%s/%s", p_sys->psz_current_dir,
+                                  p_sys->pp_dir_entries[p_sys->i_box_bidx]->psz_path ) != -1 )
+                        ReadDir( p_intf );
                 }
                 break;
             default: