Skip to content

Commit 20f15f4

Browse files
fixed memory leaks in PathBuf::leak & OsString::leak tests
1 parent 85aa4b6 commit 20f15f4

File tree

2 files changed

+6
-1
lines changed

2 files changed

+6
-1
lines changed

std/src/ffi/os_str/tests.rs

+2
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,10 @@ fn test_os_string_clear() {
2626
#[test]
2727
fn test_os_string_leak() {
2828
let os_string = OsString::from("have a cake");
29+
let (len, cap) = (os_string.len(), os_string.capacity());
2930
let leaked = os_string.leak();
3031
assert_eq!(leaked.as_encoded_bytes(), b"have a cake");
32+
unsafe { drop(String::from_raw_parts(leaked as *mut OsStr as _, len, cap)) }
3133
}
3234

3335
#[test]

std/src/path/tests.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -128,9 +128,12 @@ fn into() {
128128

129129
#[test]
130130
fn test_pathbuf_leak() {
131-
let buf = PathBuf::from("/have/a/cake".to_owned());
131+
let string = "/have/a/cake".to_owned();
132+
let (len, cap) = (string.len(), string.capacity());
133+
let buf = PathBuf::from(string);
132134
let leaked = buf.leak();
133135
assert_eq!(leaked.as_os_str().as_encoded_bytes(), b"/have/a/cake");
136+
unsafe { drop(String::from_raw_parts(leaked.as_mut_os_str() as *mut OsStr as _, len, cap)) }
134137
}
135138

136139
#[test]

0 commit comments

Comments
 (0)