Skip to content

Commit

Permalink
fixed memory leaks in PathBuf::leak & OsString::leak tests
Browse files Browse the repository at this point in the history
  • Loading branch information
its-the-shrimp committed Jun 6, 2024
1 parent fd5777c commit 2bdc53b
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 1 deletion.
2 changes: 2 additions & 0 deletions library/std/src/ffi/os_str/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@ fn test_os_string_clear() {
#[test]
fn test_os_string_leak() {
let os_string = OsString::from("have a cake");
let (len, cap) = (os_string.len(), os_string.capacity());
let leaked = os_string.leak();
assert_eq!(leaked.as_encoded_bytes(), b"have a cake");
unsafe { drop(String::from_raw_parts(leaked as *mut OsStr as _, len, cap)) }
}

#[test]
Expand Down
5 changes: 4 additions & 1 deletion library/std/src/path/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,12 @@ fn into() {

#[test]
fn test_pathbuf_leak() {
let buf = PathBuf::from("/have/a/cake".to_owned());
let string = "/have/a/cake".to_owned();
let (len, cap) = (string.len(), string.capacity());
let buf = PathBuf::from(string);
let leaked = buf.leak();
assert_eq!(leaked.as_os_str().as_encoded_bytes(), b"/have/a/cake");
unsafe { drop(String::from_raw_parts(leaked.as_mut_os_str() as *mut OsStr as _, len, cap)) }
}

#[test]
Expand Down

0 comments on commit 2bdc53b

Please sign in to comment.