Skip to content

Commit 2804036

Browse files
committed
buffer bound handling in jemalloc stats callback to avoid unintended growth
1 parent d8be469 commit 2804036

1 file changed

Lines changed: 7 additions & 2 deletions

File tree

  • crates/aptos-admin-service/src/server

crates/aptos-admin-service/src/server/malloc.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,13 @@ unsafe extern "C" fn write_cb(buf: *mut c_void, s: *const c_char) {
1818
let out = unsafe { &mut *(buf as *mut Vec<u8>) };
1919
let stats_cstr = unsafe { CStr::from_ptr(s).to_bytes() };
2020
// We do not want any memory allocation in the callback.
21-
let len = std::cmp::min(out.capacity(), stats_cstr.len());
22-
out.extend_from_slice(&stats_cstr[0..len]);
21+
let remaining = out.capacity().saturating_sub(out.len());
22+
if remaining == 0 {
23+
return;
24+
}
25+
26+
let len = std::cmp::min(remaining, stats_cstr.len());
27+
out.extend_from_slice(&stats_cstr[..len]);
2328
}
2429

2530
fn get_jemalloc_stats_string(max_len: usize) -> anyhow::Result<String> {

0 commit comments

Comments
 (0)