Skip to content

Commit

Permalink
channels: Use dynamically allocated memory for callbacks
Browse files Browse the repository at this point in the history
Fixes ansible#57

Signed-off-by: Jakub Jelen <[email protected]>
  • Loading branch information
Jakuje committed Nov 14, 2024
1 parent f6df844 commit 10be284
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions src/pylibsshext/channel.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import time
from io import BytesIO

from cpython.bytes cimport PyBytes_AS_STRING
from cpython.mem cimport PyMem_Free, PyMem_Malloc
from libc.string cimport memset

from pylibsshext.errors cimport LibsshChannelException
Expand Down Expand Up @@ -166,19 +167,23 @@ 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 = <callbacks.ssh_channel_callbacks_struct *>PyMem_Malloc(cb_size)
if cb is NULL:
raise LibsshChannelException("Memory allocation error")
memset(cb, 0, cb_size)
cb.channel_data_function = <callbacks.ssh_channel_data_callback>&_process_outputs
cb.userdata = <void *>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)
if channel is not NULL:
libssh.ssh_channel_close(channel)
libssh.ssh_channel_free(channel)

""" XXX leaking the memory of the callbacks """
return result

def send_eof(self):
Expand Down

0 comments on commit 10be284

Please sign in to comment.