mirror of
https://git.libssh.org/projects/libssh.git
synced 2025-11-27 13:21:11 +03:00
sftp: Use calloc() instead of malloc()/memset()
This is calloc() is faster then calling memset(). Signed-off-by: Andreas Schneider <asn@cryptomilk.org>
This commit is contained in:
40
src/sftp.c
40
src/sftp.c
@@ -65,11 +65,10 @@ static void status_msg_free(sftp_status_message status);
|
|||||||
static sftp_ext sftp_ext_new(void) {
|
static sftp_ext sftp_ext_new(void) {
|
||||||
sftp_ext ext;
|
sftp_ext ext;
|
||||||
|
|
||||||
ext = malloc(sizeof(struct sftp_ext_struct));
|
ext = calloc(1, sizeof(struct sftp_ext_struct));
|
||||||
if (ext == NULL) {
|
if (ext == NULL) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
ZERO_STRUCTP(ext);
|
|
||||||
|
|
||||||
return ext;
|
return ext;
|
||||||
}
|
}
|
||||||
@@ -100,13 +99,12 @@ sftp_session sftp_new(ssh_session session){
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
sftp = malloc(sizeof(struct sftp_session_struct));
|
sftp = calloc(1, sizeof(struct sftp_session_struct));
|
||||||
if (sftp == NULL) {
|
if (sftp == NULL) {
|
||||||
ssh_set_error_oom(session);
|
ssh_set_error_oom(session);
|
||||||
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
ZERO_STRUCTP(sftp);
|
|
||||||
|
|
||||||
sftp->ext = sftp_ext_new();
|
sftp->ext = sftp_ext_new();
|
||||||
if (sftp->ext == NULL) {
|
if (sftp->ext == NULL) {
|
||||||
@@ -149,13 +147,12 @@ sftp_session sftp_new_channel(ssh_session session, ssh_channel channel){
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
sftp = malloc(sizeof(struct sftp_session_struct));
|
sftp = calloc(1, sizeof(struct sftp_session_struct));
|
||||||
if (sftp == NULL) {
|
if (sftp == NULL) {
|
||||||
ssh_set_error_oom(session);
|
ssh_set_error_oom(session);
|
||||||
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
ZERO_STRUCTP(sftp);
|
|
||||||
|
|
||||||
sftp->ext = sftp_ext_new();
|
sftp->ext = sftp_ext_new();
|
||||||
if (sftp->ext == NULL) {
|
if (sftp->ext == NULL) {
|
||||||
@@ -175,12 +172,11 @@ sftp_session sftp_new_channel(ssh_session session, ssh_channel channel){
|
|||||||
sftp_session sftp_server_new(ssh_session session, ssh_channel chan){
|
sftp_session sftp_server_new(ssh_session session, ssh_channel chan){
|
||||||
sftp_session sftp = NULL;
|
sftp_session sftp = NULL;
|
||||||
|
|
||||||
sftp = malloc(sizeof(struct sftp_session_struct));
|
sftp = calloc(1, sizeof(struct sftp_session_struct));
|
||||||
if (sftp == NULL) {
|
if (sftp == NULL) {
|
||||||
ssh_set_error_oom(session);
|
ssh_set_error_oom(session);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
ZERO_STRUCTP(sftp);
|
|
||||||
|
|
||||||
sftp->session = session;
|
sftp->session = session;
|
||||||
sftp->channel = chan;
|
sftp->channel = chan;
|
||||||
@@ -309,7 +305,7 @@ sftp_packet sftp_packet_read(sftp_session sftp) {
|
|||||||
size_t size;
|
size_t size;
|
||||||
int r, s;
|
int r, s;
|
||||||
|
|
||||||
packet = malloc(sizeof(struct sftp_packet_struct));
|
packet = calloc(1, sizeof(struct sftp_packet_struct));
|
||||||
if (packet == NULL) {
|
if (packet == NULL) {
|
||||||
ssh_set_error_oom(sftp->session);
|
ssh_set_error_oom(sftp->session);
|
||||||
return NULL;
|
return NULL;
|
||||||
@@ -398,12 +394,11 @@ int sftp_get_error(sftp_session sftp) {
|
|||||||
static sftp_message sftp_message_new(sftp_session sftp){
|
static sftp_message sftp_message_new(sftp_session sftp){
|
||||||
sftp_message msg = NULL;
|
sftp_message msg = NULL;
|
||||||
|
|
||||||
msg = malloc(sizeof(struct sftp_message_struct));
|
msg = calloc(1, sizeof(struct sftp_message_struct));
|
||||||
if (msg == NULL) {
|
if (msg == NULL) {
|
||||||
ssh_set_error_oom(sftp->session);
|
ssh_set_error_oom(sftp->session);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
ZERO_STRUCTP(msg);
|
|
||||||
|
|
||||||
msg->payload = ssh_buffer_new();
|
msg->payload = ssh_buffer_new();
|
||||||
if (msg->payload == NULL) {
|
if (msg->payload == NULL) {
|
||||||
@@ -663,12 +658,11 @@ int sftp_extension_supported(sftp_session sftp, const char *name,
|
|||||||
static sftp_request_queue request_queue_new(sftp_message msg) {
|
static sftp_request_queue request_queue_new(sftp_message msg) {
|
||||||
sftp_request_queue queue = NULL;
|
sftp_request_queue queue = NULL;
|
||||||
|
|
||||||
queue = malloc(sizeof(struct sftp_request_queue_struct));
|
queue = calloc(1, sizeof(struct sftp_request_queue_struct));
|
||||||
if (queue == NULL) {
|
if (queue == NULL) {
|
||||||
ssh_set_error_oom(msg->sftp->session);
|
ssh_set_error_oom(msg->sftp->session);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
ZERO_STRUCTP(queue);
|
|
||||||
|
|
||||||
queue->message = msg;
|
queue->message = msg;
|
||||||
|
|
||||||
@@ -766,12 +760,11 @@ static sftp_status_message parse_status_msg(sftp_message msg){
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
status = malloc(sizeof(struct sftp_status_message_struct));
|
status = calloc(1, sizeof(struct sftp_status_message_struct));
|
||||||
if (status == NULL) {
|
if (status == NULL) {
|
||||||
ssh_set_error_oom(msg->sftp->session);
|
ssh_set_error_oom(msg->sftp->session);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
ZERO_STRUCTP(status);
|
|
||||||
|
|
||||||
status->id = msg->id;
|
status->id = msg->id;
|
||||||
rc = ssh_buffer_unpack(msg->payload, "d",
|
rc = ssh_buffer_unpack(msg->payload, "d",
|
||||||
@@ -825,12 +818,11 @@ static sftp_file parse_handle_msg(sftp_message msg){
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
file = malloc(sizeof(struct sftp_file_struct));
|
file = calloc(1, sizeof(struct sftp_file_struct));
|
||||||
if (file == NULL) {
|
if (file == NULL) {
|
||||||
ssh_set_error_oom(msg->sftp->session);
|
ssh_set_error_oom(msg->sftp->session);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
ZERO_STRUCTP(file);
|
|
||||||
|
|
||||||
file->handle = ssh_buffer_get_ssh_string(msg->payload);
|
file->handle = ssh_buffer_get_ssh_string(msg->payload);
|
||||||
if (file->handle == NULL) {
|
if (file->handle == NULL) {
|
||||||
@@ -910,13 +902,12 @@ sftp_dir sftp_opendir(sftp_session sftp, const char *path){
|
|||||||
file = parse_handle_msg(msg);
|
file = parse_handle_msg(msg);
|
||||||
sftp_message_free(msg);
|
sftp_message_free(msg);
|
||||||
if (file != NULL) {
|
if (file != NULL) {
|
||||||
dir = malloc(sizeof(struct sftp_dir_struct));
|
dir = calloc(1, sizeof(struct sftp_dir_struct));
|
||||||
if (dir == NULL) {
|
if (dir == NULL) {
|
||||||
ssh_set_error_oom(sftp->session);
|
ssh_set_error_oom(sftp->session);
|
||||||
free(file);
|
free(file);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
ZERO_STRUCTP(dir);
|
|
||||||
|
|
||||||
dir->sftp = sftp;
|
dir->sftp = sftp;
|
||||||
dir->name = strdup(path);
|
dir->name = strdup(path);
|
||||||
@@ -954,12 +945,11 @@ static sftp_attributes sftp_parse_attr_4(sftp_session sftp, ssh_buffer buf,
|
|||||||
/* unused member variable */
|
/* unused member variable */
|
||||||
(void) expectnames;
|
(void) expectnames;
|
||||||
|
|
||||||
attr = malloc(sizeof(struct sftp_attributes_struct));
|
attr = calloc(1, sizeof(struct sftp_attributes_struct));
|
||||||
if (attr == NULL) {
|
if (attr == NULL) {
|
||||||
ssh_set_error_oom(sftp->session);
|
ssh_set_error_oom(sftp->session);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
ZERO_STRUCTP(attr);
|
|
||||||
|
|
||||||
/* This isn't really a loop, but it is like a try..catch.. */
|
/* This isn't really a loop, but it is like a try..catch.. */
|
||||||
do {
|
do {
|
||||||
@@ -1150,7 +1140,7 @@ static char *sftp_parse_longname(const char *longname,
|
|||||||
|
|
||||||
/* There is no strndup on windows */
|
/* There is no strndup on windows */
|
||||||
len = q - p + 1;
|
len = q - p + 1;
|
||||||
x = malloc(len);
|
x = calloc(1, len);
|
||||||
if (x == NULL) {
|
if (x == NULL) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
@@ -1180,12 +1170,11 @@ static sftp_attributes sftp_parse_attr_3(sftp_session sftp, ssh_buffer buf,
|
|||||||
sftp_attributes attr;
|
sftp_attributes attr;
|
||||||
int rc;
|
int rc;
|
||||||
|
|
||||||
attr = malloc(sizeof(struct sftp_attributes_struct));
|
attr = calloc(1, sizeof(struct sftp_attributes_struct));
|
||||||
if (attr == NULL) {
|
if (attr == NULL) {
|
||||||
ssh_set_error_oom(sftp->session);
|
ssh_set_error_oom(sftp->session);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
ZERO_STRUCTP(attr);
|
|
||||||
|
|
||||||
if (expectname) {
|
if (expectname) {
|
||||||
rc = ssh_buffer_unpack(buf, "ss",
|
rc = ssh_buffer_unpack(buf, "ss",
|
||||||
@@ -2664,12 +2653,11 @@ static sftp_statvfs_t sftp_parse_statvfs(sftp_session sftp, ssh_buffer buf) {
|
|||||||
sftp_statvfs_t statvfs;
|
sftp_statvfs_t statvfs;
|
||||||
int rc;
|
int rc;
|
||||||
|
|
||||||
statvfs = malloc(sizeof(struct sftp_statvfs_struct));
|
statvfs = calloc(1, sizeof(struct sftp_statvfs_struct));
|
||||||
if (statvfs == NULL) {
|
if (statvfs == NULL) {
|
||||||
ssh_set_error_oom(sftp->session);
|
ssh_set_error_oom(sftp->session);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
ZERO_STRUCTP(statvfs);
|
|
||||||
|
|
||||||
rc = ssh_buffer_unpack(buf, "qqqqqqqqqqq",
|
rc = ssh_buffer_unpack(buf, "qqqqqqqqqqq",
|
||||||
&statvfs->f_bsize, /* file system block size */
|
&statvfs->f_bsize, /* file system block size */
|
||||||
|
|||||||
Reference in New Issue
Block a user