From e3103705a1770108b3a46886c9b5a40280077439 Mon Sep 17 00:00:00 2001 From: Rednaxeh <360394513+Rednaxeh@users.noreply.github.com> Date: Tue, 24 Nov 2026 01:16:49 +0100 Subject: [PATCH] XGameSave: a "%" in a save folder name is decoded by bytes, not by slicing the str MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `dec` turned "%XX" back into a byte with `u8::from_str_radix(&file[i + 1..i + 3], 16)`. Slicing the name at byte offsets panics when a multi-byte character follows the hex digit ("%aƩ": "end byte index 3 is not a char boundary"), and `dec` runs on every folder and file name of the save provider from `extern "system"` entry points, where the panic cannot unwind: XGameSaveEnumerateContainerInfo over such a folder ends the title with 0xC0000409. XCL's own `enc` never writes such a name; a folder restored from a backup or placed by hand can have one. `from_str_radix` also took a sign, so "%+1" decoded to the byte 1. The two characters after "%" are now read as bytes and must both be hex digits; anything else stays as it is. Fixes #325. Co-Authored-By: Claude Fable 5.1 --- crates/xcl/src/hle/xgameruntime/gamesave.rs | 43 ++++++++++++++++++--- 1 file changed, 37 insertions(+), 6 deletions(-) diff --git a/crates/xcl/src/hle/xgameruntime/gamesave.rs b/crates/xcl/src/hle/xgameruntime/gamesave.rs index e529c1817..49ca47c32 100644 --- a/crates/xcl/src/hle/xgameruntime/gamesave.rs +++ b/crates/xcl/src/hle/xgameruntime/gamesave.rs @@ -96,12 +96,13 @@ fn dec(file: &str) -> String { let mut out = Vec::new(); let mut i = 0; while i < b.len() { - if b[i] == b'%' && i + 2 < b.len() { - if let Ok(v) = u8::from_str_radix(&file[i + 1..i + 3], 16) { - out.push(v); - i += 3; - continue; - } + // %XX by bytes: a name put in the folder by hand may hold a multi-byte character right + // after the "%" (slicing the str inside it panics), and `from_str_radix` takes a sign + if b[i] == b'%' && i + 2 < b.len() && b[i + 1].is_ascii_hexdigit() && b[i + 2].is_ascii_hexdigit() { + let hex = |c: u8| (c as char).to_digit(16).unwrap_or(0) as u8; + out.push(hex(b[i + 1]) << 4 | hex(b[i + 2])); + i += 3; + continue; } out.push(b[i]); i += 1; @@ -817,6 +818,36 @@ mod tests { true } + /// A "%" that two hex digits do not follow stays as it is: slicing the name at byte + /// offsets panicked when a multi-byte character followed, and `from_str_radix` took the + /// sign in "%+1" (#325). + #[test] + fn dec_keeps_a_percent_without_two_hex_digits() { + assert_eq!(dec("%a\u{e9}"), "%a\u{e9}"); + assert_eq!(dec("%+1"), "%+1"); + assert_eq!(dec("%2Fa%"), "/a%"); + assert_eq!(dec(&enc("\u{e9}/ 1%")), "\u{e9}/ 1%"); + } + + /// A container folder placed in the provider's root by hand (a restored backup) whose name + /// holds "%", a hex digit and a multi-byte character is enumerated, not a panic across + /// `extern "system"` that ends the title (#325). + #[test] + fn a_container_folder_named_percent_then_multibyte_is_enumerated() { + let root = std::env::temp_dir().join(format!("xcl-gamesave-325-{}", std::process::id())); + let dir = root.join("%a\u{e9}"); + std::fs::create_dir_all(dir.join("b")).unwrap(); + std::fs::write(dir.join("b").join("data"), b"x").unwrap(); + let p = Box::into_raw(Box::new(Provider { magic: MAGIC_PROVIDER, root: root.clone() })); + let mut list: Vec<(String, String, u32, u64)> = Vec::new(); + unsafe { + assert_eq!(XGameSaveEnumerateContainerInfo(p, &mut list as *mut _ as *mut c_void, Some(count_containers)), S_OK); + drop(Box::from_raw(p)); + } + let _ = std::fs::remove_dir_all(&root); + assert_eq!(list, [("%a\u{e9}".to_string(), "%a\u{e9}".to_string(), 1, 1)]); + } + /// Names Windows does not keep as file names (device names, a trailing dot or space, /// "." / "..") come back as they were written. #[test]