File tree Expand file tree Collapse file tree 3 files changed +6
-3
lines changed Expand file tree Collapse file tree 3 files changed +6
-3
lines changed Original file line number Diff line number Diff line change @@ -770,14 +770,14 @@ impl Read for &File {
770770 // Reserves space in the buffer based on the file size when available.
771771 fn read_to_end ( & mut self , buf : & mut Vec < u8 > ) -> io:: Result < usize > {
772772 let size = buffer_capacity_required ( self ) ;
773- buf. reserve ( size. unwrap_or ( 0 ) ) ;
773+ buf. try_reserve ( size. unwrap_or ( 0 ) ) . map_err ( |_| io :: ErrorKind :: OutOfMemory ) ? ;
774774 io:: default_read_to_end ( self , buf, size)
775775 }
776776
777777 // Reserves space in the buffer based on the file size when available.
778778 fn read_to_string ( & mut self , buf : & mut String ) -> io:: Result < usize > {
779779 let size = buffer_capacity_required ( self ) ;
780- buf. reserve ( size. unwrap_or ( 0 ) ) ;
780+ buf. try_reserve ( size. unwrap_or ( 0 ) ) . map_err ( |_| io :: ErrorKind :: OutOfMemory ) ? ;
781781 io:: default_read_to_string ( self , buf, size)
782782 }
783783}
Original file line number Diff line number Diff line change @@ -345,6 +345,7 @@ impl<R: ?Sized + Read> Read for BufReader<R> {
345345 // delegate to the inner implementation.
346346 fn read_to_end ( & mut self , buf : & mut Vec < u8 > ) -> io:: Result < usize > {
347347 let inner_buf = self . buffer ( ) ;
348+ buf. try_reserve ( inner_buf. len ( ) ) . map_err ( |_| io:: ErrorKind :: OutOfMemory ) ?;
348349 buf. extend_from_slice ( inner_buf) ;
349350 let nread = inner_buf. len ( ) ;
350351 self . discard_buffer ( ) ;
Original file line number Diff line number Diff line change @@ -419,7 +419,8 @@ pub(crate) fn default_read_to_end<R: Read + ?Sized>(
419419 let mut initialized = 0 ; // Extra initialized bytes from previous loop iteration
420420 loop {
421421 if buf. len ( ) == buf. capacity ( ) {
422- buf. reserve ( 32 ) ; // buf is full, need more space
422+ // buf is full, need more space
423+ buf. try_reserve ( 32 ) . map_err ( |_| ErrorKind :: OutOfMemory ) ?;
423424 }
424425
425426 let mut spare = buf. spare_capacity_mut ( ) ;
@@ -465,6 +466,7 @@ pub(crate) fn default_read_to_end<R: Read + ?Sized>(
465466 match r. read ( & mut probe) {
466467 Ok ( 0 ) => return Ok ( buf. len ( ) - start_len) ,
467468 Ok ( n) => {
469+ buf. try_reserve ( n) . map_err ( |_| ErrorKind :: OutOfMemory ) ?;
468470 buf. extend_from_slice ( & probe[ ..n] ) ;
469471 break ;
470472 }
You can’t perform that action at this time.
0 commit comments