Skip to content

Commit

Permalink
Fix memory leak
Browse files Browse the repository at this point in the history
Pass Go a pointer to the CGOBitmap, otherwise Rust assumes ownership
is passing through the FFI and will not free the memory when we're
finished with it.
  • Loading branch information
zmb3 committed Mar 7, 2022
1 parent 4f22aa7 commit 0b37060
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 7 deletions.
4 changes: 2 additions & 2 deletions lib/srv/desktop/rdp/rdpclient/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -382,11 +382,11 @@ func (c *Client) start() {
}

//export handle_bitmap
func handle_bitmap(handle C.uintptr_t, cb C.CGOBitmap) C.CGOError {
func handle_bitmap(handle C.uintptr_t, cb *C.CGOBitmap) C.CGOError {
return cgo.Handle(handle).Value().(*Client).handleBitmap(cb)
}

func (c *Client) handleBitmap(cb C.CGOBitmap) C.CGOError {
func (c *Client) handleBitmap(cb *C.CGOBitmap) C.CGOError {
// Notify the input forwarding goroutine that we're ready for input.
// Input can only be sent after connection was established, which we infer
// from the fact that a bitmap was sent.
Expand Down
2 changes: 1 addition & 1 deletion lib/srv/desktop/rdp/rdpclient/librdprs.h
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,6 @@ void free_rust_string(char *s);

extern void free_go_string(char *s);

extern CGOError handle_bitmap(uintptr_t client_ref, struct CGOBitmap b);
extern CGOError handle_bitmap(uintptr_t client_ref, struct CGOBitmap *b);

extern CGOError handle_remote_copy(uintptr_t client_ref, uint8_t *data, uint32_t len);
7 changes: 3 additions & 4 deletions lib/srv/desktop/rdp/rdpclient/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,7 @@ fn read_rdp_output_inner(client: &Client) -> Option<String> {
.unwrap()
.read(|rdp_event| match rdp_event {
RdpEvent::Bitmap(bitmap) => {
let cbitmap = match CGOBitmap::try_from(bitmap) {
let mut cbitmap = match CGOBitmap::try_from(bitmap) {
Ok(cb) => cb,
Err(e) => {
error!(
Expand All @@ -469,7 +469,7 @@ fn read_rdp_output_inner(client: &Client) -> Option<String> {
}
};
unsafe {
err = handle_bitmap(client_ref, cbitmap) as CGOError;
err = handle_bitmap(client_ref, &mut cbitmap) as CGOError;
};
}
// These should never really be sent by the server to us.
Expand Down Expand Up @@ -696,8 +696,7 @@ unsafe fn from_cgo_error(e: CGOError) -> String {
// comments.
extern "C" {
fn free_go_string(s: *mut c_char);
fn handle_bitmap(client_ref: usize, b: CGOBitmap) -> CGOError;

fn handle_bitmap(client_ref: usize, b: *mut CGOBitmap) -> CGOError;
fn handle_remote_copy(client_ref: usize, data: *mut u8, len: u32) -> CGOError;
}

Expand Down

0 comments on commit 0b37060

Please sign in to comment.