Skip to content

Commit

Permalink
sftp.c: ensure minimum read packet size
Browse files Browse the repository at this point in the history
For optimum performance we need to ensure we don't request tiny packets.
  • Loading branch information
jakob authored and bagder committed Feb 11, 2016
1 parent d7e25b4 commit 85dbd4c
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions src/sftp.c
Original file line number Diff line number Diff line change
Expand Up @@ -1367,13 +1367,18 @@ static ssize_t sftp_read(LIBSSH2_SFTP_HANDLE * handle, char *buffer,

while(count > 0) {
unsigned char *s;
uint32_t size = MIN(MAX_SFTP_READ_SIZE, count);

/* 25 = packet_len(4) + packet_type(1) + request_id(4) +
handle_len(4) + offset(8) + count(4) */
uint32_t packet_len = (uint32_t)handle->handle_len + 25;
uint32_t request_id;

uint32_t size = count;
if (size < buffer_size)
size = buffer_size;
if (size > MAX_SFTP_READ_SIZE)
size = MAX_SFTP_READ_SIZE;

chunk = LIBSSH2_ALLOC(session, packet_len +
sizeof(struct sftp_pipeline_chunk));
if (!chunk)
Expand All @@ -1399,8 +1404,8 @@ static ssize_t sftp_read(LIBSSH2_SFTP_HANDLE * handle, char *buffer,

/* add this new entry LAST in the list */
_libssh2_list_add(&handle->packet_list, &chunk->node);
count -= size; /* deduct the size we used, as we might have
to create more packets */
count -= MIN(size,count); /* deduct the size we used, as we might
* have to create more packets */
_libssh2_debug(session, LIBSSH2_TRACE_SFTP,
"read request id %d sent (offset: %d, size: %d)",
request_id, (int)chunk->offset, (int)chunk->len);
Expand Down

0 comments on commit 85dbd4c

Please sign in to comment.