Bind header+u32 to variable for clearer math

This commit is contained in:
Rune Tynan 2023-02-13 13:44:14 -05:00
parent 7df53d5e18
commit a9c3eb91e9
No known key found for this signature in database
GPG Key ID: 7ECC932F8B2C731E

View File

@ -789,6 +789,9 @@ fn get_metadata_section<'p>(
loader.get_dylib_metadata(target, filename).map_err(MetadataError::LoadFailure)?;
// The header is uncompressed
let header_len = METADATA_HEADER.len();
let u32_len = core::mem::size_of::<u32>();
let data_start = header_len + u32_len;
debug!("checking {} bytes of metadata-version stamp", header_len);
let header = &buf[..cmp::min(header_len, buf.len())];
if header != METADATA_HEADER {
@ -799,14 +802,13 @@ fn get_metadata_section<'p>(
}
// Length of the compressed stream - this allows linkers to pad the section if they want
let u32_len = core::mem::size_of::<u32>();
let Ok(len_bytes) = <[u8; 4]>::try_from(&buf[header_len..cmp::min(header_len + u32_len, buf.len())]) else {
let Ok(len_bytes) = <[u8; 4]>::try_from(&buf[header_len..cmp::min(data_start, buf.len())]) else {
return Err(MetadataError::LoadFailure("invalid metadata length found".to_string()));
};
let compressed_len = u32::from_be_bytes(len_bytes) as usize;
// Header is okay -> inflate the actual metadata
let compressed_bytes = &buf[(header_len + u32_len)..(compressed_len + header_len + u32_len)];
let compressed_bytes = &buf[data_start..(data_start + compressed_len)];
debug!("inflating {} bytes of compressed metadata", compressed_bytes.len());
// Assume the decompressed data will be at least the size of the compressed data, so we
// don't have to grow the buffer as much.