We are currently migrating Bugzilla to GitHub issues.
Any changes made to the bug tracker now will be lost, so please do not post new bugs or make changes to them.
When we're done, all bug URLs will redirect to their equivalent location on the new bug tracker.

Bug 5319

Summary: On Windows, GetClipboardText returns empty string if ran shortly after SetClipboardText
Product: SDL Reporter: czipperz
Component: videoAssignee: Sam Lantinga <slouken>
Status: NEW --- QA Contact: Sam Lantinga <slouken>
Severity: normal    
Priority: P2    
Version: 2.0.12   
Hardware: x86_64   
OS: Windows 10   

Description czipperz 2020-10-13 02:42:47 UTC
If you call SDL_SetClipboardText() and then shortly thereafter SDL_GetClipboardText() on Windows (x64), SDL_GetClipboardText returns "".  I downloaded the SDL source and located this bug to the lines at SDL_windowsclipboard.c:128-130 which read:

    if (!text) {
        text = SDL_strdup("");
    }

Basically if there is a failure in the interaction with Windows to get the clipboard text, it returns "" instead of returning null.  The problem though is that SDL_GetClipboardText(), at least according to the documentation at https://wiki.libsdl.org/SDL_GetClipboardText is supposed to return "the clipboard text on success or NULL on failure".  In this case, instead of returning NULL on failure, we are returning empty string, indicated success but empty clipboard context.

This bug is fixed by removing these three lines.

It seems like this bug is due to a race condition triggered by the way my application is using this api as I am not able to reproduce it in a test file (ie by calling SDL_SetClipboardText and then SDL_GetClipboardText).

To reproduce this, download my source code (https://github.com/czipperz/mag) and checkout commit 1ff67c0450cda50c836df631d16ca8644100eab1 .  Then follow the build instructions in the readme (use debug mode).  Set a breakpoint on clients/sdl.cpp line 709.  Run mag.exe.  The key bindings are similar to emacs.  Type "abc123" then Control-space (start selecting text), Control-a (go to start of line), Alt-w (copy selected text).  This should reproduce the bug.

You can in addition build SDL from source (this is pretty trivial) and copy SDL2.dll and SDL2.lib over to mag/SDL/lib/x64 to debug inside SDL.  I recommend adding the following at mag/clients/sdl.cpp line 704 so you can debug inside the SDL_GetClipboardText call:

    if (clipboard->stall) {
        int i = 0;
    }

The call to SDL_SetClipboardText is on line 734 of sdl.cpp.

Thanks!
Comment 1 czipperz 2020-10-13 03:37:28 UTC
It looks like this happens specifically because OpenClipboard(GetWindowHandle(_this)) fails on line 114 of SDL_windowsclipboard.c.

Also note that when debugging, if you put the breakpoint before SDL_GetClipboardText the bug will not trigger.  However, this does allow you to step into and see the normal path taken.  Then you can set breakpoints at places the implementation functions fail.
Comment 2 czipperz 2020-12-05 01:36:40 UTC
It is now alternating between an empty string ("\0") and the value of the clipboard.  I haven't updated SDL so I'm not sure what changed.