Skip to content

Commit 936ab41

Browse files
committed
Replac uses of from_utf8_unchecked with from_utf8.
Fixes #43.
1 parent 0ebd1da commit 936ab41

File tree

2 files changed

+12
-20
lines changed

2 files changed

+12
-20
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
88
## [Unreleased]
99

1010
- Use more idiomatic rust code in readme/doc.rs example.
11+
- Use `str::from_utf8` instead of `str::from_utf8_unchecked` to avoid potential
12+
panics with the Deserialize trait (Fixes #43).
1113

1214
## [9.1.0] - 2021-07-03
1315

src/lib.rs

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -712,16 +712,13 @@ impl VendorInfo {
712712
/// Return vendor identification as human readable string.
713713
pub fn as_string<'a>(&'a self) -> &'a str {
714714
let brand_string_start = self as *const VendorInfo as *const u8;
715-
unsafe {
715+
let slice = unsafe {
716716
// Safety: VendorInfo is laid out with repr(C) and exactly
717717
// 12 byte long without any padding.
718-
let slice: &'a [u8] =
719-
slice::from_raw_parts(brand_string_start, size_of::<VendorInfo>());
720-
// Safety: The field is specified to be ASCII, and the only safe
721-
// way to construct VendorInfo is from real CPUID data or the
722-
// Default implementation.
723-
str::from_utf8_unchecked(slice)
724-
}
718+
slice::from_raw_parts(brand_string_start, size_of::<VendorInfo>())
719+
};
720+
721+
str::from_utf8(slice).unwrap_or("InvalidVendorString")
725722
}
726723
}
727724

@@ -4193,15 +4190,11 @@ pub struct SoCVendorBrand {
41934190
impl SoCVendorBrand {
41944191
pub fn as_string<'a>(&'a self) -> &'a str {
41954192
let brand_string_start = self as *const SoCVendorBrand as *const u8;
4196-
unsafe {
4193+
let slice = unsafe {
41974194
// Safety: SoCVendorBrand is laid out with repr(C).
4198-
let slice: &'a [u8] =
4199-
slice::from_raw_parts(brand_string_start, size_of::<SoCVendorBrand>());
4200-
// Safety: The field is specified to be ASCII, and the only safe
4201-
// way to construct SoCVendorBrand is from real CPUID data or the
4202-
// Default implementation.
4203-
str::from_utf8_unchecked(slice)
4204-
}
4195+
slice::from_raw_parts(brand_string_start, size_of::<SoCVendorBrand>())
4196+
};
4197+
str::from_utf8(slice).unwrap_or("InvalidSoCVendorString")
42054198
}
42064199
}
42074200

@@ -4329,10 +4322,7 @@ impl ExtendedFunctionInfo {
43294322
// Brand terminated at nul byte or end, whichever comes first.
43304323
let slice = slice.split(|&x| x == 0).next().unwrap();
43314324

4332-
// Safety: Field is specified to be ASCII, and the only safe way
4333-
// to construct ExtendedFunctionInfo is from real CPUID data
4334-
// or the Default implementation.
4335-
Some(unsafe { str::from_utf8_unchecked(slice) })
4325+
str::from_utf8(slice).ok()
43364326
} else {
43374327
None
43384328
}

0 commit comments

Comments
 (0)