From d7c9a31b4bb0cbd1b52861a523e1cd7231ee196b Mon Sep 17 00:00:00 2001 From: ilammy Date: Wed, 19 Aug 2015 17:25:17 +0300 Subject: [PATCH] client/X11: correctly trim terminating null bytes from strings Sometimes Windows sends strings with excess null terminating bytes. For example, when one copies digits from calc.exe. At the same time, some local applications freak out when they encounter null bytes (at least LibreOffice is known to be replacing them with '#'). According to the specification of UTF8_STRING format [1], the string data must not contain any trailing null bytes. So they all should be trimmed, not only the last one. Also, if the trailing null byte is not present, the length should not be adjusted. For example, Firefox is actually sending "HTML Format" without a null byte while Internet Explorer adds one. The spec for text/html format [2] says nothing about the teminating null byte, so we are free to remove it, but at least we should not mistakingly delete '>' character of "" tag when it is the last character. [1] http://www.pps.univ-paris-diderot.fr/~jch/software/UTF8_STRING/UTF8_STRING.text [2] https://www.ietf.org/rfc/rfc2854.txt --- client/X11/xf_cliprdr.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/client/X11/xf_cliprdr.c b/client/X11/xf_cliprdr.c index 477c842b5..a68cc3129 100644 --- a/client/X11/xf_cliprdr.c +++ b/client/X11/xf_cliprdr.c @@ -1102,8 +1102,11 @@ static UINT xf_cliprdr_server_format_data_response(CliprdrClientContext* context DstSize = 0; pDstData = (BYTE*) ClipboardGetData(clipboard->system, dstFormatId, &DstSize); - if ((DstSize > 1) && nullTerminated) - DstSize--; + if (nullTerminated) + { + while (DstSize > 0 && pDstData[DstSize - 1] == '\0') + DstSize--; + } } clipboard->data = pDstData;