Skip to content

Commit 3220bc4

Browse files
committed
smalloc: don't take address of stack var
1 parent 831de7c commit 3220bc4

1 file changed

Lines changed: 23 additions & 19 deletions

File tree

src/smalloc.cc

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -53,18 +53,18 @@ using v8::kExternalUnsignedByteArray;
5353
struct CallbackInfo {
5454
void* hint;
5555
FreeCallback cb;
56-
Persistent<Object>* p_obj;
56+
Persistent<Object> p_obj;
5757
};
5858

59-
typedef v8::WeakReferenceCallbacks<Object, char>::Revivable Callback;
60-
typedef v8::WeakReferenceCallbacks<Object, void>::Revivable CallbackFree;
61-
6259
void TargetCallback(Isolate* isolate,
6360
Persistent<Object>* target,
6461
char* arg);
6562
void TargetFreeCallback(Isolate* isolate,
6663
Persistent<Object>* target,
67-
void* arg);
64+
CallbackInfo* arg);
65+
void TargetFreeCallback(Isolate* isolate,
66+
Local<Object> target,
67+
CallbackInfo* cb_info);
6868

6969
Cached<String> smalloc_sym;
7070
static bool using_alloc_cb;
@@ -165,7 +165,7 @@ void Alloc(Handle<Object> obj, char* data, size_t length) {
165165
void TargetCallback(Isolate* isolate,
166166
Persistent<Object>* target,
167167
char* data) {
168-
HandleScope handle_scope(node_isolate);
168+
HandleScope handle_scope(isolate);
169169
Local<Object> obj = Local<Object>::New(isolate, *target);
170170
int len = obj->GetIndexedPropertiesExternalArrayDataLength();
171171
if (data != NULL && len > 0) {
@@ -187,10 +187,8 @@ void AllocDispose(Handle<Object> obj) {
187187
if (using_alloc_cb && obj->Has(smalloc_sym)) {
188188
Local<External> ext = obj->Get(smalloc_sym).As<External>();
189189
CallbackInfo* cb_info = static_cast<CallbackInfo*>(ext->Value());
190-
Local<Object> obj = Local<Object>::New(node_isolate, *cb_info->p_obj);
191-
char* data = static_cast<char*>(
192-
obj->GetIndexedPropertiesExternalArrayData());
193-
TargetFreeCallback(node_isolate, cb_info->p_obj, data);
190+
Local<Object> obj = Local<Object>::New(node_isolate, cb_info->p_obj);
191+
TargetFreeCallback(node_isolate, obj, cb_info);
194192
return;
195193
}
196194

@@ -229,17 +227,16 @@ void Alloc(Handle<Object> obj,
229227
using_alloc_cb = true;
230228
}
231229

232-
Persistent<Object> p_obj(node_isolate, obj);
233230
CallbackInfo* cb_info = new CallbackInfo;
234231
cb_info->cb = fn;
235232
cb_info->hint = hint;
236-
cb_info->p_obj = &p_obj;
233+
cb_info->p_obj.Reset(node_isolate, obj);
237234

238235
node_isolate->AdjustAmountOfExternalAllocatedMemory(length +
239236
sizeof(*cb_info));
240-
p_obj.MakeWeak(static_cast<void*>(cb_info), TargetFreeCallback);
241-
p_obj.MarkIndependent();
242-
p_obj.SetWrapperClassId(ALLOC_ID);
237+
cb_info->p_obj.MakeWeak(cb_info, TargetFreeCallback);
238+
cb_info->p_obj.MarkIndependent();
239+
cb_info->p_obj.SetWrapperClassId(ALLOC_ID);
243240
obj->SetIndexedPropertiesToExternalArrayData(data,
244241
kExternalUnsignedByteArray,
245242
length);
@@ -248,14 +245,21 @@ void Alloc(Handle<Object> obj,
248245

249246
void TargetFreeCallback(Isolate* isolate,
250247
Persistent<Object>* target,
251-
void* arg) {
252-
HandleScope handle_scope(node_isolate);
248+
CallbackInfo* cb_info) {
249+
HandleScope handle_scope(isolate);
253250
Local<Object> obj = Local<Object>::New(isolate, *target);
251+
TargetFreeCallback(isolate, obj, cb_info);
252+
}
253+
254+
255+
void TargetFreeCallback(Isolate* isolate,
256+
Local<Object> obj,
257+
CallbackInfo* cb_info) {
258+
HandleScope handle_scope(isolate);
254259
char* data = static_cast<char*>(obj->GetIndexedPropertiesExternalArrayData());
255260
int len = obj->GetIndexedPropertiesExternalArrayDataLength();
256-
CallbackInfo* cb_info = static_cast<CallbackInfo*>(arg);
257261
isolate->AdjustAmountOfExternalAllocatedMemory(-(len + sizeof(*cb_info)));
258-
(*target).Dispose();
262+
cb_info->p_obj.Dispose();
259263
cb_info->cb(data, cb_info->hint);
260264
delete cb_info;
261265
}

0 commit comments

Comments
 (0)