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 {
770
770
// Reserves space in the buffer based on the file size when available.
771
771
fn read_to_end ( & mut self , buf : & mut Vec < u8 > ) -> io:: Result < usize > {
772
772
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 ) ? ;
774
774
io:: default_read_to_end ( self , buf, size)
775
775
}
776
776
777
777
// Reserves space in the buffer based on the file size when available.
778
778
fn read_to_string ( & mut self , buf : & mut String ) -> io:: Result < usize > {
779
779
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 ) ? ;
781
781
io:: default_read_to_string ( self , buf, size)
782
782
}
783
783
}
Original file line number Diff line number Diff line change @@ -345,6 +345,7 @@ impl<R: ?Sized + Read> Read for BufReader<R> {
345
345
// delegate to the inner implementation.
346
346
fn read_to_end ( & mut self , buf : & mut Vec < u8 > ) -> io:: Result < usize > {
347
347
let inner_buf = self . buffer ( ) ;
348
+ buf. try_reserve ( inner_buf. len ( ) ) . map_err ( |_| io:: ErrorKind :: OutOfMemory ) ?;
348
349
buf. extend_from_slice ( inner_buf) ;
349
350
let nread = inner_buf. len ( ) ;
350
351
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>(
419
419
let mut initialized = 0 ; // Extra initialized bytes from previous loop iteration
420
420
loop {
421
421
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 ) ?;
423
424
}
424
425
425
426
let mut spare = buf. spare_capacity_mut ( ) ;
@@ -465,6 +466,7 @@ pub(crate) fn default_read_to_end<R: Read + ?Sized>(
465
466
match r. read ( & mut probe) {
466
467
Ok ( 0 ) => return Ok ( buf. len ( ) - start_len) ,
467
468
Ok ( n) => {
469
+ buf. try_reserve ( n) . map_err ( |_| ErrorKind :: OutOfMemory ) ?;
468
470
buf. extend_from_slice ( & probe[ ..n] ) ;
469
471
break ;
470
472
}
You can’t perform that action at this time.
0 commit comments