1
0
mirror of https://git.libssh.org/projects/libssh.git synced 2025-11-30 13:01:23 +03:00

Add memory error checks for sftp functions.

git-svn-id: svn+ssh://svn.berlios.de/svnroot/repos/libssh/trunk@325 7dcaeef0-15fb-0310-b436-a5af3365683c
This commit is contained in:
Andreas Schneider
2009-04-01 20:09:20 +00:00
parent b2e50e8967
commit cd4df00dff

View File

@@ -44,8 +44,15 @@ static void sftp_message_free(SFTP_MESSAGE *msg);
static void sftp_set_error(SFTP_SESSION *sftp, int errnum); static void sftp_set_error(SFTP_SESSION *sftp, int errnum);
SFTP_SESSION *sftp_new(SSH_SESSION *session){ SFTP_SESSION *sftp_new(SSH_SESSION *session){
SFTP_SESSION *sftp=malloc(sizeof(SFTP_SESSION)); SFTP_SESSION *sftp;
enter_function(); enter_function();
sftp = malloc(sizeof(SFTP_SESSION));
if (sftp == NULL) {
return NULL;
}
memset(sftp,0,sizeof(SFTP_SESSION)); memset(sftp,0,sizeof(SFTP_SESSION));
sftp->session=session; sftp->session=session;
sftp->channel=channel_new(session); sftp->channel=channel_new(session);
@@ -71,7 +78,13 @@ SFTP_SESSION *sftp_new(SSH_SESSION *session){
#ifdef WITH_SERVER #ifdef WITH_SERVER
SFTP_SESSION *sftp_server_new(SSH_SESSION *session, CHANNEL *chan){ SFTP_SESSION *sftp_server_new(SSH_SESSION *session, CHANNEL *chan){
SFTP_SESSION *sftp=malloc(sizeof(SFTP_SESSION)); SFTP_SESSION *sftp;
sftp = malloc(sizeof(SFTP_SESSION));
if (sftp == NULL) {
return NULL;
}
memset(sftp,0,sizeof(SFTP_SESSION)); memset(sftp,0,sizeof(SFTP_SESSION));
sftp->session=session; sftp->session=session;
sftp->channel=chan; sftp->channel=chan;
@@ -156,9 +169,15 @@ int sftp_packet_write(SFTP_SESSION *sftp,u8 type, BUFFER *payload){
} }
SFTP_PACKET *sftp_packet_read(SFTP_SESSION *sftp){ SFTP_PACKET *sftp_packet_read(SFTP_SESSION *sftp){
SFTP_PACKET *packet=malloc(sizeof(SFTP_PACKET)); SFTP_PACKET *packet;
u32 size; u32 size;
sftp_enter_function(); sftp_enter_function();
packet = malloc(sizeof(SFTP_PACKET));
if (packet == NULL) {
return NULL;
}
packet->sftp=sftp; packet->sftp=sftp;
packet->payload=buffer_new(); packet->payload=buffer_new();
if(channel_read(sftp->channel,packet->payload,4,0)<=0){ if(channel_read(sftp->channel,packet->payload,4,0)<=0){
@@ -204,9 +223,14 @@ 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; SFTP_MESSAGE *msg;
sftp_enter_function(); sftp_enter_function();
msg=malloc(sizeof(SFTP_MESSAGE)); msg = malloc(sizeof(SFTP_MESSAGE));
if (msg == NULL) {
return NULL;
}
memset(msg,0,sizeof(*msg)); memset(msg,0,sizeof(*msg));
msg->payload=buffer_new(); msg->payload=buffer_new();
msg->sftp=sftp; msg->sftp=sftp;
@@ -325,7 +349,13 @@ int sftp_init(SFTP_SESSION *sftp){
} }
static REQUEST_QUEUE *request_queue_new(SFTP_MESSAGE *msg){ static REQUEST_QUEUE *request_queue_new(SFTP_MESSAGE *msg){
REQUEST_QUEUE *queue=malloc(sizeof(REQUEST_QUEUE)); REQUEST_QUEUE *queue;
queue = malloc(sizeof(REQUEST_QUEUE));
if (queue == NULL) {
return NULL;
}
memset(queue,0,sizeof(REQUEST_QUEUE)); memset(queue,0,sizeof(REQUEST_QUEUE));
queue->message=msg; queue->message=msg;
return queue; return queue;
@@ -397,7 +427,12 @@ static STATUS_MESSAGE *parse_status_msg(SFTP_MESSAGE *msg){
ssh_set_error(msg->sftp->session, SSH_FATAL,"Not a ssh_fxp_status message passed in !"); ssh_set_error(msg->sftp->session, SSH_FATAL,"Not a ssh_fxp_status message passed in !");
return NULL; return NULL;
} }
status=malloc(sizeof(STATUS_MESSAGE));
status = malloc(sizeof(STATUS_MESSAGE));
if (status == NULL) {
return NULL;
}
memset(status,0,sizeof(*status)); memset(status,0,sizeof(*status));
status->id=msg->id; status->id=msg->id;
if( (buffer_get_u32(msg->payload,&status->status)!= 4) if( (buffer_get_u32(msg->payload,&status->status)!= 4)
@@ -434,7 +469,12 @@ static SFTP_FILE *parse_handle_msg(SFTP_MESSAGE *msg){
ssh_set_error(msg->sftp->session,SSH_FATAL,"Not a ssh_fxp_handle message passed in !"); ssh_set_error(msg->sftp->session,SSH_FATAL,"Not a ssh_fxp_handle message passed in !");
return NULL; return NULL;
} }
file=malloc(sizeof(SFTP_FILE));
file = malloc(sizeof(SFTP_FILE));
if (file == NULL) {
return NULL;
}
memset(file,0,sizeof(*file)); memset(file,0,sizeof(*file));
file->sftp=msg->sftp; file->sftp=msg->sftp;
file->handle=buffer_get_ssh_string(msg->payload); file->handle=buffer_get_ssh_string(msg->payload);
@@ -482,8 +522,11 @@ SFTP_DIR *sftp_opendir(SFTP_SESSION *sftp, const char *path){
case SSH_FXP_HANDLE: case SSH_FXP_HANDLE:
file=parse_handle_msg(msg); file=parse_handle_msg(msg);
sftp_message_free(msg); sftp_message_free(msg);
if(file){ if (file) {
dir=malloc(sizeof(SFTP_DIR)); dir = malloc(sizeof(SFTP_DIR));
if (dir == NULL) {
return NULL;
}
memset(dir,0,sizeof(*dir)); memset(dir,0,sizeof(*dir));
dir->sftp=sftp; dir->sftp=sftp;
dir->name=strdup(path); dir->name=strdup(path);
@@ -506,7 +549,7 @@ SFTP_DIR *sftp_opendir(SFTP_SESSION *sftp, const char *path){
static SFTP_ATTRIBUTES *sftp_parse_attr_4(SFTP_SESSION *sftp, BUFFER *buf, static SFTP_ATTRIBUTES *sftp_parse_attr_4(SFTP_SESSION *sftp, BUFFER *buf,
int expectnames) { int expectnames) {
u32 flags=0; u32 flags=0;
SFTP_ATTRIBUTES *attr=malloc(sizeof(SFTP_ATTRIBUTES)); SFTP_ATTRIBUTES *attr;
STRING *owner=NULL; STRING *owner=NULL;
STRING *group=NULL; STRING *group=NULL;
int ok=0; int ok=0;
@@ -514,6 +557,11 @@ static SFTP_ATTRIBUTES *sftp_parse_attr_4(SFTP_SESSION *sftp, BUFFER *buf,
/* unused member variable */ /* unused member variable */
(void) expectnames; (void) expectnames;
attr = malloc(sizeof(SFTP_ATTRIBUTES));
if (attr == NULL) {
return NULL;
}
memset(attr,0,sizeof(*attr)); memset(attr,0,sizeof(*attr));
/* it isn't really a loop, but i use it because it's like a try..catch.. construction in C */ /* it isn't really a loop, but i use it because it's like a try..catch.. construction in C */
do { do {
@@ -632,8 +680,14 @@ static SFTP_ATTRIBUTES *sftp_parse_attr_3(SFTP_SESSION *sftp, BUFFER *buf,
u32 flags=0; u32 flags=0;
STRING *name; STRING *name;
STRING *longname; STRING *longname;
SFTP_ATTRIBUTES *attr=malloc(sizeof(SFTP_ATTRIBUTES)); SFTP_ATTRIBUTES *attr;
int ok=0; int ok=0;
attr = malloc(sizeof(SFTP_ATTRIBUTES));
if (attr == NULL) {
return NULL;
}
memset(attr,0,sizeof(*attr)); memset(attr,0,sizeof(*attr));
/* it isn't really a loop, but i use it because it's like a try..catch.. construction in C */ /* it isn't really a loop, but i use it because it's like a try..catch.. construction in C */
do { do {