From 79a3fcac72fb513b1759fe30348db7b71dba68ae Mon Sep 17 00:00:00 2001 From: Andreas Schneider Date: Mon, 3 Sep 2018 14:24:40 +0200 Subject: [PATCH] sftp: Keep a ssh_packet for reading in the sftp handle Signed-off-by: Andreas Schneider --- include/libssh/sftp.h | 1 + src/sftp.c | 44 ++++++++++++++++++++++++++++--------------- 2 files changed, 30 insertions(+), 15 deletions(-) diff --git a/include/libssh/sftp.h b/include/libssh/sftp.h index b07f269f..eeb4e615 100644 --- a/include/libssh/sftp.h +++ b/include/libssh/sftp.h @@ -84,6 +84,7 @@ struct sftp_session_struct { int errnum; void **handles; sftp_ext ext; + sftp_packet read_packet; }; struct sftp_packet_struct { diff --git a/src/sftp.c b/src/sftp.c index 81e3cc09..44c760d4 100644 --- a/src/sftp.c +++ b/src/sftp.c @@ -109,36 +109,48 @@ sftp_session sftp_new(ssh_session session) sftp->ext = sftp_ext_new(); if (sftp->ext == NULL) { - ssh_set_error_oom(session); - SAFE_FREE(sftp); + goto error; + } - return NULL; + sftp->read_packet = calloc(1, sizeof(struct sftp_packet_struct)); + if (sftp->read_packet == NULL) { + goto error; + } + + sftp->read_packet->payload = ssh_buffer_new(); + if (sftp->read_packet->payload == NULL) { + goto error; } sftp->session = session; sftp->channel = ssh_channel_new(session); if (sftp->channel == NULL) { - sftp_ext_free(sftp->ext); - SAFE_FREE(sftp); - - return NULL; + goto error; } if (ssh_channel_open_session(sftp->channel)) { - ssh_channel_free(sftp->channel); - sftp_ext_free(sftp->ext); - SAFE_FREE(sftp); - - return NULL; + goto error; } if (ssh_channel_request_sftp(sftp->channel)) { - sftp_free(sftp); - - return NULL; + goto error; } return sftp; +error: + ssh_set_error_oom(session); + if (sftp->ext != NULL) { + sftp_ext_free(sftp->ext); + } + if (sftp->channel != NULL) { + ssh_channel_free(sftp->channel); + } + if (sftp->read_packet->payload != NULL) { + ssh_buffer_free(sftp->read_packet->payload); + } + SAFE_FREE(sftp->read_packet); + SAFE_FREE(sftp); + return NULL; } sftp_session sftp_new_channel(ssh_session session, ssh_channel channel){ @@ -265,6 +277,8 @@ void sftp_free(sftp_session sftp) ssh_channel_free(sftp->channel); SAFE_FREE(sftp->handles); + SSH_BUFFER_FREE(sftp->read_packet->payload); + SAFE_FREE(sftp->read_packet); sftp_ext_free(sftp->ext);