Skip to content

Commit

Permalink
Merge pull request #331 from dherman/safe-buffer
Browse files Browse the repository at this point in the history
`JsBuffer::new()` always produces a zeroed buffer.
  • Loading branch information
dherman authored Jul 12, 2018
2 parents 2fb8f54 + 4a3da5f commit b0b5079
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 2 deletions.
7 changes: 6 additions & 1 deletion crates/neon-runtime/src/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,16 @@ use std::os::raw::c_void;

extern "C" {

/// Mutates the `out` argument provided to refer to a newly created `node::Buffer` object.
/// Mutates the `out` argument provided to refer to a newly created and zero-filled `node::Buffer` object.
/// Returns `false` if the value couldn't be created.
#[link_name = "Neon_Buffer_New"]
pub fn new(out: &mut Local, size: u32) -> bool;

/// Mutates the `out` argument provided to refer to a newly created `node::Buffer` object.
/// Returns `false` if the value couldn't be created.
#[link_name = "Neon_Buffer_Uninitialized"]
pub fn uninitialized(out: &mut Local, size: u32) -> bool;

/// Mutates the `base_out` and `size_out` arguments to access the data of a `node::Buffer` object.
#[link_name = "Neon_Buffer_Data"]
pub fn data<'a, 'b>(base_out: &'a mut *mut c_void, size_out: &'a mut usize, obj: Local);
Expand Down
11 changes: 11 additions & 0 deletions crates/neon-runtime/src/neon.cc
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,17 @@ extern "C" bool Neon_Convert_ToObject(v8::Local<v8::Object> *out, v8::Local<v8::
}

extern "C" bool Neon_Buffer_New(v8::Local<v8::Object> *out, uint32_t size) {
Nan::MaybeLocal<v8::Object> maybe = Nan::NewBuffer(size);
if (!maybe.ToLocal(out)) {
return false;
}

void *data = node::Buffer::Data(*out);
memset(data, 0, size);
return true;
}

extern "C" bool Neon_Buffer_Uninitialized(v8::Local<v8::Object> *out, uint32_t size) {
Nan::MaybeLocal<v8::Object> maybe = Nan::NewBuffer(size);
return maybe.ToLocal(out);
}
Expand Down
1 change: 1 addition & 0 deletions crates/neon-runtime/src/neon.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ extern "C" {
void Neon_Buffer_Data(void **base_out, size_t *len_out, v8::Local<v8::Object> obj);

bool Neon_ArrayBuffer_New(v8::Local<v8::ArrayBuffer> *out, v8::Isolate *isolate, uint32_t size);
bool Neon_ArrayBuffer_Uninitialized(v8::Local<v8::ArrayBuffer> *out, v8::Isolate *isolate, uint32_t size);
void Neon_ArrayBuffer_Data(void **base_out, size_t *len_out, v8::Local<v8::ArrayBuffer> buffer);

typedef void(*Neon_ChainedScopeCallback)(void *, void *, void *, void *);
Expand Down
7 changes: 6 additions & 1 deletion src/value/binary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,16 @@ pub struct JsBuffer(raw::Local);

impl JsBuffer {

/// Constructs a new `Buffer` object.
/// Constructs a new `Buffer` object, safely zero-filled.
pub fn new<'a, C: Context<'a>>(_: &mut C, size: u32) -> JsResult<'a, JsBuffer> {
build(|out| { unsafe { neon_runtime::buffer::new(out, size) } })
}

/// Constructs a new `Buffer` object, safely zero-filled.
pub unsafe fn uninitialized<'a, C: Context<'a>>(_: &mut C, size: u32) -> JsResult<'a, JsBuffer> {
build(|out| { neon_runtime::buffer::uninitialized(out, size) })
}

}

impl Managed for JsBuffer {
Expand Down

0 comments on commit b0b5079

Please sign in to comment.