Skip to content

Commit 6fb7ae5

Browse files
author
Eric Fu
authored
fix: remove unsafe code in auto/manual heap dump (#12596)
1 parent ae3a59d commit 6fb7ae5

File tree

2 files changed

+4
-6
lines changed

2 files changed

+4
-6
lines changed

src/compute/src/memory_management/policy.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,9 +122,9 @@ impl JemallocMemoryControl {
122122
.unwrap()
123123
.to_string();
124124

125+
// `file_path_str` is leaked because `jemalloc_dump_mib.write` requires static lifetime
125126
let file_path_str = Box::leak(file_path.into_boxed_str());
126-
let file_path_bytes = unsafe { file_path_str.as_bytes_mut() };
127-
let file_path_ptr = file_path_bytes.as_mut_ptr();
127+
let file_path_bytes = file_path_str.as_bytes();
128128
if let Err(e) = self
129129
.jemalloc_dump_mib
130130
.write(CStr::from_bytes_with_nul(file_path_bytes).unwrap())
@@ -133,7 +133,6 @@ impl JemallocMemoryControl {
133133
} else {
134134
tracing::info!("Successfully dumped heap profile to {}", file_name);
135135
}
136-
let _ = unsafe { Box::from_raw(file_path_ptr) };
137136
}
138137
}
139138
}

src/compute/src/rpc/service/monitor_service.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -154,9 +154,9 @@ impl MonitorService for MonitorServiceImpl {
154154
.to_str()
155155
.ok_or_else(|| Status::internal("The file dir is not a UTF-8 String"))?;
156156

157+
// `file_path_str` is leaked because `prof::dump::write` requires static lifetime
157158
let file_path_str = Box::leak(file_path.to_string().into_boxed_str());
158-
let file_path_bytes = unsafe { file_path_str.as_bytes_mut() };
159-
let file_path_ptr = file_path_bytes.as_mut_ptr();
159+
let file_path_bytes = file_path_str.as_bytes();
160160
let response = if let Err(e) = tikv_jemalloc_ctl::prof::dump::write(
161161
CStr::from_bytes_with_nul(file_path_bytes).unwrap(),
162162
) {
@@ -165,7 +165,6 @@ impl MonitorService for MonitorServiceImpl {
165165
} else {
166166
Ok(Response::new(HeapProfilingResponse {}))
167167
};
168-
let _ = unsafe { Box::from_raw(file_path_ptr) };
169168
response
170169
}
171170

0 commit comments

Comments
 (0)