Cleanup memory leak in ssh transport
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
diff --git a/src/transports/ssh.c b/src/transports/ssh.c
index 39e4e3d..1f6716f 100644
--- a/src/transports/ssh.c
+++ b/src/transports/ssh.c
@@ -457,27 +457,31 @@ static int _git_ssh_setup_conn(
LIBSSH2_SESSION* session=NULL;
LIBSSH2_CHANNEL* channel=NULL;
+ t->current_stream = NULL;
+
*stream = NULL;
if (ssh_stream_alloc(t, url, cmd, stream) < 0)
return -1;
s = (ssh_stream *)*stream;
+ s->session = NULL;
+ s->channel = NULL;
if (!git__prefixcmp(url, prefix_ssh)) {
if ((error = gitno_extract_url_parts(&host, &port, &path, &user, &pass, url, default_port)) < 0)
- goto on_error;
+ goto done;
} else {
if ((error = git_ssh_extract_url_parts(&host, &user, url)) < 0)
- goto on_error;
+ goto done;
port = git__strdup(default_port);
GITERR_CHECK_ALLOC(port);
}
if ((error = gitno_connect(&s->socket, host, port, 0)) < 0)
- goto on_error;
+ goto done;
if ((error = _git_ssh_session_create(&session, s->socket)) < 0)
- goto on_error;
+ goto done;
if (t->owner->certificate_check_cb != NULL) {
git_cert_hostkey cert = { 0 }, *cert_ptr;
@@ -499,7 +503,8 @@ static int _git_ssh_setup_conn(
if (cert.type == 0) {
giterr_set(GITERR_SSH, "unable to get the host key");
- return -1;
+ error = -1;
+ goto done;
}
/* We don't currently trust any hostkeys */
@@ -512,27 +517,27 @@ static int _git_ssh_setup_conn(
if (!giterr_last())
giterr_set(GITERR_NET, "user cancelled hostkey check");
- goto on_error;
+ goto done;
}
- }
+ }
/* we need the username to ask for auth methods */
if (!user) {
if ((error = request_creds(&cred, t, NULL, GIT_CREDTYPE_USERNAME)) < 0)
- goto on_error;
+ goto done;
user = git__strdup(((git_cred_username *) cred)->username);
cred->free(cred);
cred = NULL;
if (!user)
- goto on_error;
+ goto done;
} else if (user && pass) {
if ((error = git_cred_userpass_plaintext_new(&cred, user, pass)) < 0)
- goto on_error;
+ goto done;
}
if ((error = list_auth_methods(&auth_methods, session, user)) < 0)
- goto on_error;
+ goto done;
error = GIT_EAUTH;
/* if we already have something to try */
@@ -546,25 +551,25 @@ static int _git_ssh_setup_conn(
}
if ((error = request_creds(&cred, t, user, auth_methods)) < 0)
- goto on_error;
+ goto done;
if (strcmp(user, git_cred__username(cred))) {
giterr_set(GITERR_SSH, "username does not match previous request");
error = -1;
- goto on_error;
+ goto done;
}
error = _git_ssh_authenticate_session(session, cred);
}
if (error < 0)
- goto on_error;
+ goto done;
channel = libssh2_channel_open_session(session);
if (!channel) {
error = -1;
ssh_error(session, "Failed to open SSH channel");
- goto on_error;
+ goto done;
}
libssh2_channel_set_blocking(channel, 1);
@@ -573,36 +578,25 @@ static int _git_ssh_setup_conn(
s->channel = channel;
t->current_stream = s;
- if (cred)
- cred->free(cred);
- git__free(host);
- git__free(port);
- git__free(path);
- git__free(user);
- git__free(pass);
-
- return 0;
+done:
+ if (error < 0) {
+ if (*stream)
+ ssh_stream_free(*stream);
-on_error:
- s->session = NULL;
- s->channel = NULL;
- t->current_stream = NULL;
-
- if (*stream)
- ssh_stream_free(*stream);
+ if (session)
+ libssh2_session_free(session);
+ }
if (cred)
cred->free(cred);
git__free(host);
git__free(port);
+ git__free(path);
git__free(user);
git__free(pass);
- if (session)
- libssh2_session_free(session);
-
return error;
}