Skip to content

Commit

Permalink
GUACAMOLE-2002: Fix clipboard buffer allocation in protocol specific …
Browse files Browse the repository at this point in the history
…functions
  • Loading branch information
eugen-keeper committed Dec 6, 2024
1 parent 1c18f46 commit 223c554
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 12 deletions.
16 changes: 11 additions & 5 deletions src/protocols/rdp/channels/cliprdr.c
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,9 @@ static UINT guac_rdp_cliprdr_format_data_request(CliprdrClientContext* cliprdr,

guac_iconv_write* remote_writer;
const char* input = clipboard->clipboard->buffer;
char* output = guac_mem_alloc(GUAC_COMMON_CLIPBOARD_MAX_LENGTH);

int output_buf_size = clipboard->clipboard->available;
char* output = guac_mem_alloc(output_buf_size);

/* Map requested clipboard format to a guac_iconv writer */
switch (format_data_request->requestedFormatId) {
Expand Down Expand Up @@ -402,7 +404,7 @@ static UINT guac_rdp_cliprdr_format_data_request(CliprdrClientContext* cliprdr,
BYTE* start = (BYTE*) output;
guac_iconv_read* local_reader = settings->normalize_clipboard ? GUAC_READ_UTF8_NORMALIZED : GUAC_READ_UTF8;
guac_iconv(local_reader, &input, clipboard->clipboard->length,
remote_writer, &output, GUAC_COMMON_CLIPBOARD_MAX_LENGTH);
remote_writer, &output, output_buf_size);

CLIPRDR_FORMAT_DATA_RESPONSE data_response = {
.requestedFormatData = (BYTE*) start,
Expand Down Expand Up @@ -470,7 +472,8 @@ static UINT guac_rdp_cliprdr_format_data_response(CliprdrClientContext* cliprdr,
return CHANNEL_RC_OK;
}

char received_data[GUAC_COMMON_CLIPBOARD_MAX_LENGTH];
int output_buf_size = clipboard->clipboard->available;
char* received_data = guac_mem_alloc(output_buf_size);

guac_iconv_read* remote_reader;
const char* input = (char*) format_data_response->requestedFormatData;
Expand Down Expand Up @@ -498,6 +501,7 @@ static UINT guac_rdp_cliprdr_format_data_response(CliprdrClientContext* cliprdr,
default:
guac_client_log(client, GUAC_LOG_DEBUG, "Requested clipboard data "
"in unsupported format (0x%X).", clipboard->requested_format);
guac_mem_free(received_data);
return CHANNEL_RC_OK;

}
Expand All @@ -512,13 +516,15 @@ static UINT guac_rdp_cliprdr_format_data_response(CliprdrClientContext* cliprdr,
/* Convert, store, and forward the clipboard data received from RDP
* server */
if (guac_iconv(remote_reader, &input, data_len,
GUAC_WRITE_UTF8, &output, sizeof(received_data))) {
int length = strnlen(received_data, sizeof(received_data));
GUAC_WRITE_UTF8, &output, output_buf_size)) {
int length = strnlen(received_data, output_buf_size);
guac_common_clipboard_reset(clipboard->clipboard, "text/plain");
guac_common_clipboard_append(clipboard->clipboard, received_data, length);
guac_common_clipboard_send(clipboard->clipboard, client);
}

guac_mem_free(received_data);

return CHANNEL_RC_OK;

}
Expand Down
13 changes: 9 additions & 4 deletions src/protocols/vnc/clipboard.c
Original file line number Diff line number Diff line change
Expand Up @@ -103,20 +103,23 @@ int guac_vnc_clipboard_end_handler(guac_user* user, guac_stream* stream) {
guac_vnc_client* vnc_client = (guac_vnc_client*) user->client->data;
rfbClient* rfb_client = vnc_client->rfb_client;

char output_data[GUAC_COMMON_CLIPBOARD_MAX_LENGTH];
int output_buf_size = vnc_client->clipboard->available;
char* output_data = guac_mem_alloc(output_buf_size);

const char* input = vnc_client->clipboard->buffer;
char* output = output_data;
guac_iconv_write* writer = vnc_client->clipboard_writer;

/* Convert clipboard contents */
guac_iconv(GUAC_READ_UTF8, &input, vnc_client->clipboard->length,
writer, &output, sizeof(output_data));
writer, &output, output_buf_size);

/* Send via VNC only if finished connecting */
if (rfb_client != NULL)
SendClientCutText(rfb_client, output_data, output - output_data);

guac_mem_free(output_data);

return 0;
}

Expand All @@ -129,20 +132,22 @@ void guac_vnc_cut_text(rfbClient* client, const char* text, int textlen) {
if (vnc_client->settings->disable_copy)
return;

char received_data[GUAC_COMMON_CLIPBOARD_MAX_LENGTH];
int output_buf_size = vnc_client->clipboard->available;
char* received_data = guac_mem_alloc(output_buf_size);

const char* input = text;
char* output = received_data;
guac_iconv_read* reader = vnc_client->clipboard_reader;

/* Convert clipboard contents */
guac_iconv(reader, &input, textlen,
GUAC_WRITE_UTF8, &output, sizeof(received_data));
GUAC_WRITE_UTF8, &output, output_buf_size);

/* Send converted data */
guac_common_clipboard_reset(vnc_client->clipboard, "text/plain");
guac_common_clipboard_append(vnc_client->clipboard, received_data, output - received_data);
guac_common_clipboard_send(vnc_client->clipboard, gc);

guac_mem_free(received_data);
}

9 changes: 6 additions & 3 deletions src/terminal/terminal.c
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ guac_terminal_options* guac_terminal_options_create(
options->dpi = dpi;

/* Set default values for all other parameters */
options->clipboard_buffer_size = GUAC_COMMON_CLIPBOARD_MAX_LENGTH;
options->clipboard_buffer_size = GUAC_COMMON_CLIPBOARD_MIN_LENGTH;
options->disable_copy = GUAC_TERMINAL_DEFAULT_DISABLE_COPY;
options->max_scrollback = GUAC_TERMINAL_DEFAULT_MAX_SCROLLBACK;
options->font_name = GUAC_TERMINAL_DEFAULT_FONT_NAME;
Expand Down Expand Up @@ -2207,14 +2207,17 @@ void guac_terminal_clipboard_append(guac_terminal* terminal,
const char* data, int length) {

/* Allocate and clear space for the converted data */
char output_data[GUAC_COMMON_CLIPBOARD_MAX_LENGTH];
int output_buf_size = terminal->clipboard->available;
char* output_data = guac_mem_alloc(output_buf_size);
char* output = output_data;

/* Convert clipboard contents */
guac_iconv(GUAC_READ_UTF8_NORMALIZED, &data, length,
GUAC_WRITE_UTF8, &output, GUAC_COMMON_CLIPBOARD_MAX_LENGTH);
GUAC_WRITE_UTF8, &output, output_buf_size);

guac_common_clipboard_append(terminal->clipboard, output_data, output - output_data);

guac_mem_free(output_data);
}

void guac_terminal_remove_user(guac_terminal* terminal, guac_user* user) {
Expand Down

0 comments on commit 223c554

Please sign in to comment.