From 84fd9104232e94a7d71ee55df703e8e2e1b07490 Mon Sep 17 00:00:00 2001 From: Andreas Schneider Date: Sat, 8 Sep 2018 09:29:57 +0200 Subject: [PATCH] sftp: Allocate a new buffer in sftp_packet_read() if needed We will move the buffer to the message instead of duplicating the memory. Signed-off-by: Andreas Schneider (cherry picked from commit be8302e2f3277bd25c305fb6fc14063e0e4ec945) --- src/sftp.c | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/src/sftp.c b/src/sftp.c index fdcffcd7..a1f1062e 100644 --- a/src/sftp.c +++ b/src/sftp.c @@ -339,10 +339,22 @@ sftp_packet sftp_packet_read(sftp_session sftp) packet->sftp = sftp; - rc = ssh_buffer_reinit(packet->payload); - if (rc != 0) { - ssh_set_error_oom(sftp->session); - return NULL; + /* + * If the packet has a payload, then just reinit the buffer, otherwise + * allocate a new one. + */ + if (packet->payload != NULL) { + rc = ssh_buffer_reinit(packet->payload); + if (rc != 0) { + ssh_set_error_oom(sftp->session); + return NULL; + } + } else { + packet->payload = ssh_buffer_new(); + if (packet->payload == NULL) { + ssh_set_error_oom(sftp->session); + return NULL; + } } nread = 0;