@@ -1236,22 +1236,22 @@ impl<'a> IoSliceMut<'a> {
1236
1236
pub fn advance_slices ( bufs : & mut & mut [ IoSliceMut < ' a > ] , n : usize ) {
1237
1237
// Number of buffers to remove.
1238
1238
let mut remove = 0 ;
1239
- // Total length of all the to be removed buffers .
1240
- let mut accumulated_len = 0 ;
1239
+ // Remaining length before reaching n .
1240
+ let mut left = n ;
1241
1241
for buf in bufs. iter ( ) {
1242
- if accumulated_len + buf. len ( ) > n {
1243
- break ;
1244
- } else {
1245
- accumulated_len += buf. len ( ) ;
1242
+ if let Some ( remainder) = left. checked_sub ( buf. len ( ) ) {
1243
+ left = remainder;
1246
1244
remove += 1 ;
1245
+ } else {
1246
+ break ;
1247
1247
}
1248
1248
}
1249
1249
1250
1250
* bufs = & mut take ( bufs) [ remove..] ;
1251
1251
if bufs. is_empty ( ) {
1252
- assert ! ( n == accumulated_len , "advancing io slices beyond their length" ) ;
1252
+ assert ! ( left == 0 , "advancing io slices beyond their length" ) ;
1253
1253
} else {
1254
- bufs[ 0 ] . advance ( n - accumulated_len )
1254
+ bufs[ 0 ] . advance ( left ) ;
1255
1255
}
1256
1256
}
1257
1257
}
@@ -1379,22 +1379,25 @@ impl<'a> IoSlice<'a> {
1379
1379
pub fn advance_slices ( bufs : & mut & mut [ IoSlice < ' a > ] , n : usize ) {
1380
1380
// Number of buffers to remove.
1381
1381
let mut remove = 0 ;
1382
- // Total length of all the to be removed buffers.
1383
- let mut accumulated_len = 0 ;
1382
+ // Remaining length before reaching n. This prevents overflow
1383
+ // that could happen if the length of slices in `bufs` were instead
1384
+ // accumulated. Those slice may be aliased and, if they are large
1385
+ // enough, their added length may overflow a `usize`.
1386
+ let mut left = n;
1384
1387
for buf in bufs. iter ( ) {
1385
- if accumulated_len + buf. len ( ) > n {
1386
- break ;
1387
- } else {
1388
- accumulated_len += buf. len ( ) ;
1388
+ if let Some ( remainder) = left. checked_sub ( buf. len ( ) ) {
1389
+ left = remainder;
1389
1390
remove += 1 ;
1391
+ } else {
1392
+ break ;
1390
1393
}
1391
1394
}
1392
1395
1393
1396
* bufs = & mut take ( bufs) [ remove..] ;
1394
1397
if bufs. is_empty ( ) {
1395
- assert ! ( n == accumulated_len , "advancing io slices beyond their length" ) ;
1398
+ assert ! ( left == 0 , "advancing io slices beyond their length" ) ;
1396
1399
} else {
1397
- bufs[ 0 ] . advance ( n - accumulated_len )
1400
+ bufs[ 0 ] . advance ( left ) ;
1398
1401
}
1399
1402
}
1400
1403
}
0 commit comments