[vlc-devel] [PATCH v3 2/4] smb2: build smb2 urls using memstream rather than complex printf construct

Pierre Lamot pierre at videolabs.io
Fri May 29 10:57:00 CEST 2020


---
 modules/access/smb2.c | 31 +++++++++++++++++++++----------
 1 file changed, 21 insertions(+), 10 deletions(-)

diff --git a/modules/access/smb2.c b/modules/access/smb2.c
index d287493e4f..17063c8406 100644
--- a/modules/access/smb2.c
+++ b/modules/access/smb2.c
@@ -42,6 +42,7 @@
 #include <vlc_keystore.h>
 #include <vlc_interrupt.h>
 #include <vlc_network.h>
+#include <vlc_memstream.h>
 
 #include <smb2/smb2.h>
 #include <smb2/libsmb2.h>
@@ -323,17 +324,27 @@ static char *
 vlc_smb2_get_url(vlc_url_t *url, const char *file)
 {
     /* smb2://<psz_host><psz_path><file>?<psz_option> */
-    char *buf;
-    if (asprintf(&buf, "smb://%s%s%s%s%s%s", url->psz_host,
-                 url->psz_path != NULL ? url->psz_path : "",
-                 url->psz_path != NULL && url->psz_path[0] != '\0' &&
-                 url->psz_path[strlen(url->psz_path) - 1] != '/' ? "/" : "",
-                 file,
-                 url->psz_option != NULL ? "?" : "",
-                 url->psz_option != NULL ? url->psz_option : "") == -1)
-        return NULL;
+    struct vlc_memstream buf;
+    vlc_memstream_open(&buf);
+    vlc_memstream_printf(&buf, "smb://%s", url->psz_host);
+
+    if (url->psz_path != NULL)
+    {
+        vlc_memstream_puts(&buf, url->psz_path);
+        if (url->psz_path[0] != '\0' && url->psz_path[strlen(url->psz_path) - 1] != '/')
+            vlc_memstream_putc(&buf, '/');
+    }
     else
-        return buf;
+        vlc_memstream_putc(&buf, '/');
+
+    vlc_memstream_puts(&buf, file);
+
+    if (url->psz_option)
+        vlc_memstream_printf(&buf, "?%s", url->psz_option);
+
+    if (vlc_memstream_close(&buf))
+        return NULL;
+    return buf.ptr;
 }
 
 static int AddItem(stream_t *access, struct vlc_readdir_helper *rdh,
-- 
2.25.1



More information about the vlc-devel mailing list