Browse Source

Use bytemuck instead of incorrect align_to

Std docs say that prorgams should not rely on `align_to` to return empty
prefix/suffix for correctess, only performance. `bytemuck` on the other
hand deals with alignment for correctness so let's use that.
pull/18/head
Ian Chamberlain 3 years ago
parent
commit
6ba85149d8
No known key found for this signature in database
GPG Key ID: AE5484D09405AA60
  1. 1
      citro3d/Cargo.toml
  2. 16
      citro3d/src/shader.rs

1
citro3d/Cargo.toml

@ -5,6 +5,7 @@ edition = "2021"
[dependencies] [dependencies]
bitflags = "1.3.2" bitflags = "1.3.2"
bytemuck = { version = "1.10.0", features = ["extern_crate_std"] }
citro3d-sys = { git = "https://github.com/ian-h-chamberlain/citro3d-rs.git" } citro3d-sys = { git = "https://github.com/ian-h-chamberlain/citro3d-rs.git" }
ctru-rs = { git = "https://github.com/Meziu/ctru-rs.git" } ctru-rs = { git = "https://github.com/Meziu/ctru-rs.git" }
libc = "0.2.125" libc = "0.2.125"

16
citro3d/src/shader.rs

@ -98,22 +98,16 @@ impl Library {
/// An error is returned if the input data does not have an alignment of 4 /// An error is returned if the input data does not have an alignment of 4
/// (cannot be safely converted to `&[u32]`). /// (cannot be safely converted to `&[u32]`).
pub fn from_bytes(bytes: &[u8]) -> Result<Self, Box<dyn Error>> { pub fn from_bytes(bytes: &[u8]) -> Result<Self, Box<dyn Error>> {
unsafe { let aligned: &[u32] = bytemuck::try_cast_slice(bytes)?;
let (prefix, aligned, suffix) = bytes.align_to::<u32>(); Ok(Self(unsafe {
if !prefix.is_empty() || !suffix.is_empty() { citro3d_sys::DVLB_ParseFile(
// Align is incorrect, we don't want to drop any data
// TODO fill in error details
return Err("uh oh".into());
}
Ok(Self(citro3d_sys::DVLB_ParseFile(
// SAFETY: we're trusting the parse implementation doesn't mutate // SAFETY: we're trusting the parse implementation doesn't mutate
// the contents of the data. From a quick read it looks like that's // the contents of the data. From a quick read it looks like that's
// correct and it should just take a const arg in the API. // correct and it should just take a const arg in the API.
aligned.as_ptr() as *mut _, aligned.as_ptr() as *mut _,
aligned.len().try_into()?, aligned.len().try_into()?,
))) )
} }))
} }
#[must_use] #[must_use]

Loading…
Cancel
Save