Skip to content

Commit

Permalink
Fix mismatched C free of memory allocated with C++ new (#220)
Browse files Browse the repository at this point in the history
* Fix mismatched C free of memory allocated with C++ new

* clang-format (using `go generate`)
  • Loading branch information
dylanahsmith authored Nov 2, 2021
1 parent ea7debf commit 24e3ca7
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 18 deletions.
34 changes: 19 additions & 15 deletions v8go.cc
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,9 @@ void CPUProfilerStartProfiling(CPUProfiler* profiler, const char* title) {
Isolate::Scope isolate_scope(profiler->iso);
HandleScope handle_scope(profiler->iso);

Local<String> title_str = String::NewFromUtf8(profiler->iso, title, NewStringType::kNormal).ToLocalChecked();
Local<String> title_str =
String::NewFromUtf8(profiler->iso, title, NewStringType::kNormal)
.ToLocalChecked();
profiler->ptr->StartProfiling(title_str);
}

Expand All @@ -242,13 +244,13 @@ CPUProfileNode* NewCPUProfileNode(const CpuProfileNode* ptr_) {
}

CPUProfileNode* root = new CPUProfileNode{
ptr_,
ptr_->GetScriptResourceNameStr(),
ptr_->GetFunctionNameStr(),
ptr_->GetLineNumber(),
ptr_->GetColumnNumber(),
count,
children,
ptr_,
ptr_->GetScriptResourceNameStr(),
ptr_->GetFunctionNameStr(),
ptr_->GetLineNumber(),
ptr_->GetColumnNumber(),
count,
children,
};
return root;
}
Expand All @@ -263,7 +265,8 @@ CPUProfile* CPUProfilerStopProfiling(CPUProfiler* profiler, const char* title) {
HandleScope handle_scope(profiler->iso);

Local<String> title_str =
String::NewFromUtf8(profiler->iso, title, NewStringType::kNormal).ToLocalChecked();
String::NewFromUtf8(profiler->iso, title, NewStringType::kNormal)
.ToLocalChecked();

CPUProfile* profile = new CPUProfile;
profile->ptr = profiler->ptr->StopProfiling(title_str);
Expand Down Expand Up @@ -295,7 +298,7 @@ void CPUProfileDelete(CPUProfile* profile) {
return;
}
profile->ptr->Delete();
free((void *)profile->title);
free((void*)profile->title);

CPUProfileNodeDelete(profile->root);

Expand Down Expand Up @@ -374,7 +377,8 @@ RtnValue ObjectTemplateNewInstance(TemplatePtr ptr, ContextPtr ctx) {
return rtn;
}

void ObjectTemplateSetInternalFieldCount(TemplatePtr ptr, uint32_t field_count) {
void ObjectTemplateSetInternalFieldCount(TemplatePtr ptr,
uint32_t field_count) {
LOCAL_TEMPLATE(ptr);

Local<ObjectTemplate> obj_tmpl = tmpl.As<ObjectTemplate>();
Expand Down Expand Up @@ -778,7 +782,7 @@ const uint32_t* ValueToArrayIndex(ValuePtr ptr) {
return nullptr;
}

uint32_t* idx = new uint32_t;
uint32_t* idx = (uint32_t*)malloc(sizeof(uint32_t));
*idx = array_index->Value();
return idx;
}
Expand Down Expand Up @@ -840,7 +844,7 @@ ValueBigInt ValueToBigInt(ValuePtr ptr) {

int word_count = bint->WordCount();
int sign_bit = 0;
uint64_t* words = new uint64_t[word_count];
uint64_t* words = (uint64_t*)malloc(sizeof(uint64_t) * word_count);
bint->ToWordsArray(&sign_bit, &word_count, words);
ValueBigInt rtn = {words, word_count, sign_bit};
return rtn;
Expand Down Expand Up @@ -1214,8 +1218,8 @@ ValuePtr ObjectGetInternalField(ValuePtr ptr, uint32_t idx) {
m_value* new_val = new m_value;
new_val->iso = iso;
new_val->ctx = ctx;
new_val->ptr = Persistent<Value, CopyablePersistentTraits<Value>>(
iso, result);
new_val->ptr =
Persistent<Value, CopyablePersistentTraits<Value>>(iso, result);

return tracked_value(ctx, new_val);
}
Expand Down
8 changes: 5 additions & 3 deletions v8go.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ struct _EXCEPTION_POINTERS;
#endif

#include "libplatform/libplatform.h"
#include "v8.h"
#include "v8-profiler.h"
#include "v8.h"

typedef v8::Isolate* IsolatePtr;
typedef v8::CpuProfiler* CpuProfilerPtr;
Expand Down Expand Up @@ -117,7 +117,8 @@ extern IsolateHStatistics IsolationGetHeapStatistics(IsolatePtr ptr);
extern CPUProfiler* NewCPUProfiler(IsolatePtr iso_ptr);
extern void CPUProfilerDispose(CPUProfiler* ptr);
extern void CPUProfilerStartProfiling(CPUProfiler* ptr, const char* title);
extern CPUProfile* CPUProfilerStopProfiling(CPUProfiler* ptr, const char* title);
extern CPUProfile* CPUProfilerStopProfiling(CPUProfiler* ptr,
const char* title);
extern void CPUProfileDelete(CPUProfile* ptr);

extern ContextPtr NewContext(IsolatePtr iso_ptr,
Expand All @@ -143,7 +144,8 @@ extern void TemplateSetTemplate(TemplatePtr ptr,

extern TemplatePtr NewObjectTemplate(IsolatePtr iso_ptr);
extern RtnValue ObjectTemplateNewInstance(TemplatePtr ptr, ContextPtr ctx_ptr);
extern void ObjectTemplateSetInternalFieldCount(TemplatePtr ptr, uint32_t field_count);
extern void ObjectTemplateSetInternalFieldCount(TemplatePtr ptr,
uint32_t field_count);
extern int ObjectTemplateInternalFieldCount(TemplatePtr ptr);

extern TemplatePtr NewFunctionTemplate(IsolatePtr iso_ptr, int callback_ref);
Expand Down

0 comments on commit 24e3ca7

Please sign in to comment.