From 50f62f40d73b67d7b603d597b043992839522831 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phan=20Kochen?= Date: Fri, 25 Mar 2022 13:27:04 +0100 Subject: [PATCH] fix(napi): double allocation in create_buffer --- crates/napi/src/env.rs | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/crates/napi/src/env.rs b/crates/napi/src/env.rs index 77f85e92..90df5612 100644 --- a/crates/napi/src/env.rs +++ b/crates/napi/src/env.rs @@ -236,8 +236,7 @@ impl Env { /// This API allocates a node::Buffer object. While this is still a fully-supported data structure, in most cases using a TypedArray will suffice. pub fn create_buffer(&self, length: usize) -> Result { let mut raw_value = ptr::null_mut(); - let mut data: Vec = Vec::with_capacity(length); - let mut data_ptr = data.as_mut_ptr() as *mut c_void; + let mut data_ptr = ptr::null_mut(); check_status!(unsafe { sys::napi_create_buffer(self.0, length, &mut data_ptr, &mut raw_value) })?; @@ -248,7 +247,7 @@ impl Env { value: raw_value, value_type: ValueType::Object, }), - mem::ManuallyDrop::new(data), + mem::ManuallyDrop::new(unsafe { Vec::from_raw_parts(data_ptr as *mut _, length, length) }), )) } @@ -366,8 +365,7 @@ impl Env { pub fn create_arraybuffer(&self, length: usize) -> Result { let mut raw_value = ptr::null_mut(); - let mut data: Vec = Vec::with_capacity(length as usize); - let mut data_ptr = data.as_mut_ptr() as *mut c_void; + let mut data_ptr = ptr::null_mut(); check_status!(unsafe { sys::napi_create_arraybuffer(self.0, length, &mut data_ptr, &mut raw_value) })?;