libssh port: backported sftp_server_free

sftp_server_free function was added in 0.9 version of libssh and is
required to avoid memory leaks when clients are disconnecting.

Issue #4258
This commit is contained in:
Tomasz Gajewski 2021-07-04 21:44:40 +02:00 committed by Norman Feske
parent f327a40bbb
commit 6ef6f16cb3
4 changed files with 56 additions and 2 deletions

View File

@ -57,6 +57,7 @@ sftp_rmdir T
sftp_seek T
sftp_seek64 T
sftp_send_client_message T
sftp_server_free T
sftp_server_init T
sftp_server_new T
sftp_server_version T

View File

@ -1 +1 @@
41048c9e30b72f70e68fad3806ce1a756178e713
32ea34e5e531845ea1b155620b7685c668a9851d

View File

@ -9,5 +9,7 @@ DIR(libssh) := src/lib/libssh
DIRS := include
DIR_CONTENT(include) := src/lib/libssh/include/libssh
PATCHES := src/lib/libssh/event_bind.patch src/lib/libssh/sftp_support.patch
PATCHES := src/lib/libssh/event_bind.patch \
src/lib/libssh/sftp_support.patch \
src/lib/libssh/sftp_server_free.patch
PATCH_OPT := -p1 -d src/lib/libssh

View File

@ -0,0 +1,51 @@
--- a/include/libssh/sftp.h 2021-07-04 21:26:20.445524952 +0200
+++ b/include/libssh/sftp.h 2021-07-04 20:19:16.752749814 +0200
@@ -854,6 +854,13 @@
* @return 0 on success, < 0 on error.
*/
LIBSSH_API int sftp_server_init(sftp_session sftp);
+
+/**
+ * @brief Close and deallocate a sftp server session.
+ *
+ * @param sftp The sftp session handle to free.
+ */
+LIBSSH_API void sftp_server_free(sftp_session sftp);
#endif /* WITH_SERVER */
/* this is not a public interface */
--- a/src/sftp.c 2021-07-04 21:26:20.445524952 +0200
+++ b/src/sftp.c 2021-07-04 20:18:14.310652966 +0200
@@ -299,6 +299,32 @@
return 0;
}
+
+void sftp_server_free(sftp_session sftp)
+{
+ sftp_request_queue ptr;
+
+ if (sftp == NULL) {
+ return;
+ }
+
+ ptr = sftp->queue;
+ while(ptr) {
+ sftp_request_queue old;
+ sftp_message_free(ptr->message);
+ old = ptr->next;
+ SAFE_FREE(ptr);
+ ptr = old;
+ }
+
+ SAFE_FREE(sftp->handles);
+ SSH_BUFFER_FREE(sftp->read_packet->payload);
+ SAFE_FREE(sftp->read_packet);
+
+ sftp_ext_free(sftp->ext);
+
+ SAFE_FREE(sftp);
+}
#endif /* WITH_SERVER */
void sftp_free(sftp_session sftp)