From b7b01494926a134f39daf8102281889702ddf62b Mon Sep 17 00:00:00 2001 From: salmonik Date: Sun, 15 Nov 2026 17:13:24 +0100 Subject: 0.5.3: session locking, UTF-8 and size guards for edit, correct character widths - exclusive flock on session files - edit refuses non-UTF-8 and >64 MiB files with actionable messages - editor uses unicode-width, expands tabs, handles resize --- src/tools/fs.rs | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) (limited to 'src/tools/fs.rs') diff --git a/src/tools/fs.rs b/src/tools/fs.rs index 947649f..ac0a475 100644 --- a/src/tools/fs.rs +++ b/src/tools/fs.rs @@ -10,6 +10,9 @@ const MAX_LINE_BYTES: usize = 4096; const MAX_READ_LINES: usize = 2000; const MAX_MATCHES: usize = 200; const MAX_SCAN_BYTES: u64 = 4 * 1024 * 1024; +/// Edit rewrites the whole file, so it has to fit comfortably in the 32-bit +/// address space together with its own copy. +const MAX_EDIT_BYTES: u64 = 64 * 1024 * 1024; const PREVIEW_BUDGET: usize = 60; const SKIP_DIRS: &[&str] = &[ @@ -239,7 +242,19 @@ pub fn plan_edit(args: &Value) -> Result { if old.is_empty() { return Err("`old` must not be empty; use write to create a file".into()); } - let text = fs::read_to_string(&path).map_err(|e| format!("{path}: {e}"))?; + let bytes = fs::read(&path).map_err(|e| format!("{path}: {e}"))?; + if bytes.len() as u64 > MAX_EDIT_BYTES { + return Err(format!( + "{path} is {} MiB, too large to edit safely; use bash (sed) instead", + bytes.len() / (1024 * 1024) + )); + } + let text = String::from_utf8(bytes).map_err(|_| { + format!( + "{path} is not valid UTF-8, so edit cannot round-trip it without \ + corrupting the file; use bash (sed, perl) instead" + ) + })?; // Fail before the approval prompt if the match is unusable. locate(&text, &old)?; Ok(Change::Edit { path, old, new }) -- cgit v1.2.3