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 @@ -4546,7 +4546,8 @@ pub unsafe trait FromBytes: FromZeros {
4546
4546
Self : Sized ,
4547
4547
R : io:: Read ,
4548
4548
{
4549
- let mut buf = CoreMaybeUninit :: < Self > :: zeroed ( ) ;
4549
+ let mut buf = CoreMaybeUninit :: < Self > :: uninit ( ) ;
4550
+ buf. zero ( ) ;
4550
4551
let ptr = Ptr :: from_mut ( & mut buf) ;
4551
4552
// SAFETY: `buf` consists entirely of initialized, zeroed bytes.
4552
4553
let ptr = unsafe { ptr. assume_validity :: < invariant:: Initialized > ( ) } ;
@@ -5905,6 +5906,29 @@ mod tests {
5905
5906
assert_eq ! ( bytes, want) ;
5906
5907
}
5907
5908
5909
+ #[ test]
5910
+ #[ cfg( feature = "std" ) ]
5911
+ fn test_read_io_with_padding_soundness ( ) {
5912
+ #[ derive( FromBytes ) ]
5913
+ #[ repr( C ) ]
5914
+ struct WithPadding {
5915
+ x : u8 ,
5916
+ y : u16 ,
5917
+ }
5918
+ struct ReadsInRead ;
5919
+ impl std:: io:: Read for ReadsInRead {
5920
+ fn read ( & mut self , buf : & mut [ u8 ] ) -> std:: io:: Result < usize > {
5921
+ if buf. iter ( ) . all ( |& x| x == 0 ) {
5922
+ Ok ( buf. len ( ) )
5923
+ } else {
5924
+ buf. iter_mut ( ) . for_each ( |x| * x = 0 ) ;
5925
+ Ok ( buf. len ( ) )
5926
+ }
5927
+ }
5928
+ }
5929
+ assert ! ( matches!( WithPadding :: read_from_io( ReadsInRead ) , Ok ( WithPadding { x: 0 , y: 0 } ) ) ) ;
5930
+ }
5931
+
5908
5932
#[ test]
5909
5933
#[ cfg( feature = "std" ) ]
5910
5934
fn test_read_write_io ( ) {
You can’t perform that action at this time.
0 commit comments