]> git.sesse.net Git - vlc/blobdiff - src/input/input.c
decoder: fix potential NULL dereference
[vlc] / src / input / input.c
index a51d3a265563907ce15daa8645818c01db670c7c..7f916f262e736566a2977aa311080891ce1f97e2 100644 (file)
@@ -34,6 +34,7 @@
 #include <limits.h>
 #include <assert.h>
 #include <errno.h>
+#include <math.h>
 #include <sys/stat.h>
 
 #include "input_internal.h"
@@ -91,7 +92,7 @@ static void SlaveDemux( input_thread_t *p_input, bool *pb_demux_polled );
 static void SlaveSeek( input_thread_t *p_input );
 
 static void InputMetaUser( input_thread_t *p_input, vlc_meta_t *p_meta );
-static void InputUpdateMeta( input_thread_t *p_input, vlc_meta_t *p_meta );
+static void InputUpdateMeta( input_thread_t *p_input, demux_t *p_demux );
 static void InputGetExtraFiles( input_thread_t *p_input,
                                 int *pi_list, char ***pppsz_list,
                                 const char *psz_access, const char *psz_path );
@@ -429,7 +430,7 @@ static input_thread_t *Create( vlc_object_t *p_parent, input_item_t *p_item,
                      else if( !strncmp( psz_start, "time=", 5 ) )
                      {
                          p_seekpoint->i_time_offset = atoll(psz_start + 5) *
-                                                        1000000;
+                                                        CLOCK_FREQ;
                      }
                      psz_start = psz_end + 1;
                 }
@@ -631,7 +632,7 @@ static int MainLoopTryRepeat( input_thread_t *p_input, mtime_t *pi_start_mdate )
     }
     else
     {
-        val.f_float = 0.0;
+        val.f_float = 0.f;
         input_ControlPush( p_input, INPUT_CONTROL_SET_POSITION, &val );
     }
 
@@ -700,8 +701,7 @@ static void MainLoop( input_thread_t *p_input, bool b_interactive )
     {
         bool b_force_update;
         vlc_value_t val;
-        mtime_t i_current;
-        mtime_t i_wakeup;
+        mtime_t i_wakeup, i_current;
         bool b_paused;
         bool b_demux_polled;
 
@@ -765,9 +765,9 @@ static void MainLoop( input_thread_t *p_input, bool b_interactive )
                 if( b_buffering )
                 {
                     /* When postpone is in order, check the ES level every 20ms */
-                    mtime_t i_current = mdate();
-                    if( i_last_seek_mdate + INT64_C(125000) >= i_current )
-                        i_limit = __MIN( i_deadline, i_current + INT64_C(20000) );
+                    mtime_t now = mdate();
+                    if( i_last_seek_mdate + INT64_C(125000) >= now )
+                        i_limit = __MIN( i_deadline, now + INT64_C(20000) );
                 }
 
                 int i_type;
@@ -801,7 +801,7 @@ static void MainLoop( input_thread_t *p_input, bool b_interactive )
             if( i_statistic_update < i_current )
             {
                 MainLoopStatistic( p_input );
-                i_statistic_update = i_current + INT64_C(1000000);
+                i_statistic_update = i_current + CLOCK_FREQ;
             }
 
             /* Update the wakeup time */
@@ -922,11 +922,11 @@ static void StartTitle( input_thread_t * p_input )
         input_ControlPush( p_input, INPUT_CONTROL_SET_SEEKPOINT, &val );
 
     /* Start/stop/run time */
-    p_input->p->i_start = (int64_t)(1000000.0
+    p_input->p->i_start = llroundf(1000000.f
                                      * var_GetFloat( p_input, "start-time" ));
-    p_input->p->i_stop  = (int64_t)(1000000.0
+    p_input->p->i_stop  = llroundf(1000000.f
                                      * var_GetFloat( p_input, "stop-time" ));
-    p_input->p->i_run   = (int64_t)(1000000.0
+    p_input->p->i_run   = llroundf(1000000.f
                                      * var_GetFloat( p_input, "run-time" ));
     if( p_input->p->i_run < 0 )
     {
@@ -939,7 +939,7 @@ static void StartTitle( input_thread_t * p_input )
         vlc_value_t s;
 
         msg_Dbg( p_input, "starting at time: %ds",
-                 (int)( p_input->p->i_start / INT64_C(1000000) ) );
+                 (int)( p_input->p->i_start / CLOCK_FREQ ) );
 
         s.i_time = p_input->p->i_start;
         input_ControlPush( p_input, INPUT_CONTROL_SET_TIME, &s );
@@ -956,15 +956,13 @@ static void LoadSubtitles( input_thread_t *p_input )
 {
     /* Load subtitles */
     /* Get fps and set it if not already set */
-    const double f_fps = p_input->p->f_fps;
-    if( f_fps > 1.0 )
+    const float f_fps = p_input->p->f_fps;
+    if( f_fps > 1.f )
     {
-        float f_requested_fps;
-
         var_Create( p_input, "sub-original-fps", VLC_VAR_FLOAT );
         var_SetFloat( p_input, "sub-original-fps", f_fps );
 
-        f_requested_fps = var_CreateGetFloat( p_input, "sub-fps" );
+        float f_requested_fps = var_CreateGetFloat( p_input, "sub-fps" );
         if( f_requested_fps != f_fps )
         {
             var_Create( p_input, "sub-fps", VLC_VAR_FLOAT|
@@ -1128,11 +1126,6 @@ static void InitPrograms( input_thread_t * p_input )
     {
         char *prgms;
 
-        if( var_GetBool( p_input, "sout-all" ) )
-        {
-            i_es_out_mode = ES_OUT_MODE_ALL;
-        }
-        else
         if( (prgms = var_GetNonEmptyString( p_input, "programs" )) != NULL )
         {
             char *buf;
@@ -1152,6 +1145,10 @@ static void InitPrograms( input_thread_t * p_input )
 
             free( prgms );
         }
+        else if( var_GetBool( p_input, "sout-all" ) )
+        {
+            i_es_out_mode = ES_OUT_MODE_ALL;
+        }
     }
     es_out_SetMode( p_input->p->p_es_out, i_es_out_mode );
 
@@ -1175,10 +1172,7 @@ static void InitPrograms( input_thread_t * p_input )
 
 static int Init( input_thread_t * p_input )
 {
-    vlc_meta_t *p_meta;
-    int i;
-
-    for( i = 0; i < p_input->p->p_item->i_options; i++ )
+    for( int i = 0; i < p_input->p->p_item->i_options; i++ )
     {
         if( !strncmp( p_input->p->p_item->ppsz_options[i], "meta-file", 9 ) )
         {
@@ -1256,8 +1250,8 @@ static int Init( input_thread_t * p_input )
                  p_input->p->b_out_pace_control ? "async" : "sync" );
     }
 
-    p_meta = vlc_meta_New();
-    if( p_meta )
+    vlc_meta_t *p_meta = vlc_meta_New();
+    if( p_meta != NULL )
     {
         /* Get meta data from users */
         InputMetaUser( p_input, p_meta );
@@ -1269,8 +1263,8 @@ static int Init( input_thread_t * p_input )
         for( int i = 0; i < p_input->p->i_slave; i++ )
             InputSourceMeta( p_input, p_input->p->slave[i], p_meta );
 
-        /* */
-        InputUpdateMeta( p_input, p_meta );
+        es_out_ControlSetMeta( p_input->p->p_es_out, p_meta );
+        vlc_meta_Delete( p_meta );
     }
 
     msg_Dbg( p_input, "`%s' successfully opened",
@@ -1653,27 +1647,26 @@ static bool Control( input_thread_t *p_input,
 
         case INPUT_CONTROL_SET_POSITION:
         {
-            double f_pos;
-
             if( p_input->p->b_recording )
             {
                 msg_Err( p_input, "INPUT_CONTROL_SET_POSITION(_OFFSET) ignored while recording" );
                 break;
             }
-            f_pos = val.f_float;
+
+            float f_pos = val.f_float;
             if( i_type != INPUT_CONTROL_SET_POSITION )
                 f_pos += var_GetFloat( p_input, "position" );
-            if( f_pos < 0.0 )
-                f_pos = 0.0;
-            else if( f_pos > 1.0 )
-                f_pos = 1.0;
+            if( f_pos < 0.f )
+                f_pos = 0.f;
+            else if( f_pos > 1.f )
+                f_pos = 1.f;
             /* Reset the decoders states and clock sync (before calling the demuxer */
             es_out_SetTime( p_input->p->p_es_out, -1 );
             if( demux_Control( p_input->p->input.p_demux, DEMUX_SET_POSITION,
-                                f_pos, !p_input->p->b_fast_seek ) )
+                               (double) f_pos, !p_input->p->b_fast_seek ) )
             {
                 msg_Err( p_input, "INPUT_CONTROL_SET_POSITION(_OFFSET) "
-                         "%2.1f%% failed", f_pos * 100 );
+                         "%2.1f%% failed", (double)(f_pos * 100.f) );
             }
             else
             {
@@ -1960,7 +1953,6 @@ static bool Control( input_thread_t *p_input,
 
                 if( slave && !InputSourceInit( p_input, slave, uri, NULL, false ) )
                 {
-                    vlc_meta_t *p_meta;
                     int64_t i_time;
 
                     /* Add the slave */
@@ -1985,12 +1977,7 @@ static bool Control( input_thread_t *p_input,
                     }
 
                     /* Get meta (access and demux) */
-                    p_meta = vlc_meta_New();
-                    if( p_meta )
-                    {
-                        demux_Control( slave->p_demux, DEMUX_GET_META, p_meta );
-                        InputUpdateMeta( p_input, p_meta );
-                    }
+                    InputUpdateMeta( p_input, slave->p_demux );
 
                     TAB_APPEND( p_input->p->i_slave, p_input->p->slave, slave );
                 }
@@ -2146,12 +2133,7 @@ static void UpdateGenericFromDemux( input_thread_t *p_input )
 
     if( p_demux->info.i_update & INPUT_UPDATE_META )
     {
-        vlc_meta_t *p_meta = vlc_meta_New();
-        if( p_meta )
-        {
-            demux_Control( p_input->p->input.p_demux, DEMUX_GET_META, p_meta );
-            InputUpdateMeta( p_input, p_meta );
-        }
+        InputUpdateMeta( p_input, p_demux );
         p_demux->info.i_update &= ~INPUT_UPDATE_META;
     }
     {
@@ -2413,7 +2395,6 @@ static int InputSourceInit( input_thread_t *p_input,
 
         if( in->p_demux == NULL )
         {
-            stream_Delete( p_stream );
             if( vlc_object_alive( p_input ) )
             {
                 msg_Err( p_input, "no suitable demux module for `%s/%s://%s'",
@@ -2424,6 +2405,7 @@ static int InputSourceInit( input_thread_t *p_input,
                                   _("The format of '%s' cannot be detected. "
                                     "Have a look at the log for details."), psz_mrl );
             }
+            stream_Delete( p_stream );
             goto error;
         }
         assert( in->p_demux->pf_demux != NULL );
@@ -2722,15 +2704,21 @@ static void AppendAttachment( int *pi_attachment, input_attachment_t ***ppp_atta
  * InputUpdateMeta: merge p_item meta data with p_meta taking care of
  * arturl and locking issue.
  *****************************************************************************/
-static void InputUpdateMeta( input_thread_t *p_input, vlc_meta_t *p_meta )
+static void InputUpdateMeta( input_thread_t *p_input, demux_t *p_demux )
 {
+    vlc_meta_t *p_meta = vlc_meta_New();
+    if( unlikely(p_meta == NULL) )
+        return;
+
+    demux_Control( p_demux, DEMUX_GET_META, p_meta );
+
     /* If metadata changed, then the attachments might have changed.
        We need to update them in case they contain album art. */
-    input_source_t *in = &p_input->p->input;
-    int i_attachment;
     input_attachment_t **attachment;
-    if( !demux_Control( in->p_demux, DEMUX_GET_ATTACHMENTS,
-                         &attachment, &i_attachment ) )
+    int i_attachment;
+
+    if( !demux_Control( p_demux, DEMUX_GET_ATTACHMENTS,
+                        &attachment, &i_attachment ) )
     {
         vlc_mutex_lock( &p_input->p->p_item->lock );
         if( p_input->p->i_attachment > 0 )
@@ -2743,6 +2731,7 @@ static void InputUpdateMeta( input_thread_t *p_input, vlc_meta_t *p_meta )
                           i_attachment, attachment );
         vlc_mutex_unlock( &p_input->p->p_item->lock );
     }
+
     es_out_ControlSetMeta( p_input->p->p_es_out, p_meta );
     vlc_meta_Delete( p_meta );
 }