File tree Expand file tree Collapse file tree 2 files changed +5
-3
lines changed Expand file tree Collapse file tree 2 files changed +5
-3
lines changed Original file line number Diff line number Diff line change @@ -772,14 +772,14 @@ impl Read for &File {
772772 // Reserves space in the buffer based on the file size when available.
773773 fn read_to_end ( & mut self , buf : & mut Vec < u8 > ) -> io:: Result < usize > {
774774 let size = buffer_capacity_required ( self ) ;
775- buf. reserve ( size. unwrap_or ( 0 ) ) ;
775+ buf. try_reserve ( size. unwrap_or ( 0 ) ) . map_err ( |_| io :: ErrorKind :: OutOfMemory ) ? ;
776776 io:: default_read_to_end ( self , buf, size)
777777 }
778778
779779 // Reserves space in the buffer based on the file size when available.
780780 fn read_to_string ( & mut self , buf : & mut String ) -> io:: Result < usize > {
781781 let size = buffer_capacity_required ( self ) ;
782- buf. reserve ( size. unwrap_or ( 0 ) ) ;
782+ buf. try_reserve ( size. unwrap_or ( 0 ) ) . map_err ( |_| io :: ErrorKind :: OutOfMemory ) ? ;
783783 io:: default_read_to_string ( self , buf, size)
784784 }
785785}
Original file line number Diff line number Diff line change @@ -418,7 +418,8 @@ pub(crate) fn default_read_to_end<R: Read + ?Sized>(
418418 let mut initialized = 0 ; // Extra initialized bytes from previous loop iteration
419419 loop {
420420 if buf. len ( ) == buf. capacity ( ) {
421- buf. reserve ( 32 ) ; // buf is full, need more space
421+ // buf is full, need more space
422+ buf. try_reserve ( 32 ) . map_err ( |_| ErrorKind :: OutOfMemory ) ?;
422423 }
423424
424425 let mut spare = buf. spare_capacity_mut ( ) ;
@@ -464,6 +465,7 @@ pub(crate) fn default_read_to_end<R: Read + ?Sized>(
464465 match r. read ( & mut probe) {
465466 Ok ( 0 ) => return Ok ( buf. len ( ) - start_len) ,
466467 Ok ( n) => {
468+ buf. try_reserve ( n) . map_err ( |_| ErrorKind :: OutOfMemory ) ?;
467469 buf. extend_from_slice ( & probe[ ..n] ) ;
468470 break ;
469471 }
You can’t perform that action at this time.
0 commit comments