Skip to content

Commit

Permalink
Fix UB in FromRegValue for u32 and u64
Browse files Browse the repository at this point in the history
`TryInto` requires rust-1.34.

Fixes #61
  • Loading branch information
gentoo90 committed Nov 19, 2023
1 parent 9aea2ad commit 7634148
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
- channel: stable
target: x86_64-pc-windows-msvc
lint: true
- channel: 1.31.1
- channel: 1.34.2
target: x86_64-pc-windows-msvc
restrict_deps_versions: true
env:
Expand Down
2 changes: 1 addition & 1 deletion clippy.toml
Original file line number Diff line number Diff line change
@@ -1 +1 @@
msrv = "1.31"
msrv = "1.34"
18 changes: 14 additions & 4 deletions src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use crate::common::*;
use crate::enums::*;
use crate::RegValue;
use std::convert::TryInto;
use std::ffi::{OsStr, OsString};
use std::io;
use std::os::windows::ffi::OsStringExt;
Expand Down Expand Up @@ -107,11 +108,21 @@ impl FromRegValue for Vec<OsString> {
}
}

macro_rules! try_from_reg_value_int {
($val:expr, $map:expr) => {
$val.bytes
.as_slice()
.try_into()
.map($map)
.map_err(|_| io::Error::from_raw_os_error(winerror::ERROR_INVALID_DATA as i32))
};
}

impl FromRegValue for u32 {
fn from_reg_value(val: &RegValue) -> io::Result<u32> {
match val.vtype {
#[allow(clippy::cast_ptr_alignment)]
REG_DWORD => Ok(unsafe { *(val.bytes.as_ptr() as *const u32) }),
REG_DWORD => try_from_reg_value_int!(val, u32::from_ne_bytes),
REG_DWORD_BIG_ENDIAN => try_from_reg_value_int!(val, u32::from_be_bytes),
_ => werr!(winerror::ERROR_BAD_FILE_TYPE),
}
}
Expand All @@ -120,8 +131,7 @@ impl FromRegValue for u32 {
impl FromRegValue for u64 {
fn from_reg_value(val: &RegValue) -> io::Result<u64> {
match val.vtype {
#[allow(clippy::cast_ptr_alignment)]
REG_QWORD => Ok(unsafe { *(val.bytes.as_ptr() as *const u64) }),
REG_QWORD => try_from_reg_value_int!(val, u64::from_ne_bytes),
_ => werr!(winerror::ERROR_BAD_FILE_TYPE),
}
}
Expand Down

0 comments on commit 7634148

Please sign in to comment.