Skip to content

Clipboard.GetText(TextDataFormat.Rtf) does not retrieve RTF that lacks a null terminating character #14458

Closed

Description

@rjacobw

.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?

// 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.

https://learn.microsoft.com/en-us/windows/win32/dataxchg/standard-clipboard-formats

As Raymond notes, registered formats have no structural guarantees beyond the size of the buffer - they are not required to be null-terminated:

https://devblogs.microsoft.com/oldnewthing/20251225-00/?p=111914

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

  1. Copy rich text onto the clipboard from an application like PowerPoint
  2. Author a .NET10 app that calls Clipboard.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

  1. added
    untriaged
    on Apr 9, 2026
  2. self-assigned this
    on Apr 10, 2026
  3. SimonZhao888 commented on Apr 13, 2026

    @SimonZhao888
    Member

    @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:

    • According to the Windows clipboard conventions, string-based formats (including RTF) are expected to be null-terminated.
    • Data without a terminator is technically malformed.

    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.

  4. added
    💥 regression-release
    and removed
    💥 regression-release
    on Apr 13, 2026
  5. added
    💥 regression-release
    and removed
    untriaged
    on Apr 15, 2026
  6. rjacobw commented on May 6, 2026

    @rjacobw
    Author

    @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 Net10 update if possible. Thanks!

  7. SimonZhao888 commented on May 7, 2026

    @SimonZhao888
    Member

    @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 Net10 update if possible. Thanks!

    🤝!

    Backport PR: #14527

  8. added this to the 11.0 Preview 5 milestone on May 8, 2026
  9. Zheng-Li01 commented on May 8, 2026

    @Zheng-Li01
    Member

    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
  10. Zheng-Li01 commented on May 8, 2026

    @Zheng-Li01
    Member

    And verified the issue #13929, which have the below results.
    Image

  11. Amy-Li03 commented on Jun 2, 2026

    @Amy-Li03
    Contributor

    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.

  12. locked and limited conversation to collaborators on Jul 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

💥 regression-releaseneeds-area-label

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions