@@ -25,13 +25,13 @@ unsafe extern "system" {
2525 ) -> * mut c_void ;
2626}
2727
28- #[ repr( C ) ]
28+ #[ repr( C , packed ( 4 ) ) ]
2929struct DismFeature {
3030 feature_name : * const u16 ,
3131 state : i32 ,
3232}
3333
34- #[ repr( C ) ]
34+ #[ repr( C , packed ( 4 ) ) ]
3535struct DismFeatureInfo {
3636 feature_name : * const u16 ,
3737 state : i32 ,
@@ -248,14 +248,21 @@ impl DismSessionHandle {
248248 }
249249
250250 let result = unsafe {
251- let info = & * info_ptr;
251+ // Use addr_of! + read_unaligned because the struct is packed(4):
252+ // pointer fields are only 4-byte aligned, so we cannot create
253+ // Rust references to them (that would be UB on x64).
254+ let feature_name = std:: ptr:: addr_of!( ( * info_ptr) . feature_name) . read_unaligned ( ) ;
255+ let state = std:: ptr:: addr_of!( ( * info_ptr) . state) . read_unaligned ( ) ;
256+ let display_name = std:: ptr:: addr_of!( ( * info_ptr) . display_name) . read_unaligned ( ) ;
257+ let description = std:: ptr:: addr_of!( ( * info_ptr) . description) . read_unaligned ( ) ;
258+ let restart = std:: ptr:: addr_of!( ( * info_ptr) . restart_required) . read_unaligned ( ) ;
252259 let feature_info = WindowsFeatureInfo {
253- feature_name : Some ( from_wide_ptr ( info . feature_name ) ) ,
260+ feature_name : Some ( from_wide_ptr ( feature_name) ) ,
254261 exist : None ,
255- state : FeatureState :: from_dism ( info . state ) ,
256- display_name : Some ( from_wide_ptr ( info . display_name ) ) ,
257- description : Some ( from_wide_ptr ( info . description ) ) ,
258- restart_required : RestartType :: from_dism ( info . restart_required ) ,
262+ state : FeatureState :: from_dism ( state) ,
263+ display_name : Some ( from_wide_ptr ( display_name) ) ,
264+ description : Some ( from_wide_ptr ( description) ) ,
265+ restart_required : RestartType :: from_dism ( restart ) ,
259266 enable_all : None ,
260267 source_paths : None ,
261268 limit_access : None ,
@@ -377,9 +384,11 @@ impl DismSessionHandle {
377384 let mut result = Vec :: new ( ) ;
378385 unsafe {
379386 for i in 0 ..count as usize {
380- let feature = & * features_ptr. add ( i) ;
381- let name = from_wide_ptr ( feature. feature_name ) ;
382- result. push ( ( name, feature. state ) ) ;
387+ let fp = features_ptr. add ( i) ;
388+ let name_ptr = std:: ptr:: addr_of!( ( * fp) . feature_name) . read_unaligned ( ) ;
389+ let state = std:: ptr:: addr_of!( ( * fp) . state) . read_unaligned ( ) ;
390+ let name = from_wide_ptr ( name_ptr) ;
391+ result. push ( ( name, state) ) ;
383392 }
384393 ( self . api . delete ) ( features_ptr as * const c_void ) ;
385394 }
0 commit comments