<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<!DOCTYPE bugzilla SYSTEM "https://bugs.kde.org/page.cgi?id=bugzilla.dtd">

<bugzilla version="5.0.6"
          urlbase="https://bugs.kde.org/"
          
          maintainer="sysadmin@kde.org"
>

    <bug>
          <bug_id>512076</bug_id>
          
          <creation_ts>2025-11-14 12:48:45 +0000</creation_ts>
          <short_desc>Clipboard.SelectionWrite results in repeated data in clipboard</short_desc>
          <delta_ts>2026-03-03 12:51:03 +0000</delta_ts>
          <reporter_accessible>1</reporter_accessible>
          <cclist_accessible>1</cclist_accessible>
          <classification_id>4</classification_id>
          <classification>Plasma</classification>
          <product>xdg-desktop-portal-kde</product>
          <component>general</component>
          <version>6.5.1</version>
          <rep_platform>Other</rep_platform>
          <op_sys>Linux</op_sys>
          <bug_status>RESOLVED</bug_status>
          <resolution>FIXED</resolution>
          
          <see_also>https://bugs.kde.org/show_bug.cgi?id=511989</see_also>
          <bug_file_loc></bug_file_loc>
          <status_whiteboard></status_whiteboard>
          <keywords></keywords>
          <priority>NOR</priority>
          <bug_severity>normal</bug_severity>
          <target_milestone>---</target_milestone>
          
          
          <everconfirmed>1</everconfirmed>
          <reporter name="Adam">adaha</reporter>
          <assigned_to name="Plasma Bugs List">plasma-bugs-null</assigned_to>
          <cc>aleixpol</cc>
    
    <cc>kde</cc>
    
    <cc>koos.vriezen</cc>
    
    <cc>nate</cc>
          
          <cf_commitlink>https://invent.kde.org/plasma/xdg-desktop-portal-kde/-/commit/ef7fbf3004e5827f4668658a7b19889941adb8dd</cf_commitlink>
          <cf_versionfixedin>6.5.4</cf_versionfixedin>
          <cf_sentryurl></cf_sentryurl>
          <votes>0</votes>

      

      

      

          <comment_sort_order>oldest_to_newest</comment_sort_order>  
          <long_desc isprivate="0" >
    <commentid>2470259</commentid>
    <comment_count>0</comment_count>
    <who name="Adam">adaha</who>
    <bug_when>2025-11-14 12:48:45 +0000</bug_when>
    <thetext>SUMMARY
When calling Clipboard.SelectionWrite, whatever I am writing into the FD will get repeated in the clipboard (seemingly arbitrarily) many times.

STEPS TO REPRODUCE
1. Call Clipboard.SelectionWrite(&quot;ab&quot;)
2. Paste inside remote session

OBSERVED RESULT
The clipboard contains &quot;ab&quot; repeated many times (~3k-100k times).

EXPECTED RESULT
The clipboard should contain &quot;ab&quot;.

SOFTWARE/OS VERSIONS
Linux/KDE Plasma: Fedora 43
KDE Plasma Version: 6.5.1
KDE Frameworks Version:  6.19.0
Qt Version: 6.10.0
xdg-desktop-portal-kde-6.5.2-1 (+ patch https://invent.kde.org/plasma/xdg-desktop-portal-kde/-/commit/b7f2107d0eb35d2b4cfccf9ffaa91437b88eff0a)


ADDITIONAL INFORMATION
I was unsure if this had anything to do with a missing null-terminator. Here is some example code I used to test this:

&gt; int written;
&gt; int remaining;
&gt; const char* data;
&gt; 
&gt; char s[] = { 97, 98, &apos;\0&apos; };
&gt; remaining = 3;
&gt; data = s;
&gt; 
&gt; while (remaining &gt; 0) {
&gt;   written = write(fd, data, remaining);
&gt; 
&gt;   if (written &lt; 0) {
&gt;     vlog.error(&quot;Error writing to fd: %s&quot;, strerror(errno));
&gt;     selectionWriteDone(serial, false);
&gt;     g_object_unref(fdList);
&gt;     close(fd);
&gt;     return;
&gt;   }
&gt; 
&gt;   remaining -= written;
&gt;   data += written;
&gt; }
&gt; 
&gt; if (close(fd) != 0) {
&gt;   vlog.error(&quot;Failed to close fd: %s&quot;, strerror(errno));
&gt;   selectionWriteDone(serial, false);
&gt; } else {
&gt;   selectionWriteDone(serial, true);
&gt; }

If I include the null-terminator (remaining = 3) and try to paste into Konsole, I get a Confirm Paste warning with between ~3k and 100k characters of repeated &quot;ab&quot;, and a warning about pasting hidden control characters U+0000. Pasting with the null character will simply paste &quot;ab&quot; to the terminal.

If I don&apos;t include the null-terminator (remaining = 2), I still get the Confirm Paste warning with ~3-100k characters.

Note that the number of characters printed (~3-100k) will differ between each paste, and not between every time I write to the clipboard.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>2470281</commentid>
    <comment_count>1</comment_count>
    <who name="David Redondo">kde</who>
    <bug_when>2025-11-14 14:15:33 +0000</bug_when>
    <thetext>Can you share a complete reproducer? I am testing with  a python script that writes a string and dont get multiple  characters.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>2470286</commentid>
    <comment_count>2</comment_count>
    <who name="David Redondo">kde</who>
    <bug_when>2025-11-14 14:21:42 +0000</bug_when>
    <thetext>I reproduced this while testing https://bugs.kde.org/show_bug.cgi?id=512075

If both text/plain and text/plain;charset=utf8 are advertised we end up with a weird situation sometimes:
 text/plain;charset=utf8 is advertised twice, one instance is normal the other one contains the data repeated a random amount of times.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>2470289</commentid>
    <comment_count>3</comment_count>
    <who name="David Redondo">kde</who>
    <bug_when>2025-11-14 14:35:03 +0000</bug_when>
    <thetext>Although I can only make it happen very rarely for some reason.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>2470295</commentid>
    <comment_count>4</comment_count>
    <who name="Adam">adaha</who>
    <bug_when>2025-11-14 14:43:24 +0000</bug_when>
    <thetext>I see this issue when trying to implement Clipboard support for TigerVNC&apos;s w0vncserver [1].

[1] https://github.com/TigerVNC/tigervnc/pull/2012</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>2470304</commentid>
    <comment_count>5</comment_count>
    <who name="David Redondo">kde</who>
    <bug_when>2025-11-14 15:06:07 +0000</bug_when>
    <thetext>Currently I am not sure what goes wrong, I supect maybe ksystemclipboard loops somehow due to the duplicated mime type

https://invent.kde.org/frameworks/kguiaddons/-/merge_requests/188 fixes the duplicated mime type,
maybe https://invent.kde.org/frameworks/kguiaddons/-/commit/20b032c87b8d15228d99d162818e51c216054010 is also related. I will try to test with your  program.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>2470306</commentid>
    <comment_count>6</comment_count>
    <who name="Adam">adaha</who>
    <bug_when>2025-11-14 15:08:47 +0000</bug_when>
    <thetext>Thanks for looking into this, really appreciate it.

Let me know what I can do to help, or if you&apos;re having trouble with TigerVNC.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>2471384</commentid>
    <comment_count>7</comment_count>
    <who name="David Redondo">kde</who>
    <bug_when>2025-11-19 08:33:41 +0000</bug_when>
    <thetext>Embarassing bug our side https://invent.kde.org/plasma/xdg-desktop-portal-kde/-/merge_requests/485</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>2471591</commentid>
    <comment_count>8</comment_count>
    <who name="David Redondo">kde</who>
    <bug_when>2025-11-20 07:55:46 +0000</bug_when>
    <thetext>Git commit e42937815cd6e81a062139cfa7fc6a6487c7e125 by David Redondo.
Committed on 20/11/2025 at 07:48.
Pushed by davidre into branch &apos;master&apos;.

clipboard: Dont append data on when read results in  EAGAIN

Otherwise QByteArray::append is called with a negative number which
tries to find a null terminator in buffer.
FIXED-IN:6.5.4

M  +1    -1    src/clipboard.cpp

https://invent.kde.org/plasma/xdg-desktop-portal-kde/-/commit/e42937815cd6e81a062139cfa7fc6a6487c7e125</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>2471605</commentid>
    <comment_count>9</comment_count>
    <who name="David Redondo">kde</who>
    <bug_when>2025-11-20 09:39:19 +0000</bug_when>
    <thetext>Git commit ef7fbf3004e5827f4668658a7b19889941adb8dd by David Redondo.
Committed on 20/11/2025 at 07:56.
Pushed by davidre into branch &apos;Plasma/6.5&apos;.

clipboard: Dont append data on when read results in  EAGAIN

Otherwise QByteArray::append is called with a negative number which
tries to find a null terminator in buffer.
FIXED-IN:6.5.4


(cherry picked from commit e42937815cd6e81a062139cfa7fc6a6487c7e125)

Co-authored-by: David Redondo &lt;kde@david-redondo.de&gt;

M  +1    -1    src/clipboard.cpp

https://invent.kde.org/plasma/xdg-desktop-portal-kde/-/commit/ef7fbf3004e5827f4668658a7b19889941adb8dd</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>2471614</commentid>
    <comment_count>10</comment_count>
    <who name="Adam">adaha</who>
    <bug_when>2025-11-20 10:11:14 +0000</bug_when>
    <thetext>Just tested with your patch, works as expected!

(In reply to David Redondo from comment #7)
&gt; Embarassing bug our side
&gt; https://invent.kde.org/plasma/xdg-desktop-portal-kde/-/merge_requests/485
Can&apos;t say I haven&apos;t done way worse, these things happen :^)</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>2500166</commentid>
    <comment_count>11</comment_count>
    <who name="Nate Graham">nate</who>
    <bug_when>2026-03-03 12:51:03 +0000</bug_when>
    <thetext>*** Bug 517028 has been marked as a duplicate of this bug. ***</thetext>
  </long_desc>
      
      

    </bug>

</bugzilla>