Просмотр исходного кода

Have Url::to_file_path() return Err rather than fail on NUL bytes.

Simon Sapin 12 лет назад
Родитель
Сommit
5a690d5955
2 измененных файлов с 24 добавлено и 5 удалено
  1. 8 4
      src/lib.rs
  2. 16 1
      src/tests.rs

+ 8 - 4
src/lib.rs

@@ -790,10 +790,14 @@ impl RelativeSchemeData {
                         bytes.push(b'/');
                         bytes.push(b'/');
                         percent_decode_to(path_part.as_bytes(), &mut bytes);
                         percent_decode_to(path_part.as_bytes(), &mut bytes);
                     }
                     }
-                    let path = Path::new(bytes);
-                    debug_assert!(path.is_absolute(),
-                                  "to_file_path() failed to produce an absolute Path")
-                    Ok(path)
+                    match Path::new_opt(bytes) {
+                        None => Err(()),  // Path contains a NUL byte
+                        Some(path) => {
+                            debug_assert!(path.is_absolute(),
+                                          "to_file_path() failed to produce an absolute Path")
+                            Ok(path)
+                        }
+                    }
                 }
                 }
             }
             }
             _ => Err(())
             _ => Err(())

+ 16 - 1
src/tests.rs

@@ -197,6 +197,14 @@ fn unescape(input: &str) -> String {
 }
 }
 
 
 
 
+fn set_path(url: &mut Url, path: Vec<String>) {
+    match url.scheme_data {
+        RelativeSchemeData(ref mut scheme_data) => scheme_data.path = path,
+        _ => fail!()
+    }
+}
+
+
 #[test]
 #[test]
 fn file_paths() {
 fn file_paths() {
     assert_eq!(Url::from_file_path(&path::posix::Path::new("relative")), Err(()));
     assert_eq!(Url::from_file_path(&path::posix::Path::new("relative")), Err(()));
@@ -206,9 +214,16 @@ fn file_paths() {
     assert_eq!(Url::from_file_path(&path::windows::Path::new(r"\drive-relative")), Err(()));
     assert_eq!(Url::from_file_path(&path::windows::Path::new(r"\drive-relative")), Err(()));
     assert_eq!(Url::from_file_path(&path::windows::Path::new(r"\\ucn\")), Err(()));
     assert_eq!(Url::from_file_path(&path::windows::Path::new(r"\\ucn\")), Err(()));
 
 
-    let url = Url::from_file_path(&path::posix::Path::new("/foo/bar")).unwrap();
+    let mut url = Url::from_file_path(&path::posix::Path::new("/foo/bar")).unwrap();
     assert_eq!(url.host(), Some(&Domain("".to_string())));
     assert_eq!(url.host(), Some(&Domain("".to_string())));
     assert_eq!(url.path(), Some(&["foo".to_string(), "bar".to_string()]));
     assert_eq!(url.path(), Some(&["foo".to_string(), "bar".to_string()]));
+    assert!(url.to_file_path() == Ok(path::posix::Path::new("/foo/bar")));
+
+    set_path(&mut url, vec!["foo".to_string(), "ba\0r".to_string()]);
+    assert!(url.to_file_path() == Err(()));
+
+    set_path(&mut url, vec!["foo".to_string(), "ba%00r".to_string()]);
+    assert!(url.to_file_path() == Err(()));
 
 
     let url = Url::from_file_path(&path::windows::Path::new(r"C:\foo\bar")).unwrap();
     let url = Url::from_file_path(&path::windows::Path::new(r"C:\foo\bar")).unwrap();
     assert_eq!(url.host(), Some(&Domain("".to_string())));
     assert_eq!(url.host(), Some(&Domain("".to_string())));