@@ -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 {
41934190impl 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