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

Fix segfaults #1368

Merged
merged 4 commits into from
Sep 6, 2020
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
29 changes: 13 additions & 16 deletions src/backup.cc
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,10 @@ void Backup::Process() {
}

while (inited && !locked && !queue.empty()) {
Call* call = queue.front();
std::unique_ptr<Call> call(queue.front());
queue.pop();

call->callback(call->baton);
delete call;
}
}

Expand Down Expand Up @@ -92,21 +91,17 @@ void Backup::CleanQueue() {

// Clear out the queue so that this object can get GC'ed.
while (!queue.empty()) {
Call* call = queue.front();
std::unique_ptr<Call> call(queue.front());
queue.pop();

Napi::Function cb = call->baton->callback.Value();
std::unique_ptr<Baton> baton(call->baton);
Napi::Function cb = baton->callback.Value();

if (inited && !cb.IsEmpty() &&
cb.IsFunction()) {
TRY_CATCH_CALL(Value(), cb, 1, argv);
called = true;
}

// We don't call the actual callback, so we have to make sure that
// the baton gets destroyed.
delete call->baton;
delete call;
}

// When we couldn't call a callback function, emit an error on the
Expand All @@ -119,13 +114,12 @@ void Backup::CleanQueue() {
else while (!queue.empty()) {
// Just delete all items in the queue; we already fired an event when
// initializing the backup failed.
Call* call = queue.front();
std::unique_ptr<Call> call(queue.front());
queue.pop();

// We don't call the actual callback, so we have to make sure that
// the baton gets destroyed.
delete call->baton;
delete call;
}
}

Expand Down Expand Up @@ -226,13 +220,14 @@ void Backup::Work_Initialize(napi_env e, void* data) {
}

void Backup::Work_AfterInitialize(napi_env e, napi_status status, void* data) {
BACKUP_INIT(InitializeBaton);
std::unique_ptr<InitializeBaton> baton(static_cast<InitializeBaton*>(data));
Backup* backup = baton->backup;

Napi::Env env = backup->Env();
Napi::HandleScope scope(env);

if (backup->status != SQLITE_OK) {
Error(baton);
Error(baton.get());
backup->FinishAll();
}
else {
Expand Down Expand Up @@ -288,7 +283,8 @@ void Backup::Work_Step(napi_env e, void* data) {
}

void Backup::Work_AfterStep(napi_env e, napi_status status, void* data) {
BACKUP_INIT(StepBaton);
std::unique_ptr<StepBaton> baton(static_cast<StepBaton*>(data));
Backup* backup = baton->backup;

Napi::Env env = backup->Env();
Napi::HandleScope scope(env);
Expand All @@ -300,7 +296,7 @@ void Backup::Work_AfterStep(napi_env e, napi_status status, void* data) {
}

if (backup->status != SQLITE_OK && backup->status != SQLITE_DONE) {
Error(baton);
Error(baton.get());
}
else {
// Fire callbacks.
Expand Down Expand Up @@ -335,7 +331,8 @@ void Backup::Work_Finish(napi_env e, void* data) {
}

void Backup::Work_AfterFinish(napi_env e, napi_status status, void* data) {
BACKUP_INIT(Baton);
std::unique_ptr<Baton> baton(static_cast<Baton*>(data));
Backup* backup = baton->backup;

Napi::Env env = backup->Env();
Napi::HandleScope scope(env);
Expand Down
3 changes: 2 additions & 1 deletion src/backup.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ class Backup : public Napi::ObjectWrap<Backup> {
static Napi::Object Init(Napi::Env env, Napi::Object exports);

struct Baton {
napi_async_work request;
napi_async_work request = NULL;
Backup* backup;
Napi::FunctionReference callback;

Expand All @@ -107,6 +107,7 @@ class Backup : public Napi::ObjectWrap<Backup> {
callback.Reset(cb_, 1);
}
virtual ~Baton() {
if (request) napi_delete_async_work(backup->Env(), request);
backup->Unref();
callback.Reset();
}
Expand Down
Loading