Auto merge of #125070 - tbu-:pr_set_extension_panic, r=jhpratt · model-checking/verify-rust-std@4014081 (original) (raw)

Original file line number Diff line number Diff line change
@@ -1425,6 +1425,11 @@ impl PathBuf {
1425 1425 /// If `extension` is the empty string, [`self.extension`] will be [`None`]
1426 1426 /// afterwards, not `Some("")`.
1427 1427 ///
1428 + /// # Panics
1429 + ///
1430 + /// Panics if the passed extension contains a path separator (see
1431 + /// [`is_separator`]).
1432 + ///
1428 1433 /// # Caveats
1429 1434 ///
1430 1435 /// The new `extension` may contain dots and will be used in its entirety,
@@ -1470,6 +1475,14 @@ impl PathBuf {
1470 1475 }
1471 1476
1472 1477 fn _set_extension(&mut self, extension: &OsStr) -> bool {
1478 +for &b in extension.as_encoded_bytes() {
1479 +if b < 128 {
1480 +if is_separator(b as char) {
1481 +panic!("extension cannot contain path separators: {:?}", extension);
1482 +}
1483 +}
1484 +}
1485 +
1473 1486 let file_stem = match self.file_stem() {
1474 1487 None => return false,
1475 1488 Some(f) => f.as_encoded_bytes(),