unc: chop before rather than after string copy
authorRémi Denis-Courmont <remi@remlab.net>
Sun, 23 Feb 2020 14:09:32 +0000 (16:09 +0200)
committerRémi Denis-Courmont <remi@remlab.net>
Sun, 23 Feb 2020 14:11:59 +0000 (16:11 +0200)
modules/access/unc.c

index fbb3a58..92a3905 100644 (file)
@@ -56,14 +56,18 @@ static void Win32AddConnection(stream_t *access, const char *server,
 {
     char *remote_name;
 
-    if (asprintf(&remote_name, "\\\\%s\\%s", server,
-                 (share != NULL) ? share + 1 /* skip leading '/' */: "") < 0)
-        return;
-
-    /* remove trailings '/' */
-    char *delim = strchr(remote_name, '/');
-    if (delim != NULL)
-        *delim = '\0';
+    if (share != NULL)
+    {   /* skip leading and remove trailing slashes */
+        int slen = strcspn(++share, "/");
+
+        if (asprintf(&remote_name, "\\\\%s\\%.*s", server, slen, share) < 0)
+            return;
+    }
+    else
+    {
+        if (asprintf(&remote_name, "\\\\%s\\", server) < 0)
+            return;
+    }
 
     NETRESOURCE net_resource = {
         .dwType = RESOURCETYPE_DISK,