1- use futures_core:: ready;
21use futures_core:: future:: Future ;
2+ use futures_core:: ready;
33use futures_core:: task:: { Context , Poll } ;
44use futures_io:: AsyncRead ;
55use std:: io;
@@ -28,11 +28,16 @@ impl<'a, R: AsyncRead + ?Sized + Unpin> ReadToEnd<'a, R> {
2828 }
2929}
3030
31- struct Guard < ' a > { buf : & ' a mut Vec < u8 > , len : usize }
31+ struct Guard < ' a > {
32+ buf : & ' a mut Vec < u8 > ,
33+ len : usize ,
34+ }
3235
3336impl Drop for Guard < ' _ > {
3437 fn drop ( & mut self ) {
35- unsafe { self . buf . set_len ( self . len ) ; }
38+ unsafe {
39+ self . buf . set_len ( self . len ) ;
40+ }
3641 }
3742}
3843
@@ -51,8 +56,10 @@ pub(super) fn read_to_end_internal<R: AsyncRead + ?Sized>(
5156 buf : & mut Vec < u8 > ,
5257 start_len : usize ,
5358) -> Poll < io:: Result < usize > > {
54- let mut g = Guard { len : buf. len ( ) , buf } ;
55- let ret;
59+ let mut g = Guard {
60+ len : buf. len ( ) ,
61+ buf,
62+ } ;
5663 loop {
5764 if g. len == g. buf . len ( ) {
5865 unsafe {
@@ -63,24 +70,24 @@ pub(super) fn read_to_end_internal<R: AsyncRead + ?Sized>(
6370 }
6471 }
6572
66- match ready ! ( rd . as_mut ( ) . poll_read ( cx , & mut g. buf[ g. len..] ) ) {
67- Ok ( 0 ) => {
68- ret = Poll :: Ready ( Ok ( g. len - start_len) ) ;
69- break ;
70- }
71- Ok ( n ) => g . len += n ,
72- Err ( e ) => {
73- ret = Poll :: Ready ( Err ( e ) ) ;
74- break ;
73+ let buf = & mut g. buf [ g. len ..] ;
74+ match ready ! ( rd . as_mut ( ) . poll_read ( cx , buf ) ) {
75+ Ok ( 0 ) => return Poll :: Ready ( Ok ( g. len - start_len) ) ,
76+ Ok ( n ) => {
77+ // We can't allow bogus values from read. If it is too large, the returned vec could have its length
78+ // set past its capacity, or if it overflows the vec could be shortened which could create an invalid
79+ // string if this is called via read_to_string.
80+ assert ! ( n <= buf . len ( ) ) ;
81+ g . len += n ;
7582 }
83+ Err ( e) => return Poll :: Ready ( Err ( e) ) ,
7684 }
7785 }
78-
79- ret
8086}
8187
8288impl < A > Future for ReadToEnd < ' _ , A >
83- where A : AsyncRead + ?Sized + Unpin ,
89+ where
90+ A : AsyncRead + ?Sized + Unpin ,
8491{
8592 type Output = io:: Result < usize > ;
8693
0 commit comments