mirror of
https://git.libssh.org/projects/libssh.git
synced 2025-07-28 01:41:48 +03:00
examples: Fixed possible memory leak in samplesftp.c
Signed-off-by: Anderson Toshiyuki Sasaki <ansasaki@redhat.com> Reviewed-by: Andreas Schneider <asn@cryptomilk.org>
This commit is contained in:
committed by
Andreas Schneider
parent
cc83b463ce
commit
7390db6bbb
@ -51,12 +51,12 @@ static void do_sftp(ssh_session session){
|
|||||||
if(!sftp){
|
if(!sftp){
|
||||||
fprintf(stderr, "sftp error initialising channel: %s\n",
|
fprintf(stderr, "sftp error initialising channel: %s\n",
|
||||||
ssh_get_error(session));
|
ssh_get_error(session));
|
||||||
return;
|
goto end;
|
||||||
}
|
}
|
||||||
if(sftp_init(sftp)){
|
if(sftp_init(sftp)){
|
||||||
fprintf(stderr, "error initialising sftp: %s\n",
|
fprintf(stderr, "error initialising sftp: %s\n",
|
||||||
ssh_get_error(session));
|
ssh_get_error(session));
|
||||||
return;
|
goto end;
|
||||||
}
|
}
|
||||||
|
|
||||||
printf("Additional SFTP extensions provided by the server:\n");
|
printf("Additional SFTP extensions provided by the server:\n");
|
||||||
@ -71,13 +71,13 @@ static void do_sftp(ssh_session session){
|
|||||||
if (sftp_symlink(sftp, "/tmp/this_is_the_link",
|
if (sftp_symlink(sftp, "/tmp/this_is_the_link",
|
||||||
"/tmp/sftp_symlink_test") < 0) {
|
"/tmp/sftp_symlink_test") < 0) {
|
||||||
fprintf(stderr, "Could not create link (%s)\n", ssh_get_error(session));
|
fprintf(stderr, "Could not create link (%s)\n", ssh_get_error(session));
|
||||||
return;
|
goto end;
|
||||||
}
|
}
|
||||||
|
|
||||||
lnk = sftp_readlink(sftp, "/tmp/sftp_symlink_test");
|
lnk = sftp_readlink(sftp, "/tmp/sftp_symlink_test");
|
||||||
if (lnk == NULL) {
|
if (lnk == NULL) {
|
||||||
fprintf(stderr, "Could not read link (%s)\n", ssh_get_error(session));
|
fprintf(stderr, "Could not read link (%s)\n", ssh_get_error(session));
|
||||||
return;
|
goto end;
|
||||||
}
|
}
|
||||||
printf("readlink /tmp/sftp_symlink_test: %s\n", lnk);
|
printf("readlink /tmp/sftp_symlink_test: %s\n", lnk);
|
||||||
|
|
||||||
@ -87,7 +87,7 @@ static void do_sftp(ssh_session session){
|
|||||||
sftpstatvfs = sftp_statvfs(sftp, "/tmp");
|
sftpstatvfs = sftp_statvfs(sftp, "/tmp");
|
||||||
if (sftpstatvfs == NULL) {
|
if (sftpstatvfs == NULL) {
|
||||||
fprintf(stderr, "statvfs failed (%s)\n", ssh_get_error(session));
|
fprintf(stderr, "statvfs failed (%s)\n", ssh_get_error(session));
|
||||||
return;
|
goto end;
|
||||||
}
|
}
|
||||||
|
|
||||||
printf("sftp statvfs:\n"
|
printf("sftp statvfs:\n"
|
||||||
@ -118,7 +118,7 @@ static void do_sftp(ssh_session session){
|
|||||||
|
|
||||||
if (statvfs("/tmp", &sysstatvfs) < 0) {
|
if (statvfs("/tmp", &sysstatvfs) < 0) {
|
||||||
fprintf(stderr, "statvfs failed (%s)\n", strerror(errno));
|
fprintf(stderr, "statvfs failed (%s)\n", strerror(errno));
|
||||||
return;
|
goto end;
|
||||||
}
|
}
|
||||||
|
|
||||||
printf("sys statvfs:\n"
|
printf("sys statvfs:\n"
|
||||||
@ -151,7 +151,7 @@ static void do_sftp(ssh_session session){
|
|||||||
dir=sftp_opendir(sftp,"./");
|
dir=sftp_opendir(sftp,"./");
|
||||||
if(!dir) {
|
if(!dir) {
|
||||||
fprintf(stderr, "Directory not opened(%s)\n", ssh_get_error(session));
|
fprintf(stderr, "Directory not opened(%s)\n", ssh_get_error(session));
|
||||||
return ;
|
goto end;
|
||||||
}
|
}
|
||||||
/* reading the whole directory, file by file */
|
/* reading the whole directory, file by file */
|
||||||
while((file=sftp_readdir(sftp,dir))){
|
while((file=sftp_readdir(sftp,dir))){
|
||||||
@ -168,11 +168,11 @@ static void do_sftp(ssh_session session){
|
|||||||
/* when file=NULL, an error has occured OR the directory listing is end of file */
|
/* when file=NULL, an error has occured OR the directory listing is end of file */
|
||||||
if(!sftp_dir_eof(dir)){
|
if(!sftp_dir_eof(dir)){
|
||||||
fprintf(stderr, "Error: %s\n", ssh_get_error(session));
|
fprintf(stderr, "Error: %s\n", ssh_get_error(session));
|
||||||
return;
|
goto end;
|
||||||
}
|
}
|
||||||
if(sftp_closedir(dir)){
|
if(sftp_closedir(dir)){
|
||||||
fprintf(stderr, "Error: %s\n", ssh_get_error(session));
|
fprintf(stderr, "Error: %s\n", ssh_get_error(session));
|
||||||
return;
|
goto end;
|
||||||
}
|
}
|
||||||
/* this will open a file and copy it into your /home directory */
|
/* this will open a file and copy it into your /home directory */
|
||||||
/* the small buffer size was intended to stress the library. of course, you can use a buffer till 20kbytes without problem */
|
/* the small buffer size was intended to stress the library. of course, you can use a buffer till 20kbytes without problem */
|
||||||
@ -181,20 +181,23 @@ static void do_sftp(ssh_session session){
|
|||||||
if(!fichier){
|
if(!fichier){
|
||||||
fprintf(stderr, "Error opening /usr/bin/ssh: %s\n",
|
fprintf(stderr, "Error opening /usr/bin/ssh: %s\n",
|
||||||
ssh_get_error(session));
|
ssh_get_error(session));
|
||||||
return;
|
goto end;
|
||||||
}
|
}
|
||||||
/* open a file for writing... */
|
/* open a file for writing... */
|
||||||
to=sftp_open(sftp,"ssh-copy",O_WRONLY | O_CREAT, 0700);
|
to=sftp_open(sftp,"ssh-copy",O_WRONLY | O_CREAT, 0700);
|
||||||
if(!to){
|
if(!to){
|
||||||
fprintf(stderr, "Error opening ssh-copy for writing: %s\n",
|
fprintf(stderr, "Error opening ssh-copy for writing: %s\n",
|
||||||
ssh_get_error(session));
|
ssh_get_error(session));
|
||||||
return;
|
sftp_close(fichier);
|
||||||
|
goto end;
|
||||||
}
|
}
|
||||||
while((len=sftp_read(fichier,data,4096)) > 0){
|
while((len=sftp_read(fichier,data,4096)) > 0){
|
||||||
if(sftp_write(to,data,len)!=len){
|
if(sftp_write(to,data,len)!=len){
|
||||||
fprintf(stderr, "Error writing %d bytes: %s\n",
|
fprintf(stderr, "Error writing %d bytes: %s\n",
|
||||||
len, ssh_get_error(session));
|
len, ssh_get_error(session));
|
||||||
return;
|
sftp_close(to);
|
||||||
|
sftp_close(fichier);
|
||||||
|
goto end;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
printf("finished\n");
|
printf("finished\n");
|
||||||
@ -211,8 +214,9 @@ static void do_sftp(ssh_session session){
|
|||||||
printf("chunk %d : %d (%s)\n",i,len,ssh_get_error(session));
|
printf("chunk %d : %d (%s)\n",i,len,ssh_get_error(session));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
sftp_close(to);
|
|
||||||
|
|
||||||
|
sftp_close(to);
|
||||||
|
end:
|
||||||
/* close the sftp session */
|
/* close the sftp session */
|
||||||
sftp_free(sftp);
|
sftp_free(sftp);
|
||||||
printf("sftp session terminated\n");
|
printf("sftp session terminated\n");
|
||||||
|
Reference in New Issue
Block a user