File tree Expand file tree Collapse file tree 2 files changed +36
-0
lines changed Expand file tree Collapse file tree 2 files changed +36
-0
lines changed 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 ( ) ,
Original file line number Diff line number Diff line change @@ -1803,6 +1803,29 @@ fn test_windows_absolute() {
1803
1803
assert_eq ! ( absolute( r"COM1" ) . unwrap( ) . as_os_str( ) , Path :: new( r"\\.\COM1" ) . as_os_str( ) ) ;
1804
1804
}
1805
1805
1806
+ #[ test]
1807
+ #[ should_panic = "path separator" ]
1808
+ fn test_extension_path_sep ( ) {
1809
+ let mut path = PathBuf :: from ( "path/to/file" ) ;
1810
+ path. set_extension ( "d/../../../../../etc/passwd" ) ;
1811
+ }
1812
+
1813
+ #[ test]
1814
+ #[ should_panic = "path separator" ]
1815
+ #[ cfg( windows) ]
1816
+ fn test_extension_path_sep_alternate ( ) {
1817
+ let mut path = PathBuf :: from ( "path/to/file" ) ;
1818
+ path. set_extension ( "d\\ test" ) ;
1819
+ }
1820
+
1821
+ #[ test]
1822
+ #[ cfg( not( windows) ) ]
1823
+ fn test_extension_path_sep_alternate ( ) {
1824
+ let mut path = PathBuf :: from ( "path/to/file" ) ;
1825
+ path. set_extension ( "d\\ test" ) ;
1826
+ assert_eq ! ( path, Path :: new( "path/to/file.d\\ test" ) ) ;
1827
+ }
1828
+
1806
1829
#[ bench]
1807
1830
#[ cfg_attr( miri, ignore) ] // Miri isn't fast...
1808
1831
fn bench_path_cmp_fast_path_buf_sort ( b : & mut test:: Bencher ) {
You can’t perform that action at this time.
0 commit comments