Repository navigation
Description
.NET version
10.0.5
Did it work in .NET Framework?
Yes
Did it work in any of the earlier releases of .NET Core or .NET 5+?
I believe this worked in net6
Issue description
It doesn't seem like the assumption that RTF placed on the clipboard be null-terminated is correct?
Line 201 in 9bff4e6
| // Can't find the explicit docs for CF_RTF, but we've always treated it as null terminated. |
RTF is transferred using the registered "Rich Text Format" clipboard format, which is treated as an opaque HGLOBAL buffer. The clipboard contract is size-based, not terminator-based, and only specific standard text formats (e.g., CF_TEXT and CF_UNICODETEXT) guarantee null termination.
As Raymond notes, registered formats have no structural guarantees beyond the size of the buffer - they are not required to be null-terminated:
In practice, the framework's assumption appears to be invalid: RTF placed on the clipboard by applications like PowerPoint does not include a trailing null and must be read using the buffer size using GlobalSize, not by scanning for a \0.
Steps to reproduce
- Copy rich text onto the clipboard from an application like PowerPoint
- Author a .
NET10app that callsClipboard.GetText(TextDataFormat.Rtf)
-> Note that an empty string is returned
If, instead, you do the following, you'll notice that you get completely valid RTF:
var clipboardFormatRtf = user32.RegisterClipboardFormat("Rich Text Format");
if (clipboardFormatRtf == 0 || !user32.OpenClipboard(IntPtr.Zero))
{
return string.Empty;
}
try
{
if (user32.IsClipboardFormatAvailable(clipboardFormatRtf))
{
var hData = user32.GetClipboardData(clipboardFormatRtf);
if (hData == IntPtr.Zero)
{
return string.Empty;
}
var pData = kernel32.GlobalLock(hData);
if (pData == IntPtr.Zero)
{
return string.Empty;
}
try
{
var size = kernel32.GlobalSize(hData);
if (size <= 0 || size > int.MaxValue)
{
return string.Empty;
}
unsafe
{
var span = new ReadOnlySpan<byte>((byte*)pData, (int)size);
var nullIndex = span.IndexOf((byte)0);
if (nullIndex >= 0)
{
span = span[..nullIndex];
}
return new string((sbyte*)pData, 0, span.Length);
}
}
finally
{
kernel32.GlobalUnlock(hData);
}
}
}
finally
{
user32.CloseClipboard();
}
Activity
SimonZhao888 commentedon Apr 13, 2026
@rjacobw thanks for reporting this.
This behavior is actually a side effect of the security fix introduced in #13949.
Previously, WinForms would attempt to read clipboard string data even if it was not properly null-terminated. This could lead to reading beyond the buffer, causing undefined behavior or even crashes.
After the fix, the implementation now explicitly validates the presence of a null terminator before constructing a managed string. If no terminator is found, the data is treated as invalid and an empty string is returned.
From a correctness and safety perspective, this is expected behavior:
However, this change does expose compatibility issues with applications that place non-null-terminated RTF data onto the clipboard. In previous versions, such data might have been read successfully by chance.
So this is not strictly a bug, but rather a behavior change due to stricter validation and improved safety.
rjacobw commentedon May 6, 2026
@SimonZhao888 - it looks like it was decided that this was in fact worth fixing? I noticed that your once-closed change has now been merged, and I'd like to request that this get be pulled into a
Net10update if possible. Thanks!SimonZhao888 commentedon May 7, 2026
🤝!
Backport PR: #14527
Zheng-Li01 commentedon May 8, 2026
Verified the issue with latest .NET SDK 11.0.100.Preview.5.26256.117 build, the issue has been fixed that Clipboard.GetText(TextDataFormat.Rtf) retrieved RTF well that lacks a null terminating character as below screenshot.
14458.mp4
Zheng-Li01 commentedon May 8, 2026
And verified the issue #13929, which have the below results.

Amy-Li03 commentedon Jun 2, 2026
Verified this issue in .NET 11 Preview 5 test pass build: .NET SDK 11.0.100-preview.5.26279.113, it was fixed with the same testing result as above.