From 69455eafe7f4d1d54c598110757f99b59225db1e Mon Sep 17 00:00:00 2001 From: John Safranek Date: Tue, 29 Jan 2019 16:06:07 -0800 Subject: [PATCH] When reading the data from the server during wolfSSH_SFTP_Get(), make sure to get all the data from the server if the read_stream() doesn't provide all that is present. --- src/wolfsftp.c | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/src/wolfsftp.c b/src/wolfsftp.c index 7dd6c6b..98b05f6 100644 --- a/src/wolfsftp.c +++ b/src/wolfsftp.c @@ -291,6 +291,7 @@ typedef struct WS_SFTP_SEND_READ_STATE { word32 reqId; word32 idx; word32 sz; + word32 recvSz; byte type; } WS_SFTP_SEND_READ_STATE; @@ -5746,11 +5747,24 @@ int wolfSSH_SFTP_SendReadPacket(WOLFSSH* ssh, byte* handle, word32 handleSz, case STATE_SEND_READ_REMAINDER: WLOG(WS_LOG_SFTP, "SFTP SEND_READ STATE: READ_REMAINDER"); - ret = wolfSSH_stream_read(ssh, out, state->sz); - if (ret < 0) { - return ret; - } - ret = state->sz; + do { + ret = wolfSSH_stream_read(ssh, + out + state->recvSz, state->sz); + if (ret < 0) { + if (ssh->error == WS_WANT_READ || + ssh->error == WS_WANT_WRITE) { + return WS_FATAL_ERROR; + } + WLOG(WS_LOG_SFTP, "Error reading remainder of data"); + state->state = STATE_SEND_READ_CLEANUP; + continue; + } + + state->recvSz += ret; + state->sz -= ret; + } while (state->sz != 0); + + ret = state->recvSz; state->state = STATE_SEND_READ_CLEANUP; continue;