Skip to content
Open
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
14 changes: 8 additions & 6 deletions packages/nitro-sqlite-core/cpp/SQLiteOperations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,9 @@ void sqliteRemoveDb(DatabaseConnections& connections, const std::string& dbName,
connections.drop(dbName, std::filesystem::path(docPath) / dbName, connectionId, otherPath);
}

void bindStatement(sqlite3_stmt* statement, const SQLiteQueryParams& values) {
for (size_t valueIndex = 0; valueIndex < values.size(); valueIndex++) {
int sqliteIndex = valueIndex + 1;
const auto& optionalValue = values.at(valueIndex);
namespace {

void bindValue(sqlite3_stmt* statement, int sqliteIndex, const SQLiteValue& optionalValue) {
int bindStatus = SQLITE_OK;

if (!optionalValue || std::holds_alternative<NullType>(*optionalValue)) {
Expand Down Expand Up @@ -117,9 +116,12 @@ void bindStatement(sqlite3_stmt* statement, const SQLiteQueryParams& values) {
std::to_string(bindStatus) + "): " + sqlite3_errstr(bindStatus));
}
}
}

namespace {
void bindStatement(sqlite3_stmt* statement, const SQLiteQueryParams& values) {
for (size_t valueIndex = 0; valueIndex < values.size(); valueIndex++) {
bindValue(statement, valueIndex + 1, values[valueIndex]);
}
}

struct SQLiteStatementFinalizer {
void operator()(sqlite3_stmt* statement) const noexcept {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "../SQLiteMacros.hpp"
#include "../SQLiteOperations.hpp"
#include "HybridPreparedStatement.hpp"
#include "SQLiteBackgroundBindings.hpp"
#include "SQLiteConnectionOperation.hpp"
#include "SQLiteRowBatchSender.hpp"
#include <chrono>
Expand Down Expand Up @@ -43,29 +44,6 @@ HybridNitroSQLite::HybridNitroSQLite(std::string databaseDirectory, const std::o
}
}

// Copy any JS-backed ArrayBuffers on the JS thread so they can be safely
// accessed from the connection's background worker.
static std::optional<SQLiteQueryParams> copyArrayBufferParamsForBackground(const std::optional<SQLiteQueryParams>& params) {
if (!params) {
return std::nullopt;
}

SQLiteQueryParams copiedParams;
copiedParams.reserve(params->size());

for (const auto& value : *params) {
if (value && std::holds_alternative<ArrayBuffer>(*value)) {
const auto& buffer = std::get<ArrayBuffer>(*value);
const auto copiedBuffer = ArrayBuffer::copy(buffer);
copiedParams.push_back(copiedBuffer);
} else {
copiedParams.push_back(value);
}
}

return copiedParams;
}

// Replace JS-backed ArrayBuffers inside each grouped batch command with native copies, in place.
static void copyArrayBuffersForBackground(std::vector<BatchQuery>& commands) {
for (auto& command : commands) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,32 +1,9 @@
#include "HybridPreparedStatement.hpp"
#include "SQLiteBackgroundBindings.hpp"
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
#include "SQLiteConnectionOperation.hpp"
#include <NitroModules/ArrayBuffer.hpp>

namespace margelo::nitro::nitrosqlite {

namespace {

std::optional<SQLiteQueryParams> copyArrayBufferParamsForBackground(const std::optional<SQLiteQueryParams>& params) {
if (!params) {
return std::nullopt;
}

SQLiteQueryParams copiedParams;
copiedParams.reserve(params->size());

for (const auto& value : *params) {
if (value && std::holds_alternative<ArrayBuffer>(*value)) {
copiedParams.push_back(ArrayBuffer::copy(std::get<ArrayBuffer>(*value)));
} else {
copiedParams.push_back(value);
}
}

return copiedParams;
}

} // namespace

HybridPreparedStatement::HybridPreparedStatement(std::shared_ptr<SQLitePreparedStatement> statement) : _statement(std::move(statement)) {}

NativeQueryResult HybridPreparedStatement::execute(const std::optional<SQLiteQueryParams>& params) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#pragma once

#include "../SQLiteTypes.hpp"

namespace margelo::nitro::nitrosqlite {

// Copy any JS-backed ArrayBuffers on the JS thread so they can be safely
// accessed from the connection's background worker.
inline std::optional<SQLiteQueryParams> copyArrayBufferParamsForBackground(const std::optional<SQLiteQueryParams>& params) {

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apex: Skipping this call still compiles, and so does capturing params instead of copiedParams in the worker lambda. Either mistake makes the worker read JS-owned buffer memory after it may be freed. #539 adds more callers. Consider doing the snapshot inside enqueueConnectionOperation, or giving copied params their own type so raw params can't be passed by mistake.

if (!params) {
return std::nullopt;
}

SQLiteQueryParams copiedParams;
copiedParams.reserve(params->size());

for (const auto& value : *params) {
if (value && std::holds_alternative<ArrayBuffer>(*value)) {
const auto& buffer = std::get<ArrayBuffer>(*value);
const auto copiedBuffer = ArrayBuffer::copy(buffer);
copiedParams.push_back(copiedBuffer);
} else {
copiedParams.push_back(value);
}
}

return copiedParams;
}

} // namespace margelo::nitro::nitrosqlite