x11/cliprdr: impove error handling

Unify error handling in xf_clipboard_new() by using a common cleanup
code path. This fixes a leak of clipboard->system and format names when
an error occurs during initialization.

Also reformat the code to fit into 100 column limit without this line
break insanity and with improved readability.

I do not particularly like to use a variable with such a descriptive
name 'n' far away in the error handling part, but its short name is
kinda important for readability so let's keep it as is.
This commit is contained in:
ilammy 2017-04-09 02:29:51 +03:00
parent 458c042b53
commit 96fe94c85f

View File

@ -1393,6 +1393,7 @@ static UINT xf_cliprdr_server_format_data_response(CliprdrClientContext*
xfClipboard* xf_clipboard_new(xfContext* xfc)
{
int i;
int n;
rdpChannels* channels;
xfClipboard* clipboard;
@ -1415,19 +1416,18 @@ xfClipboard* xf_clipboard_new(xfContext* xfc)
if (clipboard->clipboard_atom == None)
{
WLog_ERR(TAG, "unable to get CLIPBOARD atom");
free(clipboard);
return NULL;
goto error;
}
clipboard->property_atom = XInternAtom(xfc->display, "_FREERDP_CLIPRDR", FALSE);
clipboard->raw_transfer_atom = XInternAtom(xfc->display, "_FREERDP_CLIPRDR_RAW",
FALSE);
clipboard->raw_format_list_atom = XInternAtom(xfc->display,
"_FREERDP_CLIPRDR_FORMATS", FALSE);
clipboard->raw_transfer_atom = XInternAtom(xfc->display, "_FREERDP_CLIPRDR_RAW", FALSE);
clipboard->raw_format_list_atom =
XInternAtom(xfc->display, "_FREERDP_CLIPRDR_FORMATS", FALSE);
xf_cliprdr_set_raw_transfer_enabled(clipboard, TRUE);
XSelectInput(xfc->display, clipboard->root_window, PropertyChangeMask);
#ifdef WITH_XFIXES
XSelectInput(xfc->display, clipboard->root_window, PropertyChangeMask);
#ifdef WITH_XFIXES
if (XFixesQueryExtension(xfc->display, &clipboard->xfixes_event_base,
&clipboard->xfixes_error_base))
{
@ -1453,53 +1453,60 @@ xfClipboard* xf_clipboard_new(xfContext* xfc)
WLog_ERR(TAG,
"Warning: Using clipboard redirection without XFIXES extension is strongly discouraged!");
#endif
n = 0;
clipboard->clientFormats[n].atom = XInternAtom(xfc->display, "_FREERDP_RAW",
False);
clipboard->clientFormats[n].atom = XInternAtom(xfc->display, "_FREERDP_RAW", False);
clipboard->clientFormats[n].formatId = CF_RAW;
n++;
clipboard->clientFormats[n].atom = XInternAtom(xfc->display, "UTF8_STRING",
False);
clipboard->clientFormats[n].atom = XInternAtom(xfc->display, "UTF8_STRING", False);
clipboard->clientFormats[n].formatId = CF_UNICODETEXT;
n++;
clipboard->clientFormats[n].atom = XA_STRING;
clipboard->clientFormats[n].formatId = CF_TEXT;
n++;
clipboard->clientFormats[n].atom = XInternAtom(xfc->display, "image/png",
False);
clipboard->clientFormats[n].atom = XInternAtom(xfc->display, "image/png", False);
clipboard->clientFormats[n].formatId = CB_FORMAT_PNG;
n++;
clipboard->clientFormats[n].atom = XInternAtom(xfc->display, "image/jpeg",
False);
clipboard->clientFormats[n].atom = XInternAtom(xfc->display, "image/jpeg", False);
clipboard->clientFormats[n].formatId = CB_FORMAT_JPEG;
n++;
clipboard->clientFormats[n].atom = XInternAtom(xfc->display, "image/gif",
False);
clipboard->clientFormats[n].atom = XInternAtom(xfc->display, "image/gif", False);
clipboard->clientFormats[n].formatId = CB_FORMAT_GIF;
n++;
clipboard->clientFormats[n].atom = XInternAtom(xfc->display, "image/bmp",
False);
clipboard->clientFormats[n].atom = XInternAtom(xfc->display, "image/bmp", False);
clipboard->clientFormats[n].formatId = CF_DIB;
n++;
clipboard->clientFormats[n].atom = XInternAtom(xfc->display, "text/html",
False);
clipboard->clientFormats[n].atom = XInternAtom(xfc->display, "text/html", False);
clipboard->clientFormats[n].formatId = CB_FORMAT_HTML;
clipboard->clientFormats[n].formatName = _strdup("HTML Format");
if (!clipboard->clientFormats[n].formatName)
{
ClipboardDestroy(clipboard->system);
free(clipboard);
return NULL;
}
goto error;
n++;
clipboard->numClientFormats = n;
clipboard->targets[0] = XInternAtom(xfc->display, "TIMESTAMP", FALSE);
clipboard->targets[1] = XInternAtom(xfc->display, "TARGETS", FALSE);
clipboard->numTargets = 2;
clipboard->incr_atom = XInternAtom(xfc->display, "INCR", FALSE);
return clipboard;
error:
for (i = 0; i < n; i++)
free(clipboard->clientFormats[i].formatName);
ClipboardDestroy(clipboard->system);
free(clipboard);
return NULL;
}
void xf_clipboard_free(xfClipboard* clipboard)