[vlc-commits] es: use vlc_memstream instead of unchecked sprintf()

Rémi Denis-Courmont git at videolan.org
Fri Feb 24 20:32:44 CET 2017


vlc | branch: master | Rémi Denis-Courmont <remi at remlab.net> | Fri Feb 24 21:32:29 2017 +0200| [e9804d78697631dfd6368a0a638d8269277a7c21] | committer: Rémi Denis-Courmont

es: use vlc_memstream instead of unchecked sprintf()

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

 modules/stream_out/es.c | 78 +++++++++++++++++++++----------------------------
 1 file changed, 33 insertions(+), 45 deletions(-)

diff --git a/modules/stream_out/es.c b/modules/stream_out/es.c
index 6372dc5..a03c402 100644
--- a/modules/stream_out/es.c
+++ b/modules/stream_out/es.c
@@ -34,6 +34,7 @@
 #include <vlc_input.h>
 #include <vlc_sout.h>
 #include <vlc_dialog.h>
+#include <vlc_memstream.h>
 
 /*****************************************************************************
  * Module descriptor
@@ -211,62 +212,49 @@ struct sout_stream_id_sys_t
 static char * es_print_url( const char *psz_fmt, vlc_fourcc_t i_fourcc, int i_count,
                             const char *psz_access, const char *psz_mux )
 {
-    char *psz_dst, *p;
+    struct vlc_memstream stream;
+    unsigned char c;
+
+    if (vlc_memstream_open(&stream))
+        return NULL;
 
     if( psz_fmt == NULL || !*psz_fmt )
-    {
-        psz_fmt = (char*)"stream-%n-%c.%m";
-    }
+        psz_fmt = "stream-%n-%c.%m";
 
-    p = psz_dst = malloc( 4096 );
-    if( !psz_dst )
-        return NULL;
-    memset( p, 0, 4096 );
-    for( ;; )
+    while ((c = *(psz_fmt++)) != '\0')
     {
-        if( *psz_fmt == '\0' )
+        if (c != '%')
         {
-            *p = '\0';
-            break;
+            vlc_memstream_putc(&stream, c);
+            continue;
         }
 
-        if( *psz_fmt != '%' )
-        {
-            *p++ = *psz_fmt++;
-        }
-        else
+        switch (c = *(psz_fmt++))
         {
-            if( psz_fmt[1] == 'n' )
-            {
-                p += sprintf( p, "%d", i_count );
-            }
-            else if( psz_fmt[1] == 'c' )
-            {
-                p += sprintf( p, "%4.4s", (char*)&i_fourcc );
-            }
-            else if( psz_fmt[1] == 'm' )
-            {
-                p += sprintf( p, "%s", psz_mux );
-            }
-            else if( psz_fmt[1] == 'a' )
-            {
-                p += sprintf( p, "%s", psz_access );
-            }
-            else if( psz_fmt[1] != '\0' )
-            {
-                p += sprintf( p, "%c%c", psz_fmt[0], psz_fmt[1] );
-            }
-            else
-            {
-                p += sprintf( p, "%c", psz_fmt[0] );
-                *p++ = '\0';
+            case 'n':
+                vlc_memstream_printf(&stream, "%d", i_count);
+                break;
+            case 'c':
+                vlc_memstream_printf(&stream, "%4.4s", (char *)&i_fourcc);
+                break;
+            case 'm':
+                vlc_memstream_puts(&stream, psz_mux);
+                break;
+            case 'a':
+                vlc_memstream_puts(&stream, psz_access);
+                break;
+            case '\0':
+                vlc_memstream_putc(&stream, '%');
+                goto out;
+            default:
+                vlc_memstream_printf(&stream, "%%%c", (int) c);
                 break;
-            }
-            psz_fmt += 2;
         }
     }
-
-    return( psz_dst );
+out:
+    if (vlc_memstream_close(&stream))
+        return NULL;
+    return stream.ptr;
 }
 
 static sout_stream_id_sys_t *Add( sout_stream_t *p_stream, const es_format_t *p_fmt )



More information about the vlc-commits mailing list