[vlc-commits] smb2: build smb2 urls using memstream rather than complex printf construct

Pierre Lamot git at videolan.org
Wed Jun 3 12:06:55 CEST 2020


vlc | branch: master | Pierre Lamot <pierre at videolabs.io> | Thu May 28 15:34:56 2020 +0200| [9775ae34a9a44156b7de21d98f3662381c22df47] | committer: Pierre Lamot

smb2: build smb2 urls using memstream rather than complex printf construct

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

 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,



More information about the vlc-commits mailing list