]> xenbits.xensource.com Git - libvirt.git/commitdiff
rpc: use virStringSplit instead of strsep
authorPeter Krempa <pkrempa@redhat.com>
Thu, 14 Nov 2019 08:59:52 +0000 (09:59 +0100)
committerPeter Krempa <pkrempa@redhat.com>
Thu, 14 Nov 2019 14:50:43 +0000 (15:50 +0100)
When parsing allowed authentication methods for the native ssh lib
transports we used strsep. Since we have virStringSplit helper let's use
that one.

Signed-off-by: Peter Krempa <pkrempa@redhat.com>
Reviewed-by: Pavel Hrdina <phrdina@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
src/rpc/virnetsocket.c

index c9b2a16fe55b648195190208d2e9a2b4f3cd414d..21ef7a803463329ac475644ba208f7c2afbb76f3 100644 (file)
@@ -944,9 +944,8 @@ virNetSocketNewConnectLibSSH2(const char *host,
     int ret = -1;
     int portN;
 
-    char *authMethodNext = NULL;
-    char *authMethodsCopy = NULL;
-    char *authMethod;
+    VIR_AUTOSTRINGLIST authMethodList = NULL;
+    char **authMethodNext;
 
     /* port number will be verified while opening the socket */
     if (virStrToLong_i(port, NULL, 10, &portN) < 0) {
@@ -987,11 +986,12 @@ virNetSocketNewConnectLibSSH2(const char *host,
     if (virNetSSHSessionSetChannelCommand(sess, command) != 0)
         goto error;
 
-    authMethodsCopy = g_strdup(authMethods);
+    if (!(authMethodList = virStringSplit(authMethods, ",", 0)))
+        goto error;
 
-    authMethodNext = authMethodsCopy;
+    for (authMethodNext = authMethodList; *authMethodNext; authMethodNext++) {
+        const char *authMethod = *authMethodNext;
 
-    while ((authMethod = strsep(&authMethodNext, ","))) {
         if (STRCASEEQ(authMethod, "keyboard-interactive")) {
             ret = virNetSSHSessionAuthAddKeyboardAuth(sess, username, -1);
         } else if (STRCASEEQ(authMethod, "password")) {
@@ -1028,13 +1028,11 @@ virNetSocketNewConnectLibSSH2(const char *host,
     sock->sshSession = sess;
     *retsock = sock;
 
-    VIR_FREE(authMethodsCopy);
     return 0;
 
  error:
     virObjectUnref(sock);
     virObjectUnref(sess);
-    VIR_FREE(authMethodsCopy);
     return ret;
 }
 #else
@@ -1079,9 +1077,8 @@ virNetSocketNewConnectLibssh(const char *host,
     int ret = -1;
     int portN;
 
-    char *authMethodNext = NULL;
-    char *authMethodsCopy = NULL;
-    char *authMethod;
+    VIR_AUTOSTRINGLIST authMethodList = NULL;
+    char **authMethodNext;
 
     /* port number will be verified while opening the socket */
     if (virStrToLong_i(port, NULL, 10, &portN) < 0) {
@@ -1121,11 +1118,12 @@ virNetSocketNewConnectLibssh(const char *host,
     if (virNetLibsshSessionSetChannelCommand(sess, command) != 0)
         goto error;
 
-    authMethodsCopy = g_strdup(authMethods);
+    if (!(authMethodList = virStringSplit(authMethods, ",", 0)))
+        goto error;
 
-    authMethodNext = authMethodsCopy;
+    for (authMethodNext = authMethodList; *authMethodNext; authMethodNext++) {
+        const char *authMethod = *authMethodNext;
 
-    while ((authMethod = strsep(&authMethodNext, ","))) {
         if (STRCASEEQ(authMethod, "keyboard-interactive")) {
             ret = virNetLibsshSessionAuthAddKeyboardAuth(sess, -1);
         } else if (STRCASEEQ(authMethod, "password")) {
@@ -1164,13 +1162,11 @@ virNetSocketNewConnectLibssh(const char *host,
     sock->ownsFd = false;
     *retsock = sock;
 
-    VIR_FREE(authMethodsCopy);
     return 0;
 
  error:
     virObjectUnref(sock);
     virObjectUnref(sess);
-    VIR_FREE(authMethodsCopy);
     return ret;
 }
 #else