From 4c97cae249e1e4005ecbc48df7723c27d27675c5 Mon Sep 17 00:00:00 2001 From: Simon Gardling Date: Wed, 3 Dec 2025 00:25:25 -0500 Subject: [PATCH] fix localstorage error --- Cargo.lock | 7 +++++++ Cargo.toml | 1 + src/math_app.rs | 1 + src/misc.rs | 23 +++++++++++++---------- 4 files changed, 22 insertions(+), 10 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 3421004..d5fb7ed 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -136,6 +136,12 @@ version = "1.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c08606f8c3cbf4ce6ec8e28fb0014a2c086708fe954eaa885384a6165172e7e8" +[[package]] +name = "base64" +version = "0.22.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "72b3254f16251a8381aa12e40e3c4d2f0199f8c6508fbecb9d91f575e0fbb8c6" + [[package]] name = "bincode" version = "1.3.3" @@ -3680,6 +3686,7 @@ dependencies = [ name = "ytbn_graphing_software" version = "0.1.0" dependencies = [ + "base64", "bincode", "cfg-if", "const_format", diff --git a/Cargo.toml b/Cargo.toml index fe97525..b392e7b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -57,6 +57,7 @@ static_assertions = "1.1" bincode = "1.3" serde = "1" log = "0.4" +base64 = "0.22" # Note: benchmarks are in a separate crate - run with: # cd benchmarks && cargo bench diff --git a/src/math_app.rs b/src/math_app.rs index 02d6db0..2f3cb58 100644 --- a/src/math_app.rs +++ b/src/math_app.rs @@ -176,6 +176,7 @@ impl MathApp { // TODO: stabilize FunctionManager serialize so it can persist across builds let (commit, func_data) = crate::misc::hashed_storage_read(&data)?; + let func_data: &[u8] = &func_data; if commit == unsafe { std::mem::transmute::<&str, &[u8]>(build::SHORT_COMMIT) } { diff --git a/src/misc.rs b/src/misc.rs index 40a87d2..c784edf 100644 --- a/src/misc.rs +++ b/src/misc.rs @@ -179,24 +179,27 @@ pub type HashBytes = [u8; HASH_LENGTH]; #[allow(dead_code)] pub fn hashed_storage_create(hashbytes: HashBytes, data: &[u8]) -> String { - unsafe { std::mem::transmute::, String>([hashbytes.to_vec(), data.to_vec()].concat()) } + // Use base64 encoding to safely store binary data in localStorage + let combined: Vec = [hashbytes.to_vec(), data.to_vec()].concat(); + base64::encode(combined) } #[allow(dead_code)] -pub fn hashed_storage_read(data: &str) -> Option<(HashBytes, &[u8])> { +pub fn hashed_storage_read(data: &str) -> Option<(HashBytes, Vec)> { + // Decode base64 data + let decoded = base64::decode(data).ok()?; + // Make sure data is long enough to decode - if HASH_LENGTH >= data.len() { + if HASH_LENGTH >= decoded.len() { return None; } - // Transmute data into slice - let decoded_1: &[u8] = unsafe { std::mem::transmute::<&str, &[u8]>(data) }; + // Extract hash and data + let mut hash: HashBytes = [0u8; HASH_LENGTH]; + hash.copy_from_slice(&decoded[..HASH_LENGTH]); + let data_part = decoded[HASH_LENGTH..].to_vec(); - // Return hash and decoded data - Some(( - unsafe { *(decoded_1[..HASH_LENGTH].as_ptr() as *const HashBytes) }, - &decoded_1[HASH_LENGTH..], - )) + Some((hash, data_part)) } /// Creates and returns random u64