Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

src: plug memory leaks #2352

Merged
merged 4 commits into from
Aug 13, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/js_stream.cc
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ int JSStream::DoWrite(WriteWrap* w,
Local<Array> bufs_arr = Array::New(env()->isolate(), count);
Local<Object> buf;
for (size_t i = 0; i < count; i++) {
buf = Buffer::New(env(), bufs[i].base, bufs[i].len).ToLocalChecked();
buf = Buffer::Copy(env(), bufs[i].base, bufs[i].len).ToLocalChecked();
bufs_arr->Set(i, buf);
}

Expand Down
6 changes: 3 additions & 3 deletions src/node_buffer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -281,13 +281,13 @@ MaybeLocal<Object> Copy(Isolate* isolate, const char* data, size_t length) {
Environment* env = Environment::GetCurrent(isolate);
EscapableHandleScope handle_scope(env->isolate());
Local<Object> obj;
if (Buffer::New(env, data, length).ToLocal(&obj))
if (Buffer::Copy(env, data, length).ToLocal(&obj))
return handle_scope.Escape(obj);
return Local<Object>();
}


MaybeLocal<Object> New(Environment* env, const char* data, size_t length) {
MaybeLocal<Object> Copy(Environment* env, const char* data, size_t length) {
EscapableHandleScope scope(env->isolate());

// V8 currently only allows a maximum Typed Array index of max Smi.
Expand Down Expand Up @@ -371,7 +371,7 @@ MaybeLocal<Object> New(Isolate* isolate, char* data, size_t length) {
}


MaybeLocal<Object> Use(Environment* env, char* data, size_t length) {
MaybeLocal<Object> New(Environment* env, char* data, size_t length) {
EscapableHandleScope scope(env->isolate());

if (length > 0) {
Expand Down
17 changes: 0 additions & 17 deletions src/node_buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@
#include "node.h"
#include "v8.h"

#if defined(NODE_WANT_INTERNALS)
#include "env.h"
#endif // defined(NODE_WANT_INTERNALS)

namespace node {
namespace Buffer {

Expand Down Expand Up @@ -63,19 +59,6 @@ static inline bool IsWithinBounds(size_t off, size_t len, size_t max) {
return true;
}

// Internal. Not for public consumption. We can't define these
// in src/node_internals.h because of a circular dependency.
#if defined(NODE_WANT_INTERNALS)
v8::MaybeLocal<v8::Object> New(Environment* env, size_t size);
v8::MaybeLocal<v8::Object> New(Environment* env, const char* data, size_t len);
v8::MaybeLocal<v8::Object> New(Environment* env,
char* data,
size_t length,
FreeCallback callback,
void* hint);
v8::MaybeLocal<v8::Object> Use(Environment* env, char* data, size_t length);
#endif // defined(NODE_WANT_INTERNALS)

} // namespace Buffer
} // namespace node

Expand Down
30 changes: 16 additions & 14 deletions src/node_crypto.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1021,12 +1021,12 @@ int SecureContext::TicketKeyCallback(SSL* ssl,
Context::Scope context_scope(env->context());

Local<Value> argv[] = {
Buffer::New(env,
reinterpret_cast<char*>(name),
kTicketPartSize).ToLocalChecked(),
Buffer::New(env,
reinterpret_cast<char*>(iv),
kTicketPartSize).ToLocalChecked(),
Buffer::Copy(env,
reinterpret_cast<char*>(name),
kTicketPartSize).ToLocalChecked(),
Buffer::Copy(env,
reinterpret_cast<char*>(iv),
kTicketPartSize).ToLocalChecked(),
Boolean::New(env->isolate(), enc != 0)
};
Local<Value> ret = node::MakeCallback(env,
Expand Down Expand Up @@ -1219,7 +1219,7 @@ int SSLWrap<Base>::NewSessionCallback(SSL* s, SSL_SESSION* sess) {
memset(serialized, 0, size);
i2d_SSL_SESSION(sess, &serialized);

Local<Object> session = Buffer::New(
Local<Object> session = Buffer::Copy(
env,
reinterpret_cast<char*>(sess->session_id),
sess->session_id_length).ToLocalChecked();
Expand All @@ -1240,7 +1240,7 @@ void SSLWrap<Base>::OnClientHello(void* arg,
Context::Scope context_scope(env->context());

Local<Object> hello_obj = Object::New(env->isolate());
Local<Object> buff = Buffer::New(
Local<Object> buff = Buffer::Copy(
env,
reinterpret_cast<const char*>(hello.session_id()),
hello.session_size()).ToLocalChecked();
Expand Down Expand Up @@ -1703,7 +1703,7 @@ void SSLWrap<Base>::GetTLSTicket(const FunctionCallbackInfo<Value>& args) {
if (sess == nullptr || sess->tlsext_tick == nullptr)
return;

Local<Object> buff = Buffer::New(
Local<Object> buff = Buffer::Copy(
env,
reinterpret_cast<char*>(sess->tlsext_tick),
sess->tlsext_ticklen).ToLocalChecked();
Expand Down Expand Up @@ -1985,7 +1985,7 @@ int SSLWrap<Base>::TLSExtStatusCallback(SSL* s, void* arg) {
if (resp == nullptr) {
arg = Null(env->isolate());
} else {
arg = Buffer::New(
arg = Buffer::Copy(
env,
reinterpret_cast<char*>(const_cast<unsigned char*>(resp)),
len).ToLocalChecked();
Expand Down Expand Up @@ -2991,7 +2991,8 @@ bool CipherBase::GetAuthTag(char** out, unsigned int* out_len) const {
if (initialised_ || kind_ != kCipher || !auth_tag_)
return false;
*out_len = auth_tag_len_;
*out = new char[auth_tag_len_];
*out = static_cast<char*>(malloc(auth_tag_len_));
CHECK_NE(*out, nullptr);
memcpy(*out, auth_tag_, auth_tag_len_);
return true;
}
Expand Down Expand Up @@ -3122,8 +3123,9 @@ void CipherBase::Update(const FunctionCallbackInfo<Value>& args) {
"Trying to add data in unsupported state");
}

CHECK(out != nullptr || out_len == 0);
Local<Object> buf =
Buffer::New(env, reinterpret_cast<char*>(out), out_len).ToLocalChecked();
Buffer::Copy(env, reinterpret_cast<char*>(out), out_len).ToLocalChecked();
if (out)
delete[] out;

Expand Down Expand Up @@ -3198,7 +3200,7 @@ void CipherBase::Final(const FunctionCallbackInfo<Value>& args) {
}
}

Local<Object> buf = Buffer::New(
Local<Object> buf = Buffer::Copy(
env,
reinterpret_cast<char*>(out_value),
out_len).ToLocalChecked();
Expand Down Expand Up @@ -3980,7 +3982,7 @@ void PublicKeyCipher::Cipher(const FunctionCallbackInfo<Value>& args) {
}
}

Local<Object> vbuf = Buffer::New(
Local<Object> vbuf = Buffer::Copy(
env,
reinterpret_cast<char*>(out_value),
out_len).ToLocalChecked();
Expand Down
15 changes: 15 additions & 0 deletions src/node_internals.h
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,21 @@ class NodeInstanceData {
DISALLOW_COPY_AND_ASSIGN(NodeInstanceData);
};

namespace Buffer {
v8::MaybeLocal<v8::Object> Copy(Environment* env, const char* data, size_t len);
v8::MaybeLocal<v8::Object> New(Environment* env, size_t size);
// Takes ownership of |data|.
v8::MaybeLocal<v8::Object> New(Environment* env,
char* data,
size_t length,
void (*callback)(char* data, void* hint),
void* hint);
// Takes ownership of |data|. Must allocate |data| with malloc() or realloc()
// because ArrayBufferAllocator::Free() deallocates it again with free().
// Mixing operator new and free() is undefined behavior so don't do that.
v8::MaybeLocal<v8::Object> New(Environment* env, char* data, size_t length);
} // namespace Buffer

} // namespace node

#endif // SRC_NODE_INTERNALS_H_