Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 17 additions & 7 deletions library/std/src/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,15 @@ fn split_file_at_dot(file: &OsStr) -> (&OsStr, Option<&OsStr>) {
}
}

/// Checks whether the string is valid as a file extension, or panics otherwise.
fn validate_extension(extension: &OsStr) {
for &b in extension.as_encoded_bytes() {
if is_sep_byte(b) {
panic!("extension cannot contain path separators: {extension:?}");
}
}
}

////////////////////////////////////////////////////////////////////////////////
// The core iterators
////////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -1507,13 +1516,7 @@ impl PathBuf {
}

fn _set_extension(&mut self, extension: &OsStr) -> bool {
for &b in extension.as_encoded_bytes() {
if b < 128 {
if is_separator(b as char) {
panic!("extension cannot contain path separators: {:?}", extension);
}
}
}
validate_extension(extension);

let file_stem = match self.file_stem() {
None => return false,
Expand Down Expand Up @@ -1541,6 +1544,11 @@ impl PathBuf {
/// Returns `false` and does nothing if [`self.file_name`] is [`None`],
/// returns `true` and updates the extension otherwise.
///
/// # Panics
///
/// Panics if the passed extension contains a path separator (see
/// [`is_separator`]).
///
/// # Caveats
///
/// The appended `extension` may contain dots and will be used in its entirety,
Expand Down Expand Up @@ -1582,6 +1590,8 @@ impl PathBuf {
}

fn _add_extension(&mut self, extension: &OsStr) -> bool {
validate_extension(extension);

let file_name = match self.file_name() {
None => return false,
Some(f) => f.as_encoded_bytes(),
Expand Down
Loading