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 {
772
772
// Reserves space in the buffer based on the file size when available.
773
773
fn read_to_end ( & mut self , buf : & mut Vec < u8 > ) -> io:: Result < usize > {
774
774
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 ) ? ;
776
776
io:: default_read_to_end ( self , buf, size)
777
777
}
778
778
779
779
// Reserves space in the buffer based on the file size when available.
780
780
fn read_to_string ( & mut self , buf : & mut String ) -> io:: Result < usize > {
781
781
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 ) ? ;
783
783
io:: default_read_to_string ( self , buf, size)
784
784
}
785
785
}
Original file line number Diff line number Diff line change @@ -418,7 +418,8 @@ pub(crate) fn default_read_to_end<R: Read + ?Sized>(
418
418
let mut initialized = 0 ; // Extra initialized bytes from previous loop iteration
419
419
loop {
420
420
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 ) ?;
422
423
}
423
424
424
425
let mut spare = buf. spare_capacity_mut ( ) ;
@@ -464,6 +465,7 @@ pub(crate) fn default_read_to_end<R: Read + ?Sized>(
464
465
match r. read ( & mut probe) {
465
466
Ok ( 0 ) => return Ok ( buf. len ( ) - start_len) ,
466
467
Ok ( n) => {
468
+ buf. try_reserve ( n) . map_err ( |_| ErrorKind :: OutOfMemory ) ?;
467
469
buf. extend_from_slice ( & probe[ ..n] ) ;
468
470
break ;
469
471
}
You can’t perform that action at this time.
0 commit comments