[MPlayer-dev-eng] [PATCH 4/4] libmpdemux/mf: init_mf_from_comma_delimited_paths: Avoid use of strsep

Alexander Strasser eclipse7 at gmx.net
Tue Jun 1 23:06:19 EEST 2021


The strsep function modifies its parameter and we need to store our
own copy in mf->names anyway. It's also not in C99.

Make the function argument filename `const char *` as it is already
in the other init_mf_from_ functions.
---
 libmpdemux/mf.c | 27 ++++++++++++++++++++-------
 1 file changed, 20 insertions(+), 7 deletions(-)

diff --git a/libmpdemux/mf.c b/libmpdemux/mf.c
index 89fc2a7a0..eac6053a5 100644
--- a/libmpdemux/mf.c
+++ b/libmpdemux/mf.c
@@ -16,8 +16,6 @@
  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  */

-#define _BSD_SOURCE
-
 #include <ctype.h>
 #include <stdio.h>
 #include <stdlib.h>
@@ -80,21 +78,36 @@ static int init_mf_from_list_file(mf_t* mf, const char * filename){
 }


-static int init_mf_from_comma_delimited_paths(mf_t* mf, char * filename){
-   char * fname;
+static int init_mf_from_comma_delimited_paths(mf_t* mf, const char * filename){
+   int finished = 0;
+   size_t idx = 0;
    mp_msg( MSGT_STREAM,MSGL_INFO,"[mf] filelist: %s\n",filename );

-   while ( ( fname=strsep( &filename,"," ) ) )
+   while ( !finished )
     {
      struct stat fs;
-     if ( stat( fname,&fs ) )
+     size_t num_bytes = strcspn( &filename[idx],"," );
+     char * fname = calloc( 1,num_bytes+1 ); // always zero-terminated
+     if ( fname )
+      {
+       strncpy( fname,&filename[idx],num_bytes );
+      }
+     finished = !filename[idx + num_bytes];
+     idx += num_bytes + !finished;
+
+     if ( !fname )
+      {
+       mp_msg( MSGT_STREAM,MSGL_WARN,"[mf] allocation for path failed.\n" );
+      }
+     else if ( stat( fname,&fs ) )
       {
        mp_msg( MSGT_STREAM,MSGL_V,"[mf] file not found: '%s'\n",fname );
+       free( fname );
       }
       else
       {
        mf->names=realloc( mf->names,( mf->nr_of_files + 1 ) * sizeof( char* ) );
-       mf->names[mf->nr_of_files]=strdup( fname );
+       mf->names[mf->nr_of_files]=fname;
 //       mp_msg( MSGT_STREAM,MSGL_V,"[mf] added file %d.: %s\n",mf->nr_of_files,mf->names[mf->nr_of_files] );
        mf->nr_of_files++;
       }
--


More information about the MPlayer-dev-eng mailing list