From 17e177d06107fd67c3459cc4f94764e2ba36688f Mon Sep 17 00:00:00 2001 From: Jakub Jelen Date: Thu, 14 Nov 2024 15:13:20 +0100 Subject: [PATCH] channels: Use dynamically allocated memory for callbacks Fixes #57 Signed-off-by: Jakub Jelen --- src/pylibsshext/channel.pyx | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/pylibsshext/channel.pyx b/src/pylibsshext/channel.pyx index 64f662e30..06eea659d 100644 --- a/src/pylibsshext/channel.pyx +++ b/src/pylibsshext/channel.pyx @@ -20,6 +20,7 @@ import time from io import BytesIO from cpython.bytes cimport PyBytes_AS_STRING +from cpython.mem cimport PyMem_Malloc from libc.string cimport memset from pylibsshext.errors cimport LibsshChannelException @@ -166,12 +167,15 @@ cdef class Channel: raise LibsshChannelException("Failed to execute command [{0}]: [{1}]".format(command, rc)) result = CompletedProcess(args=command, returncode=-1, stdout=b'', stderr=b'') - cdef callbacks.ssh_channel_callbacks_struct cb - memset(&cb, 0, sizeof(cb)) + cb_size = sizeof(callbacks.ssh_channel_callbacks_struct) + cdef callbacks.ssh_channel_callbacks_struct *cb = PyMem_Malloc(cb_size) + if cb is NULL: + raise LibsshChannelException("Memory allocation error") + memset(cb, 0, cb_size) cb.channel_data_function = &_process_outputs cb.userdata = result - callbacks.ssh_callbacks_init(&cb) - callbacks.ssh_set_channel_callbacks(channel, &cb) + callbacks.ssh_callbacks_init(cb) + callbacks.ssh_set_channel_callbacks(channel, cb) libssh.ssh_channel_send_eof(channel) result.returncode = libssh.ssh_channel_get_exit_status(channel) @@ -179,6 +183,7 @@ cdef class Channel: libssh.ssh_channel_close(channel) libssh.ssh_channel_free(channel) + # XXX leaking the memory of the callbacks return result def send_eof(self):