1- /* $OpenBSD: scp.c,v 1.265 2025/09/02 09:34:48 djm Exp $ */
1+ /* $OpenBSD: scp.c,v 1.266 2025/09/15 05:17:37 djm Exp $ */
22/*
33 * scp - secure remote copy. This is basically patched BSD rcp which
44 * uses ssh to do the data transfer (instead of using rcmd).
@@ -1072,6 +1072,7 @@ toremote(int argc, char **argv, enum scp_mode_e mode, char *sftp_direct)
10721072 if (mode == MODE_SFTP ) {
10731073 if (remin == -1 || conn == NULL ) {
10741074 /* Connect to dest now */
1075+ sftp_free (conn );
10751076 conn = do_sftp_connect (thost , tuser ,
10761077 tport , sftp_direct ,
10771078 & remin , & remout , & do_cmd_pid );
@@ -1089,6 +1090,7 @@ toremote(int argc, char **argv, enum scp_mode_e mode, char *sftp_direct)
10891090 * scp -3 hosta:/foo hosta:/bar hostb:
10901091 */
10911092 /* Connect to origin now */
1093+ sftp_free (conn2 );
10921094 conn2 = do_sftp_connect (host , suser ,
10931095 sport , sftp_direct ,
10941096 & remin2 , & remout2 , & do_cmd_pid2 );
@@ -1178,6 +1180,7 @@ toremote(int argc, char **argv, enum scp_mode_e mode, char *sftp_direct)
11781180 }
11791181 if (remin == -1 ) {
11801182 /* Connect to remote now */
1183+ sftp_free (conn );
11811184 conn = do_sftp_connect (thost , tuser ,
11821185 tport , sftp_direct ,
11831186 & remin , & remout , & do_cmd_pid );
@@ -1206,15 +1209,15 @@ toremote(int argc, char **argv, enum scp_mode_e mode, char *sftp_direct)
12061209 }
12071210 }
12081211out :
1209- if (mode == MODE_SFTP )
1210- free (conn );
12111212 freeargs (& alist );
12121213 free (tuser );
12131214 free (thost );
12141215 free (targ );
12151216 free (suser );
12161217 free (host );
12171218 free (src );
1219+ sftp_free (conn );
1220+ sftp_free (conn2 );
12181221}
12191222
12201223void
@@ -1260,6 +1263,7 @@ tolocal(int argc, char **argv, enum scp_mode_e mode, char *sftp_direct)
12601263 }
12611264 /* Remote to local. */
12621265 if (mode == MODE_SFTP ) {
1266+ sftp_free (conn );
12631267 conn = do_sftp_connect (host , suser , sport ,
12641268 sftp_direct , & remin , & remout , & do_cmd_pid );
12651269 if (conn == NULL ) {
@@ -1271,7 +1275,6 @@ tolocal(int argc, char **argv, enum scp_mode_e mode, char *sftp_direct)
12711275 /* The protocol */
12721276 sink_sftp (1 , argv [argc - 1 ], src , conn );
12731277
1274- free (conn );
12751278 (void ) close (remin );
12761279 (void ) close (remout );
12771280 remin = remout = -1 ;
@@ -1295,6 +1298,7 @@ tolocal(int argc, char **argv, enum scp_mode_e mode, char *sftp_direct)
12951298 free (suser );
12961299 free (host );
12971300 free (src );
1301+ sftp_free (conn );
12981302}
12991303
13001304/* Prepare remote path, handling ~ by assuming cwd is the homedir */
0 commit comments