Strings in the system clipboard contain trailing NULL character

JUCE: v5.4.7
OS: Debian GNU/Linux 10 (buster)
X.Org version: 1.20.4

Using the SytemClipboard class like this:

SystemClipboard::copyTextToClipboard ("test");

and then pasting the clipboard content somewhere else (e.g. a text editor) results in the following string:

test�

The trailing symbol is the NULL unicode character (U+0000). It looks like the null character is not properly stripped off when the string is passed to the X11 clipboard system.

1 Like

Upon further investigation, the NULL character is not present on Ubuntu 18.04, with X.Org version 1.20.8.
I managed to fix the glitch with by applying the following changes:

--- a/modules/juce_gui_basics/native/juce_linux_X11_Clipboard.cpp
+++ b/modules/juce_gui_basics/native/juce_linux_X11_Clipboard.cpp
@@ -154,9 +154,9 @@ namespace ClipboardHelpers
             if (evt.target == XA_STRING || evt.target == ClipboardHelpers::atom_UTF8_STRING)
             {
                 // translate to utf8
-                numDataItems = ClipboardHelpers::localClipboardContent.getNumBytesAsUTF8() + 1;
+                numDataItems = ClipboardHelpers::localClipboardContent.getNumBytesAsUTF8();
                 data.calloc (numDataItems + 1);
-                ClipboardHelpers::localClipboardContent.copyToUTF8 (data, numDataItems);
+                ClipboardHelpers::localClipboardContent.copyToUTF8 (data, numDataItems + 1);
                 propertyFormat = 8; // bits/item
             }
             else if (evt.target == ClipboardHelpers::atom_TARGETS)

Tested both on the original system (Debian 10 with X.Org version: 1.20.4) and Ubuntu.

1 Like

I’m unable to reproduce this on Debian 10 or Ubuntu 18.04 using X11 v1.20.4. Can you check and see if the issue is still present in the latest version of JUCE?

Just tested with JUCE 6.0.1 on Debian 10 (buster), X version: 1.20.4: I still can reproduce the problem.

However I’ve noticed something more: the NULL character doesn’t show up if you copy and paste the text within the same JUCE app - e.g. I tried with the Projucer (both 5.x and 6.0.1) and I could copy/paste text flawlessly.

Where are you pasting to where you are seeing the null character? I’ve tried pasting to other non-JUCE apps like the system Text Editor and it looks correct.

I’m pasting it on a new document in Visual Studio Code, in the Chrome address bar and also in the text area right here on this forum.

Thanks, I see what’s happening now. String::getNumBytesAsUTF8() returns the number of bytes excluding the null-terminator so we’re adding 1 to it. but then also allocating an extra byte for data on the line below.

This has been fixed on the develop branch:

1 Like