Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scp example free memory on failure #750

Merged
merged 2 commits into from
Nov 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 15 additions & 7 deletions examples/scpclient/scpclient.c
Original file line number Diff line number Diff line change
Expand Up @@ -309,35 +309,43 @@ THREAD_RETURN WOLFSSH_THREAD scp_client(void* args)
}
} while (ret == WS_WANT_READ || ret == WS_WANT_WRITE ||
ret == WS_CHAN_RXD || ret == WS_REKEYING);
if (ret != WS_SUCCESS)
err_sys("Couldn't copy the file.");
if (ret != WS_SUCCESS) {
fprintf(stderr, "Couldn't copy the file.");
((func_args*)args)->return_code = 1;
}

ret = wolfSSH_shutdown(ssh);
/* do not continue on with shutdown process if peer already disconnected */
if (ret != WS_CHANNEL_CLOSED && ret != WS_SOCKET_ERROR_E &&
wolfSSH_get_error(ssh) != WS_SOCKET_ERROR_E &&
wolfSSH_get_error(ssh) != WS_CHANNEL_CLOSED) {
if (ret != WS_SUCCESS) {
err_sys("Sending the shutdown messages failed.");
WLOG(WS_LOG_DEBUG, "Sending the shutdown messages failed.");
}
ret = wolfSSH_worker(ssh, NULL);
if (ret != WS_SUCCESS && ret != WS_CHANNEL_CLOSED) {
err_sys("Failed to listen for close messages from the peer.");
else {
ret = wolfSSH_worker(ssh, NULL);
if (ret != WS_SUCCESS && ret != WS_CHANNEL_CLOSED) {
WLOG(WS_LOG_DEBUG,
"Failed to listen for close messages from the peer.");
}
}
}
WCLOSESOCKET(sockFd);
wolfSSH_free(ssh);
wolfSSH_CTX_free(ctx);
if (ret != WS_SUCCESS && ret != WS_SOCKET_ERROR_E &&
ret != WS_CHANNEL_CLOSED) {
err_sys("Closing scp stream failed. Connection could have been closed by peer");
WLOG(WS_LOG_DEBUG,
"Closing scp stream failed. Connection could have been closed by peer");
}

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

if (ret != WS_SUCCESS)
((func_args*)args)->return_code = 1;
return 0;
}

Expand Down
9 changes: 9 additions & 0 deletions src/wolfscp.c
Original file line number Diff line number Diff line change
Expand Up @@ -610,6 +610,15 @@ int DoScpSource(WOLFSSH* ssh)
continue;
}
if (ret < 0) {
#if !defined(NO_FILESYSTEM) && \
!defined(WOLFSSH_SCP_USER_CALLBACKS)
/* if the socket send had a fatal error, try to close any
* open file descriptor before exit */
ScpSendCtx* sendCtx = NULL;
sendCtx = (ScpSendCtx*)wolfSSH_GetScpSendCtx(ssh);
if (sendCtx != NULL)
WFCLOSE(ssh->fs, sendCtx->fp);
#endif
WLOG(WS_LOG_ERROR, scpError, "failed to send file", ret);
break;
}
Expand Down
Loading