Paste: an empty or padded raw bitmap reads as the old PNG path read it

A raw bitmap with no width or no height is not taken in place of the
text beside it, so the text is pasted. Bytes past a raw bitmap's pixels
are dropped before the pixels are stored, so the PNG encoder is given
exactly the pixels the size names and the paste goes through.
This commit is contained in:
dotCooCoo committed 2026-12-01 06:28:17 -08:00
1 parent 438ffdcebe
commit 6a4c1e6af9
2 files changed
+45 -3

No files matched your search

+15 -3
View File
@@ -67,8 +67,17 @@ fn deferred_flavour(f: Flavor) -> Option<Flavour> {
fn rgba8_fits(data: &[u8]) -> bool {
let Some(([w0, w1, w2, w3, h0, h1, h2, h3], pixels)) = data.split_first_chunk::<8>() else { return false };
let (width, height) = (u32::from_le_bytes([*w0, *w1, *w2, *w3]), u32::from_le_bytes([*h0, *h1, *h2, *h3]));
let needed = u64::from(width).checked_mul(u64::from(height)).and_then(|n| n.checked_mul(4));
needed.is_some_and(|n| u64::try_from(pixels.len()).is_ok_and(|len| n <= len))
rgba8_len(width, height).is_some_and(|n| n <= pixels.len())
}
/// The byte count of `width` × `height` RGBA8 pixels; `None` for an empty size or one that
/// overflows.
fn rgba8_len(width: u32, height: u32) -> Option<usize> {
if width == 0 || height == 0 {
return None;
}
let n = u64::from(width).checked_mul(u64::from(height))?.checked_mul(4)?;
usize::try_from(n).ok()
}
/// The bitmap of `f`, a flavour that imports as an image ([`imports_as_image`]), read as
@@ -84,12 +93,15 @@ pub(crate) fn read_bitmap(f: Flavour) -> (&'static str, Result<ImageRead, String
}
/// A raw bitmap (width and height as little-endian `u32`s, then straight RGBA8 pixels) as an
/// image; `None` when the pixels don't fill the size.
/// image; `None` when the size is empty or the pixels don't fill it. Bytes past the pixels are
/// dropped.
fn rgba8_image(mut data: Vec<u8>) -> Option<image::RgbaImage> {
let (header, _) = data.split_first_chunk::<8>()?;
let [w0, w1, w2, w3, h0, h1, h2, h3] = *header;
let (width, height) = (u32::from_le_bytes([w0, w1, w2, w3]), u32::from_le_bytes([h0, h1, h2, h3]));
let len = rgba8_len(width, height)?;
data.drain(..8);
data.truncate(len);
image::RgbaImage::from_raw(width, height, data)
}
+30
View File
@@ -1021,6 +1021,36 @@ fn a_cut_short_bitmap_read_off_the_ui_thread_gives_way_to_its_text() {
assert_eq!(read.err().as_deref(), Some("the copied bitmap is cut short"));
}
/// A raw bitmap read off the UI thread with no width or no height is not taken: the text beside
/// it is pasted instead.
#[test]
fn an_empty_bitmap_read_off_the_ui_thread_gives_way_to_its_text() {
let (mut app, board) = app();
for (w, h) in [(0u32, 2u32), (3, 0)] {
let raw = [w.to_le_bytes(), h.to_le_bytes()].concat();
copy_elsewhere(&board, vec![(TEXT, b"https://example.com/images/cat.png".to_vec()), (RGBA8, raw)]);
let make = app.adopt_system_clipboard_or_defer(true).unwrap().expect("the picture is read later");
let f = make().expect("the text");
assert_eq!((f.mime, f.data.as_slice()), (TEXT, &b"https://example.com/images/cat.png"[..]), "{w} by {h}");
}
}
/// A raw bitmap read off the UI thread with bytes past its pixels pastes its pixels.
#[test]
fn a_padded_bitmap_read_off_the_ui_thread_pastes_its_pixels() {
let (mut app, board) = app();
// 3 by 2 pixels need 24 bytes after the header; 28 are there.
let mut raw = [3u32.to_le_bytes(), 2u32.to_le_bytes()].concat();
raw.extend_from_slice(&[255; 28]);
copy_elsewhere(&board, vec![(TEXT, b"https://example.com/images/cat.png".to_vec()), (RGBA8, raw)]);
let make = app.adopt_system_clipboard_or_defer(true).unwrap().expect("the picture is read later");
let (mime, read) = crate::sysclip::read_bitmap(make().expect("the bitmap"));
app.adopt_image_read(mime, Some([250.0, 180.0]), read).unwrap();
app.session.execute("edit.paste", &json!({})).unwrap();
let NodeKind::Image(im) = &pasted(&app)[0].kind else { panic!("not an image: {:?}", pasted(&app)[0].kind) };
assert_eq!((im.width, im.height), (3, 2));
}
#[test]
fn a_pdf_from_another_app_pastes_as_vectors_ahead_of_its_bitmap() {
let (mut app, board) = app();