diff --git a/src/protocols/rdp/channels/cliprdr.c b/src/protocols/rdp/channels/cliprdr.c index 7f4ad4248..a93bf8c15 100644 --- a/src/protocols/rdp/channels/cliprdr.c +++ b/src/protocols/rdp/channels/cliprdr.c @@ -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) { @@ -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, @@ -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; @@ -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; } @@ -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; } diff --git a/src/protocols/vnc/clipboard.c b/src/protocols/vnc/clipboard.c index 319ca95f6..9042e9ace 100644 --- a/src/protocols/vnc/clipboard.c +++ b/src/protocols/vnc/clipboard.c @@ -103,7 +103,8 @@ 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; @@ -111,12 +112,14 @@ int guac_vnc_clipboard_end_handler(guac_user* user, guac_stream* stream) { /* 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; } @@ -129,7 +132,8 @@ 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; @@ -137,12 +141,13 @@ void guac_vnc_cut_text(rfbClient* client, const char* text, int textlen) { /* 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); } diff --git a/src/terminal/terminal.c b/src/terminal/terminal.c index 8f8dc5005..d5584d4fd 100644 --- a/src/terminal/terminal.c +++ b/src/terminal/terminal.c @@ -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; @@ -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) {