]> git.sesse.net Git - vlc/blobdiff - modules/access/ftp.c
Fix potential double free (cid #1047496 and #1047497)
[vlc] / modules / access / ftp.c
index 106cc8964a1294b66af64a813f99770f974ab3b8..f1896f9fb926ef5f04003867c0d0d362b384eef4 100644 (file)
@@ -1,26 +1,26 @@
 /*****************************************************************************
  * ftp.c: FTP input module
  *****************************************************************************
- * Copyright (C) 2001-2006 the VideoLAN team
+ * Copyright (C) 2001-2006 VLC authors and VideoLAN
  * Copyright © 2006 Rémi Denis-Courmont
  * $Id$
  *
  * Authors: Laurent Aimar <fenrir@via.ecp.fr> - original code
  *          Rémi Denis-Courmont <rem # videolan.org> - EPSV support
  *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation; either version 2.1 of the License, or
  * (at your option) any later version.
  *
  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Lesser General Public License for more details.
  *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  *****************************************************************************/
 
 /*****************************************************************************
@@ -342,6 +342,23 @@ static int Login( vlc_object_t *p_access, access_sys_t *p_sys )
     return 0;
 }
 
+static void FeaturesCheck( void *opaque, const char *feature )
+{
+    bool *unicode = opaque;
+
+    if( strcasestr( feature, "UTF8" ) != NULL )
+        *unicode = true;
+}
+
+static const char *IsASCII( const char *str )
+{
+    int8_t c;
+    for( const char *p = str; (c = *p) != '\0'; p++ )
+        if( c < 0 )
+            return NULL;
+    return str;
+}
+
 static int Connect( vlc_object_t *p_access, access_sys_t *p_sys )
 {
     if( Login( p_access, p_sys ) < 0 )
@@ -351,17 +368,13 @@ static int Connect( vlc_object_t *p_access, access_sys_t *p_sys )
     if( ftp_SendCommand( p_access, p_sys, "EPSV ALL" ) < 0 )
     {
         msg_Err( p_access, "cannot request extended passive mode" );
-        net_Close( p_sys->fd_cmd );
-        return -1;
+        goto error;
     }
 
     if( ftp_RecvCommand( p_access, p_sys, NULL, NULL ) == 2 )
     {
         if( net_GetPeerAddress( p_sys->fd_cmd, p_sys->sz_epsv_ip, NULL ) )
-        {
-            net_Close( p_sys->fd_cmd );
-            return -1;
-        }
+            goto error;
     }
     else
     {
@@ -374,10 +387,23 @@ static int Connect( vlc_object_t *p_access, access_sys_t *p_sys )
         net_Close( p_sys->fd_cmd );
 
         if( Login( p_access, p_sys ) )
-        {
-            net_Close( p_sys->fd_cmd );
-            return -1;
-        }
+            goto error;
+    }
+
+    /* features check */
+    bool unicode = false;
+    if( ftp_SendCommand( p_access, p_sys, "FEAT" ) < 0
+     || ftp_RecvAnswer( p_access, p_sys, NULL, NULL,
+                        FeaturesCheck, &unicode ) < 0 )
+    {
+         msg_Err( p_access, "cannot get server features" );
+         goto error;
+    }
+
+    if( (unicode ? IsUTF8 : IsASCII)(p_sys->url.psz_path) == NULL )
+    {
+        msg_Err( p_access, "unsupported path: \"%s\"", p_sys->url.psz_path );
+        goto error;
     }
 
     /* check binary mode support */
@@ -385,11 +411,13 @@ static int Connect( vlc_object_t *p_access, access_sys_t *p_sys )
         ftp_RecvCommand( p_access, p_sys, NULL, NULL ) != 2 )
     {
         msg_Err( p_access, "cannot set binary transfer mode" );
-        net_Close( p_sys->fd_cmd );
-        return -1;
+        goto error;
     }
 
     return 0;
+error:
+    net_Close( p_sys->fd_cmd );
+    return -1;
 }
 
 
@@ -429,8 +457,6 @@ static int parseURL( vlc_url_t *url, const char *path )
             return VLC_EGENERIC; /* ASCII and directory not supported */
     }
     decode_URI( url->psz_path );
-    /* FIXME: check for UTF-8 support, otherwise only ASCII is allowed */
-    EnsureUTF8( url->psz_path );
     return VLC_SUCCESS;
 }