Paste: a bitmap copied here is found after a late text read on Linux

On Linux and the BSDs, Copy reads the clipboard's text before it writes.
When the app that held the clipboard does not answer that read within its
wait, the read stays open, and the next Paste waits for its answer. Once it
came, Paste read the clipboard again as text only and compared that with
the Copy. A Copy whose SVG markup does not go out as text (art with more
than 1 MiB of placed images) puts only a bitmap on the clipboard, so the
text never matched, and Paste took that bitmap: one picture at 72 ppi in
place of the copied objects, in the same document or another one.

Paste now reads the clipboard as the Copy wrote it (text or bitmap) before
it compares, whatever the late read was, and takes the internal clipboard
when it matches.
This commit is contained in:
dotCooCoo
2026-12-05 06:53:15 -08:00
parent e967ac86a8
commit 2a0ba09fd3
+36 -3
View File
@@ -1204,7 +1204,8 @@ mod portable {
/// paste's bitmap read waits. Once it comes,
/// the same read is made again, since the clipboard may have changed hands meanwhile.
/// `None`, so the paste takes the internal clipboard, when that answer doesn't come or the
/// new one is what `ours` put on the clipboard. Otherwise the clipboard is read as
/// clipboard, read as `ours` was written (its text or its bitmap), holds what `ours` put
/// there. Otherwise the clipboard is read as
/// [`Clipboard::read_now_or_later`] and the paste read it, the new answer standing in for
/// the read it answers: copied files, else
/// text, else the bitmap, and the bitmap when it is asked for and the text is blank or only
@@ -1221,7 +1222,15 @@ mod portable {
};
// The late answer may come from an app that lost the clipboard while it was awaited.
let answer = fresh(self.request);
let held = match (&answer, ours) {
// The Copy made here is read as it was written, whatever the late read was: a Copy's
// text read that answers late leaves a bitmap Copy with no text to compare.
let written = match ours {
Some(Ours::Text(_)) => Some(Request::Text),
Some(Ours::Image(..)) => Some(Request::Image),
None => None,
};
let mut check = written.filter(|w| *w != answer.request()).map(fresh);
let held = match (check.as_ref().unwrap_or(&answer), ours) {
(Answer::Text(Ok(now)), Some(Ours::Text(t))) => now == t,
(Answer::Image(Ok(now)), Some(Ours::Image(w, h))) => (now.width, now.height) == (*w, *h),
_ => false,
@@ -1230,7 +1239,7 @@ mod portable {
return None;
}
let mut answer = Some(answer);
let mut read = |request: Request| match answer.take_if(|a| a.request() == request) {
let mut read = |request: Request| match answer.take_if(|a| a.request() == request).or_else(|| check.take_if(|a| a.request() == request)) {
Some(answer) => answer,
None => fresh(request),
};
@@ -2080,6 +2089,30 @@ mod portable {
assert!(paste_after_check(2).is_none());
}
#[cfg(any(target_os = "linux", target_os = "freebsd", target_os = "dragonfly", target_os = "netbsd", target_os = "openbsd"))]
#[test]
fn a_paste_after_an_unanswered_text_read_finds_a_bitmap_copied_here() {
// A Copy reads the clipboard's text first; when that read gets no answer in its wait, the
// Copy's art goes out as a `width`×1 bitmap with no text (a placed photo too large for
// the SVG markup to go out as text). The paste then waits for the text read's answer.
let paste_after_copy = |width: usize| {
let (mut cb, release) = slow_owner(move |request| match request {
Request::Image => Answer::Image(Ok(arboard::ImageData { width, height: 1, bytes: Cow::Owned(vec![0; width * 4]) })),
_ => Answer::failed(request, "none".into()),
});
assert!(cb.read_waiting(Request::Text, Duration::ZERO).text().is_err());
let unanswered = cb.unanswered(&vectorcraft_engine::cmd::clipboard::PASTE_ORDER).expect("the text read is kept for the paste");
let paste = std::thread::spawn(move || unanswered.paste(Some(&Ours::Image(2, 1)), &vectorcraft_engine::cmd::clipboard::PASTE_ORDER));
drop(release);
paste.join().ok().flatten()
};
// The bitmap copied here: the paste takes the internal clipboard, not a picture of it.
assert!(paste_after_copy(2).is_none());
// Another app's bitmap is pasted.
let pasted = paste_after_copy(3).expect("the other app's bitmap is pasted");
assert_eq!((pasted.mime, decode_png(&pasted.data).map(|i| i.dimensions())), (PNG, Some((3, 1))));
}
#[cfg(any(target_os = "linux", target_os = "freebsd", target_os = "dragonfly", target_os = "netbsd", target_os = "openbsd"))]
#[test]
fn a_late_answer_to_an_ownership_check_is_pasted_only_while_it_is_awaited() {