File tree 1 file changed +25
-1
lines changed
1 file changed +25
-1
lines changed Original file line number Diff line number Diff line change @@ -4567,7 +4567,8 @@ pub unsafe trait FromBytes: FromZeros {
4567
4567
Self : Sized ,
4568
4568
R : io:: Read ,
4569
4569
{
4570
- let mut buf = CoreMaybeUninit :: < Self > :: zeroed ( ) ;
4570
+ let mut buf = CoreMaybeUninit :: < Self > :: uninit ( ) ;
4571
+ buf. zero ( ) ;
4571
4572
let ptr = Ptr :: from_mut ( & mut buf) ;
4572
4573
// SAFETY: `buf` consists entirely of initialized, zeroed bytes.
4573
4574
let ptr = unsafe { ptr. assume_validity :: < invariant:: Initialized > ( ) } ;
@@ -6079,6 +6080,29 @@ mod tests {
6079
6080
assert_eq ! ( bytes, want) ;
6080
6081
}
6081
6082
6083
+ #[ test]
6084
+ #[ cfg( feature = "std" ) ]
6085
+ fn test_read_io_with_padding_soundness ( ) {
6086
+ #[ derive( FromBytes ) ]
6087
+ #[ repr( C ) ]
6088
+ struct WithPadding {
6089
+ x : u8 ,
6090
+ y : u16 ,
6091
+ }
6092
+ struct ReadsInRead ;
6093
+ impl std:: io:: Read for ReadsInRead {
6094
+ fn read ( & mut self , buf : & mut [ u8 ] ) -> std:: io:: Result < usize > {
6095
+ if buf. iter ( ) . all ( |& x| x == 0 ) {
6096
+ Ok ( buf. len ( ) )
6097
+ } else {
6098
+ buf. into_iter ( ) . for_each ( |x| * x = 0 ) ;
6099
+ Ok ( buf. len ( ) )
6100
+ }
6101
+ }
6102
+ }
6103
+ assert ! ( matches!( WithPadding :: read_from_io( ReadsInRead ) , Ok ( WithPadding { x: 0 , y: 0 } ) ) ) ;
6104
+ }
6105
+
6082
6106
#[ test]
6083
6107
#[ cfg( feature = "std" ) ]
6084
6108
fn test_read_write_io ( ) {
You can’t perform that action at this time.
0 commit comments