1
0
mirror of https://git.libssh.org/projects/libssh.git synced 2025-11-30 13:01:23 +03:00

options: Allocate dest in ssh_options_copy().

This commit is contained in:
Andreas Schneider
2011-08-11 12:53:02 +02:00
parent 1c30c2a510
commit a4a1af5bbf

View File

@@ -53,7 +53,8 @@
* *
* @param src The session to use to copy the options. * @param src The session to use to copy the options.
* *
* @param dest The session to copy the options to. * @param dest A pointer to store the allocated session with duplicated
* options. You have to free the memory.
* *
* @returns 0 on sucess, -1 on error with errno set. * @returns 0 on sucess, -1 on error with errno set.
* *
@@ -63,15 +64,19 @@ int ssh_options_copy(ssh_session src, ssh_session *dest) {
ssh_session new; ssh_session new;
int i; int i;
if (src == NULL || dest == NULL || *dest == NULL) { if (src == NULL || dest == NULL) {
return -1; return -1;
} }
new = *dest; new = ssh_new();
if (new == NULL) {
return -1;
}
if (src->username) { if (src->username) {
new->username = strdup(src->username); new->username = strdup(src->username);
if (new->username == NULL) { if (new->username == NULL) {
ssh_free(new);
return -1; return -1;
} }
} }
@@ -79,6 +84,7 @@ int ssh_options_copy(ssh_session src, ssh_session *dest) {
if (src->host) { if (src->host) {
new->host = strdup(src->host); new->host = strdup(src->host);
if (new->host == NULL) { if (new->host == NULL) {
ssh_free(new);
return -1; return -1;
} }
} }
@@ -88,6 +94,7 @@ int ssh_options_copy(ssh_session src, ssh_session *dest) {
new->identity = ssh_list_new(); new->identity = ssh_list_new();
if (new->identity == NULL) { if (new->identity == NULL) {
ssh_free(new);
return -1; return -1;
} }
@@ -98,11 +105,13 @@ int ssh_options_copy(ssh_session src, ssh_session *dest) {
id = strdup((char *) it->data); id = strdup((char *) it->data);
if (id == NULL) { if (id == NULL) {
ssh_free(new);
return -1; return -1;
} }
rc = ssh_list_append(new->identity, id); rc = ssh_list_append(new->identity, id);
if (rc < 0) { if (rc < 0) {
ssh_free(new);
return -1; return -1;
} }
it = it->next; it = it->next;
@@ -112,6 +121,7 @@ int ssh_options_copy(ssh_session src, ssh_session *dest) {
if (src->sshdir) { if (src->sshdir) {
new->sshdir = strdup(src->sshdir); new->sshdir = strdup(src->sshdir);
if (new->sshdir == NULL) { if (new->sshdir == NULL) {
ssh_free(new);
return -1; return -1;
} }
} }
@@ -119,6 +129,7 @@ int ssh_options_copy(ssh_session src, ssh_session *dest) {
if (src->knownhosts) { if (src->knownhosts) {
new->knownhosts = strdup(src->knownhosts); new->knownhosts = strdup(src->knownhosts);
if (new->knownhosts == NULL) { if (new->knownhosts == NULL) {
ssh_free(new);
return -1; return -1;
} }
} }
@@ -127,6 +138,7 @@ int ssh_options_copy(ssh_session src, ssh_session *dest) {
if (src->wanted_methods[i]) { if (src->wanted_methods[i]) {
new->wanted_methods[i] = strdup(src->wanted_methods[i]); new->wanted_methods[i] = strdup(src->wanted_methods[i]);
if (new->wanted_methods[i] == NULL) { if (new->wanted_methods[i] == NULL) {
ssh_free(new);
return -1; return -1;
} }
} }
@@ -135,6 +147,7 @@ int ssh_options_copy(ssh_session src, ssh_session *dest) {
if(src->ProxyCommand) { if(src->ProxyCommand) {
new->ProxyCommand = strdup(src->ProxyCommand); new->ProxyCommand = strdup(src->ProxyCommand);
if(new->ProxyCommand == NULL) if(new->ProxyCommand == NULL)
ssh_free(new);
return -1; return -1;
} }
new->fd = src->fd; new->fd = src->fd;
@@ -147,6 +160,8 @@ int ssh_options_copy(ssh_session src, ssh_session *dest) {
new->common.log_verbosity = src->common.log_verbosity; new->common.log_verbosity = src->common.log_verbosity;
new->compressionlevel = src->compressionlevel; new->compressionlevel = src->compressionlevel;
*dest = new;
return 0; return 0;
} }