mirror of
https://git.libssh.org/projects/libssh.git
synced 2026-02-11 18:50:28 +09:00
channels: Read into buffer directly in channel_read_buffer
to avoid allocate 8KB buffer from stack Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com> Reviewed-by: Jakub Jelen <jjelen@redhat.com> Change-Id: Ifc198705cb8ecec6f0a609f84965382dc151693b
This commit is contained in:
committed by
Andreas Schneider
parent
c027585a50
commit
3ab17e3fbd
@@ -2753,7 +2753,7 @@ error:
|
|||||||
int channel_read_buffer(ssh_channel channel, ssh_buffer buffer, uint32_t count,
|
int channel_read_buffer(ssh_channel channel, ssh_buffer buffer, uint32_t count,
|
||||||
int is_stderr) {
|
int is_stderr) {
|
||||||
ssh_session session;
|
ssh_session session;
|
||||||
char buffer_tmp[8192];
|
char *buffer_tmp = NULL;
|
||||||
int r;
|
int r;
|
||||||
uint32_t total=0;
|
uint32_t total=0;
|
||||||
|
|
||||||
@@ -2775,14 +2775,19 @@ int channel_read_buffer(ssh_channel channel, ssh_buffer buffer, uint32_t count,
|
|||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
if(r > 0){
|
if(r > 0){
|
||||||
|
count = r;
|
||||||
|
buffer_tmp = ssh_buffer_allocate(buffer, count);
|
||||||
|
if (buffer_tmp == NULL) {
|
||||||
|
ssh_set_error_oom(session);
|
||||||
|
return SSH_ERROR;
|
||||||
|
}
|
||||||
r=ssh_channel_read(channel, buffer_tmp, r, is_stderr);
|
r=ssh_channel_read(channel, buffer_tmp, r, is_stderr);
|
||||||
if(r < 0){
|
if(r < 0){
|
||||||
|
ssh_buffer_pass_bytes_end(buffer, count);
|
||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
if(ssh_buffer_add_data(buffer,buffer_tmp,r) < 0){
|
/* Rollback the unused space */
|
||||||
ssh_set_error_oom(session);
|
ssh_buffer_pass_bytes_end(buffer, count - r);
|
||||||
r = SSH_ERROR;
|
|
||||||
}
|
|
||||||
|
|
||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
@@ -2792,19 +2797,23 @@ int channel_read_buffer(ssh_channel channel, ssh_buffer buffer, uint32_t count,
|
|||||||
ssh_handle_packets(channel->session, SSH_TIMEOUT_INFINITE);
|
ssh_handle_packets(channel->session, SSH_TIMEOUT_INFINITE);
|
||||||
} while (r == 0);
|
} while (r == 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
buffer_tmp = ssh_buffer_allocate(buffer, count);
|
||||||
|
if (buffer_tmp == NULL) {
|
||||||
|
ssh_set_error_oom(session);
|
||||||
|
return SSH_ERROR;
|
||||||
|
}
|
||||||
while(total < count){
|
while(total < count){
|
||||||
r=ssh_channel_read(channel, buffer_tmp, sizeof(buffer_tmp), is_stderr);
|
r=ssh_channel_read(channel, buffer_tmp, count - total, is_stderr);
|
||||||
if(r<0){
|
if(r<0){
|
||||||
|
ssh_buffer_pass_bytes_end(buffer, count);
|
||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
if(r==0){
|
if(r==0){
|
||||||
|
/* Rollback the unused space */
|
||||||
|
ssh_buffer_pass_bytes_end(buffer, count - total);
|
||||||
return total;
|
return total;
|
||||||
}
|
}
|
||||||
if (ssh_buffer_add_data(buffer,buffer_tmp,r) < 0) {
|
|
||||||
ssh_set_error_oom(session);
|
|
||||||
|
|
||||||
return SSH_ERROR;
|
|
||||||
}
|
|
||||||
total += r;
|
total += r;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user