Skip to content

Commit 563e6c4

Browse files
authored
Merge pull request #750 from JacobBarthelmeh/scp_example
Scp example free memory on failure
2 parents 9bbef7d + d288a0d commit 563e6c4

2 files changed

Lines changed: 24 additions & 7 deletions

File tree

examples/scpclient/scpclient.c

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -309,35 +309,43 @@ THREAD_RETURN WOLFSSH_THREAD scp_client(void* args)
309309
}
310310
} while (ret == WS_WANT_READ || ret == WS_WANT_WRITE ||
311311
ret == WS_CHAN_RXD || ret == WS_REKEYING);
312-
if (ret != WS_SUCCESS)
313-
err_sys("Couldn't copy the file.");
312+
if (ret != WS_SUCCESS) {
313+
fprintf(stderr, "Couldn't copy the file.");
314+
((func_args*)args)->return_code = 1;
315+
}
314316

315317
ret = wolfSSH_shutdown(ssh);
316318
/* do not continue on with shutdown process if peer already disconnected */
317319
if (ret != WS_CHANNEL_CLOSED && ret != WS_SOCKET_ERROR_E &&
318320
wolfSSH_get_error(ssh) != WS_SOCKET_ERROR_E &&
319321
wolfSSH_get_error(ssh) != WS_CHANNEL_CLOSED) {
320322
if (ret != WS_SUCCESS) {
321-
err_sys("Sending the shutdown messages failed.");
323+
WLOG(WS_LOG_DEBUG, "Sending the shutdown messages failed.");
322324
}
323-
ret = wolfSSH_worker(ssh, NULL);
324-
if (ret != WS_SUCCESS && ret != WS_CHANNEL_CLOSED) {
325-
err_sys("Failed to listen for close messages from the peer.");
325+
else {
326+
ret = wolfSSH_worker(ssh, NULL);
327+
if (ret != WS_SUCCESS && ret != WS_CHANNEL_CLOSED) {
328+
WLOG(WS_LOG_DEBUG,
329+
"Failed to listen for close messages from the peer.");
330+
}
326331
}
327332
}
328333
WCLOSESOCKET(sockFd);
329334
wolfSSH_free(ssh);
330335
wolfSSH_CTX_free(ctx);
331336
if (ret != WS_SUCCESS && ret != WS_SOCKET_ERROR_E &&
332337
ret != WS_CHANNEL_CLOSED) {
333-
err_sys("Closing scp stream failed. Connection could have been closed by peer");
338+
WLOG(WS_LOG_DEBUG,
339+
"Closing scp stream failed. Connection could have been closed by peer");
334340
}
335341

336342
ClientFreeBuffers(pubKeyName, privKeyName, NULL);
337343
#if !defined(WOLFSSH_NO_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS)
338344
wc_ecc_fp_free(); /* free per thread cache */
339345
#endif
340346

347+
if (ret != WS_SUCCESS)
348+
((func_args*)args)->return_code = 1;
341349
return 0;
342350
}
343351

src/wolfscp.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -610,6 +610,15 @@ int DoScpSource(WOLFSSH* ssh)
610610
continue;
611611
}
612612
if (ret < 0) {
613+
#if !defined(NO_FILESYSTEM) && \
614+
!defined(WOLFSSH_SCP_USER_CALLBACKS)
615+
/* if the socket send had a fatal error, try to close any
616+
* open file descriptor before exit */
617+
ScpSendCtx* sendCtx = NULL;
618+
sendCtx = (ScpSendCtx*)wolfSSH_GetScpSendCtx(ssh);
619+
if (sendCtx != NULL)
620+
WFCLOSE(ssh->fs, sendCtx->fp);
621+
#endif
613622
WLOG(WS_LOG_ERROR, scpError, "failed to send file", ret);
614623
break;
615624
}

0 commit comments

Comments
 (0)