From f66aa6508350ca676414aba0defbbc9d634bd6a5 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Sun, 15 Nov 2026 12:06:56 -0800 Subject: [PATCH] Clipboard: Windows text is bounded before it is converted, and each bitmap format has its own limit The size of a CF_UNICODETEXT block is checked against the most UTF-8 it could come to (3 bytes a code unit), so the converted text is never larger than the limit. CF_DIBV5 and CF_DIB are each copied when they fit the limit, so a large first format that doesn't decode still leaves the second to paste. --- apps/vectorcraft/src/clipboard.rs | 80 +++++++++++++++---------------- docs/target-app-parity.md | 2 +- 2 files changed, 40 insertions(+), 42 deletions(-) diff --git a/apps/vectorcraft/src/clipboard.rs b/apps/vectorcraft/src/clipboard.rs index 4ed77b2e05..cf42079691 100644 --- a/apps/vectorcraft/src/clipboard.rs +++ b/apps/vectorcraft/src/clipboard.rs @@ -210,23 +210,11 @@ fn copy_within(size: Option, limit: usize, copy: impl FnOnce() -> Option< copy().filter(|data| data.len() <= limit) } -/// The copies of `ids`' data, in order, [`copy_within`] what is left of `limit` bytes once the -/// copies before them are taken: an id whose data doesn't fit is skipped and the next one is tried. +/// The most bytes of UTF-8 that `utf16` bytes of UTF-16 text come to: a code unit (2 bytes) is at +/// most 3 bytes of UTF-8, and a surrogate pair (4 bytes) is 4. #[cfg_attr(not(windows), allow(dead_code))] -fn copies_within( - ids: impl IntoIterator, - limit: usize, - mut size: impl FnMut(T) -> Option, - mut copy: impl FnMut(T) -> Option>, -) -> Vec> { - let mut left = limit; - ids.into_iter() - .filter_map(|id| { - let data = copy_within(size(id), left, || copy(id))?; - left = left.saturating_sub(data.len()); - Some(data) - }) - .collect() +fn utf8_most(utf16: usize) -> usize { + utf16.div_ceil(2).saturating_mul(3) } /// Whether a Windows file list (`CF_HDROP`: a `DROPFILES` header, then file names each ended by a @@ -346,8 +334,8 @@ mod win { use clipboard_win::{formats, options::NoClear, raw}; use super::{ - BITMAP, ClipboardRead, Cursor, Flavour, MAX_BYTES, MAX_URIS, PNG, SVG, SystemClipboard, TEXT, copied_file, copies_within, copy_within, - decode_png, dib_image, drop_files_within, png_flavour, windows_formats, + BITMAP, ClipboardRead, Cursor, Flavour, MAX_BYTES, MAX_URIS, PNG, SVG, SystemClipboard, TEXT, copied_file, copy_within, decode_png, + dib_image, drop_files_within, png_flavour, utf8_most, windows_formats, }; /// The device-independent bitmaps, the one that carries alpha first. Each is there whenever the @@ -484,16 +472,16 @@ mod win { } /// Copy both bitmap formats while the clipboard is open, then decode them in preference order. - /// `size` gives each format's size before `get` copies it, and the two copies come to at most - /// [`MAX_BYTES`] together ([`copies_within`]). - pub(super) fn read_dibs(size: impl FnMut(u32) -> Option, get: impl FnMut(u32) -> Option>) -> Option { - let dibs = copies_within(DIBS, MAX_BYTES, size, get); + /// `size` gives each format's size before `get` copies it, and a format of more than + /// [`MAX_BYTES`] is not copied ([`copy_within`]) and the next one is tried. + pub(super) fn read_dibs(mut size: impl FnMut(u32) -> Option, mut get: impl FnMut(u32) -> Option>) -> Option { + let dibs: Vec<_> = DIBS.into_iter().filter_map(|id| copy_within(size(id), MAX_BYTES, || get(id))).collect(); (!dibs.is_empty()).then_some(ClipboardRead::Later(Box::new(move || dibs.iter().find_map(|dib| dib_png(dib))))) } /// One flavour, while the clipboard is open. A device-independent bitmap is copied here, and /// the work that comes back decodes each captured format in preference order. Text whose - /// UTF-16 block is larger than [`MAX_BYTES`], or comes to more as UTF-8, is not read. + /// UTF-16 block could come to more than [`MAX_BYTES`] as UTF-8 ([`utf8_most`]) is not read. fn read_one(mime: &'static str) -> Option { let (mime, mut data) = match mime { BITMAP => match read_registered(PNG) { @@ -507,7 +495,7 @@ mod win { } }, TEXT => { - let text = copy_within(size_of(formats::CF_UNICODETEXT), MAX_BYTES, || { + let text = copy_within(size_of(formats::CF_UNICODETEXT).map(utf8_most), MAX_BYTES, || { let mut text = vec![]; raw::get_string(&mut text).ok()?; Some(text) @@ -3424,24 +3412,21 @@ mod tests { assert_eq!(MAX_BYTES, 64 << 20); } - /// Formats copied together come to the limit at most: one that doesn't fit what is left is - /// skipped without a copy and the next one is tried. + /// The UTF-8 that UTF-16 text comes to is bounded from the UTF-16 block's size alone, before + /// the text is converted: a block that could pass the limit as UTF-8 is not read. #[test] - fn formats_copied_together_share_the_limit() { - let sizes = [5, 4, 2, 0]; - let mut asked = vec![]; - let copies = copies_within( - 0..sizes.len(), - 8, - |i| sizes.get(i).copied(), - |i| { - asked.push(i); - Some(vec![0; sizes[i]]) - }, - ); - assert_eq!(copies.iter().map(Vec::len).collect::>(), [5, 2]); - assert_eq!(asked, [0, 2]); - assert!(copies_within([0], 8, |_| None, |_| Some(vec![0])).is_empty()); + fn text_is_bounded_by_what_its_utf8_could_come_to() { + for text in ["", "a", "\u{20ac}\u{20ac}\u{20ac}", "\u{1f600}", "a\u{20ac}\u{1f600}\u{ffff}"] { + let utf16 = text.encode_utf16().count() * 2; + assert!(utf8_most(utf16) >= text.len(), "{text:?}"); + } + // Every code unit at 3 bytes of UTF-8: the bound is exact. + assert_eq!(utf8_most("\u{20ac}\u{20ac}".encode_utf16().count() * 2), "\u{20ac}\u{20ac}".len()); + let copied = |utf16: usize| copy_within(Some(utf8_most(utf16)), MAX_BYTES, || Some(vec![])).is_some(); + assert!(copied(MAX_BYTES / 3 * 2)); + assert!(!copied(MAX_BYTES / 3 * 2 + 2)); + assert!(!copied(MAX_BYTES)); + assert_eq!(utf8_most(usize::MAX), usize::MAX); } /// A Windows file list (CF_HDROP) of `names`: a DROPFILES header, the names in UTF-16 or one @@ -3567,6 +3552,19 @@ mod tests { .unwrap(); assert_eq!(captured, [CF_DIB]); assert_eq!(read.flavour().map(|f| f.mime), Some(PNG)); + // Each format has the limit to itself: a first one at the limit that doesn't decode + // leaves the second to paste. + captured.clear(); + let read = win::read_dibs( + |id| if id == CF_DIBV5 { Some(MAX_BYTES) } else { get(id).map(|d| d.len()) }, + |id| { + captured.push(id); + get(id) + }, + ) + .unwrap(); + assert_eq!(captured, [CF_DIBV5, CF_DIB]); + assert_eq!(read.flavour().map(|f| f.mime), Some(PNG)); } #[test] diff --git a/docs/target-app-parity.md b/docs/target-app-parity.md index e306204afd..26fcc3fbbd 100644 --- a/docs/target-app-parity.md +++ b/docs/target-app-parity.md @@ -1982,7 +1982,7 @@ Moved from the ROADMAP's "Shipped so far" on 2026-10-10. Update it when a featur - Import and paste, 2026-10-13: a multi-artboard `.ai` with notes beside its artboards opens from its editing data, a page's art past its artboard no longer lying over the page before it (#1528); a `.ai` gradient whose stops are listed upwards keeps each midpoint on the stop that starts its part of the ramp (#1530); SVG text on a path follows basic shapes (circle, ellipse, rect, line, polyline, polygon) as their SVG 2 equivalent paths (#1531, fixes #1527); PDFs whose alpha soft masks are drawn as images import with the mask instead of empty art (#1551); EPS and PostScript type set in Type 42 (TrueType) fonts imports at its size instead of 1000 times too large (#1553); PDF and EPS import keep a clipping path whose bounding box covers the page but whose shape doesn't (#1544, fixes #1542). Opening or placing a file with an image too large to decode says so, and the Links panel shows it on the row (#1540, part of #1479). Paste takes a PDF or EPS another app copied as editable vector art: from the macOS pasteboard (with SVG), EPS on Windows beside PDF, and `.eps` files copied in a file manager, with the new command `clipboard.importEps` (#1538, fixes #1499). - A `.ai` or `.ait` file copied in a file manager pastes as its art: one that holds a PDF pastes the objects of its editing data, and one saved as PostScript pastes as a copied `.eps` file does. - Paste checks the size of a file copied in a file manager before reading it: a file of more than 64 MiB, VectorCraft's own limit, is skipped and the next copied file that is art pastes. A file that grows past the limit while it is read is skipped too, after at most one byte more than the limit is read. - - Paste checks the size of the data another app copied before copying it, and takes at most 64 MiB of a format, VectorCraft's own limit: on Windows each registered format, the two bitmap formats (CF_DIBV5 and CF_DIB, together), text (both its UTF-16 data and the UTF-8 text made from it) and the file list (CF_HDROP), and on macOS each pasteboard type. A larger format is skipped and the next one pastes. A list of more than 1,024 copied files pastes no file and no file in it is opened, as on the Linux clipboard; on Windows the names are counted before they are read (`clipboard::tests::clipboard_data_is_copied_only_when_its_size_fits`, `formats_copied_together_share_the_limit`, `a_copied_file_list_longer_than_the_limit_pastes_no_file`, `a_windows_file_list_is_counted_before_its_names_are_read`; not yet run on Windows or macOS). + - Paste checks the size of the data another app copied before copying it, and takes at most 64 MiB of a format, VectorCraft's own limit: on Windows each registered format, each of the two bitmap formats (CF_DIBV5 and CF_DIB), text (the most UTF-8 its UTF-16 data could come to, checked before it is converted) and the file list (CF_HDROP), and on macOS each pasteboard type. A larger format is skipped and the next one pastes. A list of more than 1,024 copied files pastes no file and no file in it is opened, as on the Linux clipboard; on Windows the names are counted before they are read (`clipboard::tests::clipboard_data_is_copied_only_when_its_size_fits`, `text_is_bounded_by_what_its_utf8_could_come_to`, `a_copied_file_list_longer_than_the_limit_pastes_no_file`, `a_windows_file_list_is_counted_before_its_names_are_read`; not yet run on Windows or macOS). - A copied `.ai` or `.ait` file that holds a PDF pastes the objects of its editing data as File › Open reads them: every layer's objects go into the active layer in their stacking order, with their names, hidden objects and type objects, instead of the PDF page's art with its type as outlines. When the editing data can't be read, the file pastes its page as before (`clipboard.importPdf {editingData}`). The objects of a hidden layer come in hidden, from a pasted EPS too. - Import and export, later 2026-10-13 merges: SVGs that move objects with a CSS transform (`translate(100px, 0)`, `rotate(45deg)`) or style type with the `font` shorthand import as drawn (#1565); SVG symbols whose shapes use percentage lengths import at the size and place they are drawn, the percentages being of the symbol's `viewBox` (#1596); Inkscape's flowed text (`flowRoot`) opens as area type in its rectangle, a paragraph per `flowPara`, instead of being dropped, and a region that can't be read is left out with a warning (#1569, fixes #1568); the warning for an unsupported SVG filter names the filter, its primitives and the object, and says the object is drawn without it (#1599, fixes #1598); the pages of an Inkscape multi-page SVG become artboards, named by their labels (#1603, fixes #1601); SVG export with CSS classes or style entities stays fast in documents with many distinct styles (#1567); PDF and `.ai` import: evenly letter-spaced or tightened type on a path keeps its length along the path instead of closing up to the font's advances (#1586), and type on a path whose stroke is written glyph by glyph comes in as one text object with its stroke, not one object per glyph (#1585); the ICC profiles embedded in JPEG, TIFF and PSD exports have every tag on a 4-byte boundary (ICC.1 §7.1.2), so strict readers accept them (#1589, fixes #1584); GIF, PNG-8 and other indexed exports find each pixel's palette colour through an index instead of a scan, with identical output (#1576); a zero-length subpath draws its round cap as a dot and its projecting cap as a square, so dots in imported `.ai` and SVG files show (#1619, fixes #1613). - Files and untrusted input, 2026-10-14 merges: EPS import: a stitched shading keeps its hard colour changes (#1654, #1653). WMF/EMF import refuses an oversized embedded PNG or JPEG from its header, before any pixels are decoded (#1776). PDF open no longer takes exponential time on a crafted name tree with shared nodes when it looks for the editing data (#1781). The size limits on a native file's embedded data also apply to files read from memory, not only streamed ones (#1783). Saving over a private file on Unix no longer leaves a readable temporary copy beside it while the file is written (#1778). An Export for Screens suffix can no longer place files outside the export folder (#1777). Package refuses a folder where it would overwrite the open document or one of its linked files (#1786). ruzstd 0.9.1 fixes a decompression bomb (RUSTSEC-2026-0344) reachable from EPS/`.ai` and Affinity import (#1807). File Info dates are stamped only after a save succeeds, so a failed save no longer changes them (#1774). Linked files that change during a drag are updated after the drag instead of being missed (#1773).