1
0
mirror of https://git.libssh.org/projects/libssh.git synced 2025-06-13 20:41:30 +03:00

examples: Fix a possible memory leak.

This commit is contained in:
Andreas Schneider
2013-06-19 12:14:25 +02:00
parent b698f6361c
commit abb25861e5

View File

@ -174,7 +174,7 @@ static int do_copy(struct location *src, struct location *dest, int recursive){
char buffer[16384]; char buffer[16384];
int total=0; int total=0;
int mode; int mode;
char *filename; char *filename = NULL;
/* recursive mode doesn't work yet */ /* recursive mode doesn't work yet */
(void)recursive; (void)recursive;
/* Get the file name and size*/ /* Get the file name and size*/
@ -201,6 +201,7 @@ static int do_copy(struct location *src, struct location *dest, int recursive){
} }
if(r==SSH_ERROR){ if(r==SSH_ERROR){
fprintf(stderr,"Error: %s\n",ssh_get_error(src->session)); fprintf(stderr,"Error: %s\n",ssh_get_error(src->session));
ssh_string_free_char(filename);
return -1; return -1;
} }
} while(r != SSH_SCP_REQUEST_NEWFILE); } while(r != SSH_SCP_REQUEST_NEWFILE);
@ -211,6 +212,7 @@ static int do_copy(struct location *src, struct location *dest, int recursive){
// snprintf(buffer,sizeof(buffer),"C0644 %d %s\n",size,src->path); // snprintf(buffer,sizeof(buffer),"C0644 %d %s\n",size,src->path);
if(r==SSH_ERROR){ if(r==SSH_ERROR){
fprintf(stderr,"error: %s\n",ssh_get_error(dest->session)); fprintf(stderr,"error: %s\n",ssh_get_error(dest->session));
ssh_string_free_char(filename);
ssh_scp_free(dest->scp); ssh_scp_free(dest->scp);
return -1; return -1;
} }
@ -221,6 +223,7 @@ static int do_copy(struct location *src, struct location *dest, int recursive){
fprintf(stderr,"Cannot open %s for writing: %s\n",filename,strerror(errno)); fprintf(stderr,"Cannot open %s for writing: %s\n",filename,strerror(errno));
if(src->is_ssh) if(src->is_ssh)
ssh_scp_deny_request(src->scp,"Cannot open local file"); ssh_scp_deny_request(src->scp,"Cannot open local file");
ssh_string_free_char(filename);
return -1; return -1;
} }
} }
@ -233,6 +236,7 @@ static int do_copy(struct location *src, struct location *dest, int recursive){
r=ssh_scp_read(src->scp,buffer,sizeof(buffer)); r=ssh_scp_read(src->scp,buffer,sizeof(buffer));
if(r==SSH_ERROR){ if(r==SSH_ERROR){
fprintf(stderr,"Error reading scp: %s\n",ssh_get_error(src->session)); fprintf(stderr,"Error reading scp: %s\n",ssh_get_error(src->session));
ssh_string_free_char(filename);
return -1; return -1;
} }
if(r==0) if(r==0)
@ -243,6 +247,7 @@ static int do_copy(struct location *src, struct location *dest, int recursive){
break; break;
if(r<0){ if(r<0){
fprintf(stderr,"Error reading file: %s\n",strerror(errno)); fprintf(stderr,"Error reading file: %s\n",strerror(errno));
ssh_string_free_char(filename);
return -1; return -1;
} }
} }
@ -252,18 +257,21 @@ static int do_copy(struct location *src, struct location *dest, int recursive){
fprintf(stderr,"Error writing in scp: %s\n",ssh_get_error(dest->session)); fprintf(stderr,"Error writing in scp: %s\n",ssh_get_error(dest->session));
ssh_scp_free(dest->scp); ssh_scp_free(dest->scp);
dest->scp=NULL; dest->scp=NULL;
ssh_string_free_char(filename);
return -1; return -1;
} }
} else { } else {
w=fwrite(buffer,r,1,dest->file); w=fwrite(buffer,r,1,dest->file);
if(w<=0){ if(w<=0){
fprintf(stderr,"Error writing in local file: %s\n",strerror(errno)); fprintf(stderr,"Error writing in local file: %s\n",strerror(errno));
ssh_string_free_char(filename);
return -1; return -1;
} }
} }
total+=r; total+=r;
} while(total < size); } while(total < size);
ssh_string_free_char(filename);
printf("wrote %d bytes\n",total); printf("wrote %d bytes\n",total);
return 0; return 0;
} }