File tree Expand file tree Collapse file tree 2 files changed +22
-1
lines changed Expand file tree Collapse file tree 2 files changed +22
-1
lines changed Original file line number Diff line number Diff line change @@ -578,8 +578,13 @@ where
578578 F : FnOnce ( & mut [ u8 ] ) -> Result < usize > ,
579579{
580580 let n = read ( cursor. ensure_init ( ) . init_mut ( ) ) ?;
581+ assert ! (
582+ n <= cursor. capacity( ) ,
583+ "read should not return more bytes than there is capacity for in the read buffer"
584+ ) ;
581585 unsafe {
582- // SAFETY: we initialised using `ensure_init` so there is no uninit data to advance to.
586+ // SAFETY: we initialised using `ensure_init` so there is no uninit data to advance to
587+ // and we have checked that the read amount is not over capacity (see #120603)
583588 cursor. advance ( n) ;
584589 }
585590 Ok ( ( ) )
Original file line number Diff line number Diff line change @@ -652,3 +652,19 @@ fn bench_take_read_buf(b: &mut test::Bencher) {
652652 [ 255 ; 128 ] . take ( 64 ) . read_buf ( buf. unfilled ( ) ) . unwrap ( ) ;
653653 } ) ;
654654}
655+
656+ // Issue #120603
657+ #[ test]
658+ #[ should_panic = "read should not return more bytes than there is capacity for in the read buffer" ]
659+ fn read_buf_broken_read ( ) {
660+ struct MalformedRead ;
661+
662+ impl Read for MalformedRead {
663+ fn read ( & mut self , buf : & mut [ u8 ] ) -> io:: Result < usize > {
664+ // broken length calculation
665+ Ok ( buf. len ( ) + 1 )
666+ }
667+ }
668+
669+ BufReader :: new ( MalformedRead ) . read ( & mut [ 0 ; 4 ] ) . unwrap ( ) ;
670+ }
You can’t perform that action at this time.
0 commit comments