@@ -353,6 +353,15 @@ fn split_file_at_dot(file: &OsStr) -> (&OsStr, Option<&OsStr>) {
353353 }
354354}
355355
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+
356365////////////////////////////////////////////////////////////////////////////////
357366// The core iterators
358367////////////////////////////////////////////////////////////////////////////////
@@ -1507,13 +1516,7 @@ impl PathBuf {
15071516 }
15081517
15091518 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) ;
15171520
15181521 let file_stem = match self . file_stem ( ) {
15191522 None => return false ,
@@ -1541,6 +1544,11 @@ impl PathBuf {
15411544 /// Returns `false` and does nothing if [`self.file_name`] is [`None`],
15421545 /// returns `true` and updates the extension otherwise.
15431546 ///
1547+ /// # Panics
1548+ ///
1549+ /// Panics if the passed extension contains a path separator (see
1550+ /// [`is_separator`]).
1551+ ///
15441552 /// # Caveats
15451553 ///
15461554 /// The appended `extension` may contain dots and will be used in its entirety,
@@ -1582,6 +1590,8 @@ impl PathBuf {
15821590 }
15831591
15841592 fn _add_extension ( & mut self , extension : & OsStr ) -> bool {
1593+ validate_extension ( extension) ;
1594+
15851595 let file_name = match self . file_name ( ) {
15861596 None => return false ,
15871597 Some ( f) => f. as_encoded_bytes ( ) ,
0 commit comments