@@ -353,6 +353,15 @@ fn split_file_at_dot(file: &OsStr) -> (&OsStr, Option<&OsStr>) {
353
353
}
354
354
}
355
355
356
+ /// Checks whether the string is valid as a file extension, or panics otherwise.
357
+ fn validate_extension ( extension : & OsStr ) {
358
+ for & b in extension. as_encoded_bytes ( ) {
359
+ if is_sep_byte ( b) {
360
+ panic ! ( "extension cannot contain path separators: {extension:?}" ) ;
361
+ }
362
+ }
363
+ }
364
+
356
365
////////////////////////////////////////////////////////////////////////////////
357
366
// The core iterators
358
367
////////////////////////////////////////////////////////////////////////////////
@@ -1507,13 +1516,7 @@ impl PathBuf {
1507
1516
}
1508
1517
1509
1518
fn _set_extension ( & mut self , extension : & OsStr ) -> bool {
1510
- for & b in extension. as_encoded_bytes ( ) {
1511
- if b < 128 {
1512
- if is_separator ( b as char ) {
1513
- panic ! ( "extension cannot contain path separators: {:?}" , extension) ;
1514
- }
1515
- }
1516
- }
1519
+ validate_extension ( extension) ;
1517
1520
1518
1521
let file_stem = match self . file_stem ( ) {
1519
1522
None => return false ,
@@ -1541,6 +1544,11 @@ impl PathBuf {
1541
1544
/// Returns `false` and does nothing if [`self.file_name`] is [`None`],
1542
1545
/// returns `true` and updates the extension otherwise.
1543
1546
///
1547
+ /// # Panics
1548
+ ///
1549
+ /// Panics if the passed extension contains a path separator (see
1550
+ /// [`is_separator`]).
1551
+ ///
1544
1552
/// # Caveats
1545
1553
///
1546
1554
/// The appended `extension` may contain dots and will be used in its entirety,
@@ -1582,6 +1590,8 @@ impl PathBuf {
1582
1590
}
1583
1591
1584
1592
fn _add_extension ( & mut self , extension : & OsStr ) -> bool {
1593
+ validate_extension ( extension) ;
1594
+
1585
1595
let file_name = match self . file_name ( ) {
1586
1596
None => return false ,
1587
1597
Some ( f) => f. as_encoded_bytes ( ) ,
0 commit comments