File tree Expand file tree Collapse file tree 1 file changed +14
-10
lines changed Expand file tree Collapse file tree 1 file changed +14
-10
lines changed Original file line number Diff line number Diff line change @@ -474,18 +474,28 @@ pub(crate) fn default_read_to_end<R: Read + ?Sized>(
474474 }
475475
476476 let mut cursor = read_buf. unfilled ( ) ;
477- loop {
477+ let result = loop {
478478 match r. read_buf ( cursor. reborrow ( ) ) {
479- Ok ( ( ) ) => break ,
480479 Err ( e) if e. is_interrupted ( ) => continue ,
481- Err ( e) => return Err ( e) ,
480+ // Do not stop now in case of error: we might have received both data
481+ // and an error
482+ res => break res,
482483 }
483- }
484+ } ;
484485
485486 let unfilled_but_initialized = cursor. init_ref ( ) . len ( ) ;
486487 let bytes_read = cursor. written ( ) ;
487488 let was_fully_initialized = read_buf. init_len ( ) == buf_len;
488489
490+ // SAFETY: BorrowedBuf's invariants mean this much memory is initialized.
491+ unsafe {
492+ let new_len = bytes_read + buf. len ( ) ;
493+ buf. set_len ( new_len) ;
494+ }
495+
496+ // Now that all data is pushed to the vector, we can fail without data loss
497+ result?;
498+
489499 if bytes_read == 0 {
490500 return Ok ( buf. len ( ) - start_len) ;
491501 }
@@ -499,12 +509,6 @@ pub(crate) fn default_read_to_end<R: Read + ?Sized>(
499509 // store how much was initialized but not filled
500510 initialized = unfilled_but_initialized;
501511
502- // SAFETY: BorrowedBuf's invariants mean this much memory is initialized.
503- unsafe {
504- let new_len = bytes_read + buf. len ( ) ;
505- buf. set_len ( new_len) ;
506- }
507-
508512 // Use heuristics to determine the max read size if no initial size hint was provided
509513 if size_hint. is_none ( ) {
510514 // The reader is returning short reads but it doesn't call ensure_init().
You can’t perform that action at this time.
0 commit comments