fix(windows): binary patching 32 bit updater type (#14065)

* fix(windows): binary patching 32 bit updater type

* Use `get` instead of size check and then assert
This commit is contained in:
Tony 2025-08-24 19:16:12 +08:00 committed by GitHub
parent c0d3f9d47e
commit f3df96fb38
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 18 additions and 16 deletions

View File

@ -0,0 +1,5 @@
---
"tauri-bundler": "patch:bug"
---
Fix binary patching updater type fails on 32 bit Windows builds

View File

@ -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"),