miniquad icon indicating copy to clipboard operation
miniquad copied to clipboard

X11Clipboard is a data race and should not be `Sync`

Open meithecatte opened this issue 10 months ago • 3 comments

claim_clipboard_ownership, as well as respond_to_clipboard_request perform unsynchronized accesses on mutable statics:

https://github.com/not-fl3/miniquad/blob/cccea681683bec392b890e11d0c5f4b7795ffd36/src/native/linux_x11/clipboard.rs#L130-L149

This means that X11Clipboard::set is not threadsafe:

https://github.com/not-fl3/miniquad/blob/cccea681683bec392b890e11d0c5f4b7795ffd36/src/native/linux_x11/clipboard.rs#L229-L242

Nevertheless, X11Clipboard explicitly implements Send and Sync:

https://github.com/not-fl3/miniquad/blob/cccea681683bec392b890e11d0c5f4b7795ffd36/src/native/linux_x11/clipboard.rs#L217-L218

Moreover, removing the Sync impl would not be enough – creating two separate X11Clipboard instances can cause UB even without this impl.

meithecatte avatar Apr 23 '25 03:04 meithecatte

Looks like the same can be said about WaylandClipboard.

meithecatte avatar Apr 23 '25 04:04 meithecatte

In practice this is ok since globally there is only one instance of X11Clipboard guarded behind a Mutex. Although I agree that I don't like the static mut either -- imo the clipboard object should own the string.

bolphen avatar Apr 25 '25 17:04 bolphen

If it's behind a Mutex, it definitely doesn't need the Sync impl – Send should be enough.

meithecatte avatar Apr 25 '25 17:04 meithecatte