Skip to content

Commit

Permalink
Fix db::serialize() crashing with Electron (#1036)
Browse files Browse the repository at this point in the history
Fix buffer create in sandbox mode (electron)
  • Loading branch information
DamienEspitallier authored Sep 2, 2023
1 parent 653eaaa commit 0092d43
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
13 changes: 12 additions & 1 deletion src/better_sqlite3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,17 @@
//

#include "better_sqlite3.hpp"
#line 161 "./src/util/macros.lzz"
#ifndef V8_COMPRESS_POINTERS_IN_SHARED_CAGE
# define SAFE_NEW_BUFFER(env, data, length, finalizeCallback, finalizeHint) node::Buffer::New(env, data, length, finalizeCallback, finalizeHint)
#else
static inline v8::MaybeLocal<v8::Object> BufferSandboxNew(v8::Isolate* isolate, char* data, size_t length, void (*finalizeCallback)(char*, void*), void* finalizeHint) {
v8::MaybeLocal<v8::Object> buffer = node::Buffer::Copy(isolate, data, length);
finalizeCallback(data, finalizeHint);
return buffer;
}
# define SAFE_NEW_BUFFER(env, data, length, finalizeCallback, finalizeHint) BufferSandboxNew(env, data, length, finalizeCallback, finalizeHint)
#endif
#line 39 "./src/util/binder.lzz"
static bool IsPlainObject(v8::Isolate* isolate, v8::Local<v8::Object> obj) {
v8::Local<v8::Value> proto = obj->GetPrototype();
Expand Down Expand Up @@ -573,7 +584,7 @@ void Database::JS_serialize (v8::FunctionCallbackInfo <v8 :: Value> const & info
}

info.GetReturnValue().Set(
node::Buffer::New(isolate, reinterpret_cast<char*>(data), length, FreeSerialization, NULL).ToLocalChecked()
SAFE_NEW_BUFFER(isolate, reinterpret_cast<char*>(data), length, FreeSerialization, NULL).ToLocalChecked()
);
}
#line 297 "./src/objects/database.lzz"
Expand Down
2 changes: 1 addition & 1 deletion src/objects/database.lzz
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ private:
}

info.GetReturnValue().Set(
node::Buffer::New(isolate, reinterpret_cast<char*>(data), length, FreeSerialization, NULL).ToLocalChecked()
SAFE_NEW_BUFFER(isolate, reinterpret_cast<char*>(data), length, FreeSerialization, NULL).ToLocalChecked()
);
}

Expand Down
13 changes: 13 additions & 0 deletions src/util/macros.lzz
Original file line number Diff line number Diff line change
Expand Up @@ -156,3 +156,16 @@ void SetPrototypeGetter(
v8::PropertyAttribute::None
);
}

#src
#ifndef V8_COMPRESS_POINTERS_IN_SHARED_CAGE
# define SAFE_NEW_BUFFER(env, data, length, finalizeCallback, finalizeHint) node::Buffer::New(env, data, length, finalizeCallback, finalizeHint)
#else
static inline v8::MaybeLocal<v8::Object> BufferSandboxNew(v8::Isolate* isolate, char* data, size_t length, void (*finalizeCallback)(char*, void*), void* finalizeHint) {
v8::MaybeLocal<v8::Object> buffer = node::Buffer::Copy(isolate, data, length);
finalizeCallback(data, finalizeHint);
return buffer;
}
# define SAFE_NEW_BUFFER(env, data, length, finalizeCallback, finalizeHint) BufferSandboxNew(env, data, length, finalizeCallback, finalizeHint)
#endif
#end

0 comments on commit 0092d43

Please sign in to comment.