diff --git a/.changes/binary-patching-32-bit.md b/.changes/binary-patching-32-bit.md new file mode 100644 index 000000000..354facbdc --- /dev/null +++ b/.changes/binary-patching-32-bit.md @@ -0,0 +1,5 @@ +--- +"tauri-bundler": "patch:bug" +--- + +Fix binary patching updater type fails on 32 bit Windows builds diff --git a/crates/tauri-bundler/src/bundle/windows/util.rs b/crates/tauri-bundler/src/bundle/windows/util.rs index 38c3299e3..0f3e9bde5 100644 --- a/crates/tauri-bundler/src/bundle/windows/util.rs +++ b/crates/tauri-bundler/src/bundle/windows/util.rs @@ -100,17 +100,16 @@ pub fn patch_binary(binary_path: &PathBuf, package_type: &crate::PackageType) -> .ok_or(crate::Error::MissingBundleTypeVar)?; let data_offset = tauri_bundle_section.pointer_to_raw_data as usize; - - if data_offset + 8 > file_data.len() { - return Err(crate::Error::BinaryOffsetOutOfRange); - } - - let ptr_bytes = &file_data[data_offset..data_offset + 8]; - let ptr_value = u64::from_le_bytes(ptr_bytes.try_into().map_err(|_| { - crate::Error::BinaryParseError( - std::io::Error::new(std::io::ErrorKind::InvalidData, "invalid pointer bytes").into(), - ) - })?); + let pointer_size = if pe.is_64 { 8 } else { 4 }; + let ptr_bytes = file_data + .get(data_offset..data_offset + pointer_size) + .ok_or(crate::Error::BinaryOffsetOutOfRange)?; + // `try_into` is safe to `unwrap` here because we have already checked the slice's size through `get` + let ptr_value = if pe.is_64 { + u64::from_le_bytes(ptr_bytes.try_into().unwrap()) + } else { + u32::from_le_bytes(ptr_bytes.try_into().unwrap()).into() + }; let rdata_section = pe .sections @@ -133,12 +132,10 @@ pub fn patch_binary(binary_path: &PathBuf, package_type: &crate::PackageType) -> let file_offset = rdata_section.pointer_to_raw_data as usize + (rva as usize).saturating_sub(rdata_section.virtual_address as usize); - if file_offset + 3 > file_data.len() { - return Err(crate::Error::BinaryOffsetOutOfRange); - } - // Overwrite the string at that offset - let string_bytes = &mut file_data[file_offset..file_offset + 3]; + let string_bytes = file_data + .get_mut(file_offset..file_offset + 3) + .ok_or(crate::Error::BinaryOffsetOutOfRange)?; match package_type { crate::PackageType::Nsis => string_bytes.copy_from_slice(b"NSS"), crate::PackageType::WindowsMsi => string_bytes.copy_from_slice(b"MSI"),