File tree 2 files changed +20
-0
lines changed
2 files changed +20
-0
lines changed Original file line number Diff line number Diff line change @@ -2577,6 +2577,7 @@ impl<T: Read> Read for Take<T> {
2577
2577
2578
2578
let max = cmp:: min ( buf. len ( ) as u64 , self . limit ) as usize ;
2579
2579
let n = self . inner . read ( & mut buf[ ..max] ) ?;
2580
+ assert ! ( n as u64 <= self . limit, "number of read bytes exceeds limit" ) ;
2580
2581
self . limit -= n as u64 ;
2581
2582
Ok ( n)
2582
2583
}
Original file line number Diff line number Diff line change @@ -583,6 +583,25 @@ fn test_write_all_vectored() {
583
583
}
584
584
}
585
585
586
+ // Issue 94981
587
+ #[ test]
588
+ #[ should_panic = "number of read bytes exceeds limit" ]
589
+ fn test_take_wrong_length ( ) {
590
+ struct LieAboutSize ( bool ) ;
591
+
592
+ impl Read for LieAboutSize {
593
+ fn read ( & mut self , buf : & mut [ u8 ] ) -> io:: Result < usize > {
594
+ // Lie about the read size at first time of read.
595
+ if core:: mem:: take ( & mut self . 0 ) { Ok ( buf. len ( ) + 1 ) } else { Ok ( buf. len ( ) ) }
596
+ }
597
+ }
598
+
599
+ let mut buffer = vec ! [ 0 ; 4 ] ;
600
+ let mut reader = LieAboutSize ( true ) . take ( 4 ) ;
601
+ // Primed the `Limit` by lying about the read size.
602
+ let _ = reader. read ( & mut buffer[ ..] ) ;
603
+ }
604
+
586
605
#[ bench]
587
606
fn bench_take_read ( b : & mut test:: Bencher ) {
588
607
b. iter ( || {
You can’t perform that action at this time.
0 commit comments