From 5171da2d03f109f7b4431ddab7cdf3b68b345dff Mon Sep 17 00:00:00 2001 From: Osvaldo Duran <338149330+regendorf-ux@users.noreply.github.com> Date: Mon, 19 Oct 2026 20:59:42 -0400 Subject: [PATCH] Count a file name's bytes, not its characters, when renaming The rename check limited a new file name to 255 characters, but file systems limit bytes (ext4, APFS: 255 bytes of UTF-8; NTFS: 255 UTF-16 units). A 94 character CJK name passed the check and the rename then failed. The name is now refused when it is over 255 UTF-8 bytes or over 255 UTF-16 code units, with the same error type; the message and the tool description say bytes. The limit constant is renamed to MAX_NAME_LEN (it had no other users). Test: crates/engine/tests/rename_name_length.rs::a_name_over_255_bytes_is_refused; before this change it fails because a 94-character CJK name (286 bytes with the extension) was accepted. Co-Authored-By: Claude Opus 5.5 --- crates/automation/src/tools.rs | 2 +- crates/engine/src/rename.rs | 11 ++++---- crates/engine/tests/rename_name_length.rs | 32 +++++++++++++++++++++++ 3 files changed, 39 insertions(+), 6 deletions(-) create mode 100644 crates/engine/tests/rename_name_length.rs diff --git a/crates/automation/src/tools.rs b/crates/automation/src/tools.rs index de8da1e9bf..1ebf6a071a 100644 --- a/crates/automation/src/tools.rs +++ b/crates/automation/src/tools.rs @@ -159,7 +159,7 @@ pub fn tools() -> Vec { t( "doc_rename", "Rename a document's file", - "Rename the open document's file on disk, in its own folder; the document stays open with its undo history. The extension is kept (added back when left out). Refused, with the reason, when the document has unsaved changes (save first), has no file yet, the name is taken, the file is read-only or in use, or the name can't be a file name on Windows, macOS or Linux (no / \\ : * ? \" < > |, no device names such as CON, no trailing dot, at most 255 characters).", + "Rename the open document's file on disk, in its own folder; the document stays open with its undo history. The extension is kept (added back when left out). Refused, with the reason, when the document has unsaved changes (save first), has no file yet, the name is taken, the file is read-only or in use, or the name can't be a file name on Windows, macOS or Linux (no / \\ : * ? \" < > |, no device names such as CON, no trailing dot, at most 255 bytes).", ) .destructive() .with(schema(json!({ "doc": doc(), "name": { "type": "string", "description": "The new file name, with or without the extension." } }), &["doc", "name"])), diff --git a/crates/engine/src/rename.rs b/crates/engine/src/rename.rs index 717a48e301..3b55af55de 100644 --- a/crates/engine/src/rename.rs +++ b/crates/engine/src/rename.rs @@ -5,7 +5,7 @@ //! The rules are the same on every system, so a file renamed on one keeps working when the folder //! is synced to another: no path separators or `< > : " | ? *`, no control characters, no Windows //! device names (`CON`, `NUL`, `COM1`…), no name ending in a dot or a space, at most 255 -//! characters. The file keeps its extension: a name typed without it gets it back. +//! bytes of UTF-8 and 255 UTF-16 code units. The file keeps its extension: a name typed without it gets it back. use std::path::{Path, PathBuf}; @@ -53,8 +53,9 @@ const DEVICE_NAMES: [&str; 22] = [ "CON", "PRN", "AUX", "NUL", "COM1", "COM2", "COM3", "COM4", "COM5", "COM6", "COM7", "COM8", "COM9", "LPT1", "LPT2", "LPT3", "LPT4", "LPT5", "LPT6", "LPT7", "LPT8", "LPT9", ]; -/// The longest file name the common file systems take. -const MAX_NAME_CHARS: usize = 255; +/// The longest file name the common file systems take: 255 bytes of UTF-8 (ext4, APFS) and 255 +/// UTF-16 code units (NTFS), so a name is checked against both, never by its characters. +const MAX_NAME_LEN: usize = 255; /// The file name `typed` gives the document now at `current`: trimmed, checked, with /// `current`'s extension added back when it was left out. @@ -81,8 +82,8 @@ pub fn new_file_name(current: &Path, typed: &str) -> Result if DEVICE_NAMES.iter().any(|d| d.eq_ignore_ascii_case(stem)) { return Err(RenameError::Invalid(format!("{stem} is a name Windows reserves for a device"))); } - if name.chars().count() > MAX_NAME_CHARS { - return Err(RenameError::Invalid(format!("a file name can be at most {MAX_NAME_CHARS} characters"))); + if name.len() > MAX_NAME_LEN || name.encode_utf16().count() > MAX_NAME_LEN { + return Err(RenameError::Invalid(format!("a file name can be at most {MAX_NAME_LEN} bytes"))); } Ok(name) } diff --git a/crates/engine/tests/rename_name_length.rs b/crates/engine/tests/rename_name_length.rs new file mode 100644 index 0000000000..0e8c30e727 --- /dev/null +++ b/crates/engine/tests/rename_name_length.rs @@ -0,0 +1,32 @@ +//! A file name is limited in bytes (UTF-8 on ext4 and APFS) and UTF-16 units (NTFS), not in +//! characters, so a long name of wide characters has to be refused before the rename fails. + +use std::path::Path; + +use pdfcraft_engine::{RenameError, new_file_name}; + +fn name(typed: &str) -> Result { + new_file_name(Path::new("/docs/report.pdf"), typed) +} + +#[test] +fn a_name_over_255_bytes_is_refused() { + // 94 characters of 3 bytes each: 282 bytes, then ".pdf" makes 286. Under 255 characters. + let cjk = "年".repeat(94); + assert!(cjk.chars().count() < 255); + assert!(matches!(name(&cjk), Err(RenameError::Invalid(_))), "282 bytes of CJK must be refused"); + + // 251 ASCII characters + ".pdf" is exactly 255 bytes: accepted. + let exact = "x".repeat(251); + assert_eq!(name(&exact), Ok(format!("{exact}.pdf"))); + + // One more byte is refused. + assert!(matches!(name(&"x".repeat(252)), Err(RenameError::Invalid(_)))); + + // 80 CJK characters + ".pdf" is 244 bytes: accepted. + let short = "年".repeat(80); + assert_eq!(name(&short), Ok(format!("{short}.pdf"))); + + // Characters outside the BMP take 2 UTF-16 units and 4 bytes: 63 + ".pdf" is 256 bytes. + assert!(matches!(name(&"😀".repeat(63)), Err(RenameError::Invalid(_)))); +}