Skip to content

Commit 99620ad

Browse files
authored
Rollup merge of rust-lang#94855 - m-ou-se:advance-slice-panic-docs, r=kennytm
Panic when advance_slices()'ing too far and update docs. This updates advance_slices() to panic when advancing too far, like advance() already does. And updates the docs to say so. See rust-lang#62726 (comment)
2 parents fd9ca0c + 1890372 commit 99620ad

File tree

2 files changed

+38
-20
lines changed

2 files changed

+38
-20
lines changed

library/std/src/io/mod.rs

+34-16
Original file line numberDiff line numberDiff line change
@@ -1084,6 +1084,10 @@ impl<'a> IoSliceMut<'a> {
10841084
/// Also see [`IoSliceMut::advance_slices`] to advance the cursors of
10851085
/// multiple buffers.
10861086
///
1087+
/// # Panics
1088+
///
1089+
/// Panics when trying to advance beyond the end of the slice.
1090+
///
10871091
/// # Examples
10881092
///
10891093
/// ```
@@ -1105,15 +1109,18 @@ impl<'a> IoSliceMut<'a> {
11051109
self.0.advance(n)
11061110
}
11071111

1108-
/// Advance the internal cursor of the slices.
1112+
/// Advance a slice of slices.
11091113
///
1110-
/// # Notes
1114+
/// Shrinks the slice to remove any `IoSliceMut`s that are fully advanced over.
1115+
/// If the cursor ends up in the middle of an `IoSliceMut`, it is modified
1116+
/// to start at that cursor.
11111117
///
1112-
/// Elements in the slice may be modified if the cursor is not advanced to
1113-
/// the end of the slice. For example if we have a slice of buffers with 2
1114-
/// `IoSliceMut`s, both of length 8, and we advance the cursor by 10 bytes
1115-
/// the first `IoSliceMut` will be untouched however the second will be
1116-
/// modified to remove the first 2 bytes (10 - 8).
1118+
/// For example, if we have a slice of two 8-byte `IoSliceMut`s, and we advance by 10 bytes,
1119+
/// the result will only include the second `IoSliceMut`, advanced by 2 bytes.
1120+
///
1121+
/// # Panics
1122+
///
1123+
/// Panics when trying to advance beyond the end of the slices.
11171124
///
11181125
/// # Examples
11191126
///
@@ -1154,7 +1161,9 @@ impl<'a> IoSliceMut<'a> {
11541161
}
11551162

11561163
*bufs = &mut replace(bufs, &mut [])[remove..];
1157-
if !bufs.is_empty() {
1164+
if bufs.is_empty() {
1165+
assert!(n == accumulated_len, "advancing io slices beyond their length");
1166+
} else {
11581167
bufs[0].advance(n - accumulated_len)
11591168
}
11601169
}
@@ -1219,6 +1228,10 @@ impl<'a> IoSlice<'a> {
12191228
/// Also see [`IoSlice::advance_slices`] to advance the cursors of multiple
12201229
/// buffers.
12211230
///
1231+
/// # Panics
1232+
///
1233+
/// Panics when trying to advance beyond the end of the slice.
1234+
///
12221235
/// # Examples
12231236
///
12241237
/// ```
@@ -1240,15 +1253,18 @@ impl<'a> IoSlice<'a> {
12401253
self.0.advance(n)
12411254
}
12421255

1243-
/// Advance the internal cursor of the slices.
1256+
/// Advance a slice of slices.
12441257
///
1245-
/// # Notes
1258+
/// Shrinks the slice to remove any `IoSlice`s that are fully advanced over.
1259+
/// If the cursor ends up in the middle of an `IoSlice`, it is modified
1260+
/// to start at that cursor.
12461261
///
1247-
/// Elements in the slice may be modified if the cursor is not advanced to
1248-
/// the end of the slice. For example if we have a slice of buffers with 2
1249-
/// `IoSlice`s, both of length 8, and we advance the cursor by 10 bytes the
1250-
/// first `IoSlice` will be untouched however the second will be modified to
1251-
/// remove the first 2 bytes (10 - 8).
1262+
/// For example, if we have a slice of two 8-byte `IoSlice`s, and we advance by 10 bytes,
1263+
/// the result will only include the second `IoSlice`, advanced by 2 bytes.
1264+
///
1265+
/// # Panics
1266+
///
1267+
/// Panics when trying to advance beyond the end of the slices.
12521268
///
12531269
/// # Examples
12541270
///
@@ -1288,7 +1304,9 @@ impl<'a> IoSlice<'a> {
12881304
}
12891305

12901306
*bufs = &mut replace(bufs, &mut [])[remove..];
1291-
if !bufs.is_empty() {
1307+
if bufs.is_empty() {
1308+
assert!(n == accumulated_len, "advancing io slices beyond their length");
1309+
} else {
12921310
bufs[0].advance(n - accumulated_len)
12931311
}
12941312
}

library/std/src/io/tests.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -423,18 +423,18 @@ fn io_slice_mut_advance_slices() {
423423
}
424424

425425
#[test]
426+
#[should_panic]
426427
fn io_slice_mut_advance_slices_empty_slice() {
427428
let mut empty_bufs = &mut [][..];
428-
// Shouldn't panic.
429429
IoSliceMut::advance_slices(&mut empty_bufs, 1);
430430
}
431431

432432
#[test]
433+
#[should_panic]
433434
fn io_slice_mut_advance_slices_beyond_total_length() {
434435
let mut buf1 = [1; 8];
435436
let mut bufs = &mut [IoSliceMut::new(&mut buf1)][..];
436437

437-
// Going beyond the total length should be ok.
438438
IoSliceMut::advance_slices(&mut bufs, 9);
439439
assert!(bufs.is_empty());
440440
}
@@ -463,18 +463,18 @@ fn io_slice_advance_slices() {
463463
}
464464

465465
#[test]
466+
#[should_panic]
466467
fn io_slice_advance_slices_empty_slice() {
467468
let mut empty_bufs = &mut [][..];
468-
// Shouldn't panic.
469469
IoSlice::advance_slices(&mut empty_bufs, 1);
470470
}
471471

472472
#[test]
473+
#[should_panic]
473474
fn io_slice_advance_slices_beyond_total_length() {
474475
let buf1 = [1; 8];
475476
let mut bufs = &mut [IoSlice::new(&buf1)][..];
476477

477-
// Going beyond the total length should be ok.
478478
IoSlice::advance_slices(&mut bufs, 9);
479479
assert!(bufs.is_empty());
480480
}

0 commit comments

Comments
 (0)