From 0762057eb97e61bebd138f94cef5c03e213abfe2 Mon Sep 17 00:00:00 2001 From: Andreas Schneider Date: Sat, 8 Sep 2018 09:36:14 +0200 Subject: [PATCH] sftp: Move the packet payload to the message This reduces memory allocations and copying. Signed-off-by: Andreas Schneider --- src/sftp.c | 36 +++++++----------------------------- 1 file changed, 7 insertions(+), 29 deletions(-) diff --git a/src/sftp.c b/src/sftp.c index c1e51fcd..4b310acf 100644 --- a/src/sftp.c +++ b/src/sftp.c @@ -451,26 +451,6 @@ int sftp_get_error(sftp_session sftp) { return sftp->errnum; } -static sftp_message sftp_message_new(sftp_session sftp){ - sftp_message msg = NULL; - - msg = calloc(1, sizeof(struct sftp_message_struct)); - if (msg == NULL) { - ssh_set_error_oom(sftp->session); - return NULL; - } - - msg->payload = ssh_buffer_new(); - if (msg->payload == NULL) { - ssh_set_error_oom(sftp->session); - SAFE_FREE(msg); - return NULL; - } - msg->sftp = sftp; - - return msg; -} - static void sftp_message_free(sftp_message msg) { if (msg == NULL) { @@ -503,15 +483,20 @@ static sftp_message sftp_get_message(sftp_packet packet) return NULL; } - msg = sftp_message_new(sftp); + msg = calloc(1, sizeof(struct sftp_message_struct)); if (msg == NULL) { + ssh_set_error_oom(sftp->session); return NULL; } msg->sftp = packet->sftp; msg->packet_type = packet->type; - rc = ssh_buffer_unpack(packet->payload, "d", &msg->id); + /* Move the payload from the packet to the message */ + msg->payload = packet->payload; + packet->payload = NULL; + + rc = ssh_buffer_unpack(msg->payload, "d", &msg->id); if (rc != SSH_OK) { ssh_set_error(packet->sftp->session, SSH_FATAL, "Invalid packet %d: no ID", packet->type); @@ -524,13 +509,6 @@ static sftp_message sftp_get_message(sftp_packet packet) msg->id, msg->packet_type); - if (ssh_buffer_add_data(msg->payload, ssh_buffer_get(packet->payload), - ssh_buffer_get_len(packet->payload)) < 0) { - ssh_set_error_oom(sftp->session); - sftp_message_free(msg); - return NULL; - } - return msg; }