Skip to content

fix: init from dump was loosing memory when parsing dict #24

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

Merged
merged 1 commit into from
Apr 15, 2025
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
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"main": "index.js",
"name": "libsession_util_nodejs",
"description": "Wrappers for the Session Util Library",
"version": "0.4.33",
"version": "0.4.34",
"license": "GPL-3.0",
"author": {
"name": "Oxen Project",
Expand All @@ -11,7 +11,7 @@
"scripts": {
"update_version": "sh update_version.sh",
"clean": "rimraf .cache build",
"install": "cmake-js compile --runtime=electron --runtime-version=34.2.0 --CDSUBMODULE_CHECK=OFF --CDLOCAL_MIRROR=https://oxen.rocks/deps --CDENABLE_ONIONREQ=OFF --CDWITH_TESTS=OFF",
"install": "cmake-js build --runtime=electron --runtime-version=34.2.0 --CDSUBMODULE_CHECK=OFF --CDLOCAL_MIRROR=https://oxen.rocks/deps --CDENABLE_ONIONREQ=OFF --CDWITH_TESTS=OFF",
"prepare_release": "sh prepare_release.sh"
},
"devDependencies": {
Expand Down
4 changes: 2 additions & 2 deletions src/base_config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ Napi::Value ConfigBaseImpl::merge(const Napi::CallbackInfo& info) {
assertIsArray(info[0]);
Napi::Array asArray = info[0].As<Napi::Array>();

std::vector<std::pair<std::string, std::span<const unsigned char>>> conf_strs;
std::vector<std::pair<std::string, std::vector<unsigned char>>> conf_strs;
conf_strs.reserve(asArray.Length());

for (uint32_t i = 0; i < asArray.Length(); i++) {
Expand All @@ -73,7 +73,7 @@ Napi::Value ConfigBaseImpl::merge(const Napi::CallbackInfo& info) {
Napi::Object itemObject = item.As<Napi::Object>();
conf_strs.emplace_back(
toCppString(itemObject.Get("hash"), "base.merge"),
toCppBufferView(itemObject.Get("data"), "base.merge"));
toCppBuffer(itemObject.Get("data"), "base.merge"));
}

return get_config<ConfigBase>().merge(conf_strs);
Expand Down
6 changes: 3 additions & 3 deletions src/base_config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,12 @@ class ConfigBaseImpl {
// we should get secret key as first arg and optional dumped as second argument
assertIsUInt8Array(info[0], "base construct");
assertIsUInt8ArrayOrNull(info[1]);
std::span<const unsigned char> secretKey = toCppBufferView(info[0], class_name + ".new");
std::vector<unsigned char> secretKey = toCppBuffer(info[0], class_name + ".new");

std::optional<std::span<const unsigned char>> dump;
std::optional<std::vector<unsigned char>> dump;
auto second = info[1];
if (!second.IsEmpty() && !second.IsNull() && !second.IsUndefined())
dump = toCppBufferView(second, class_name + ".new");
dump = toCppBuffer(second, class_name + ".new");

// return std::make_shared<Config>(secretKey, dump);
std::shared_ptr<Config> config = std::make_shared<Config>(secretKey, dump);
Expand Down
17 changes: 9 additions & 8 deletions src/groups/meta_group_wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -226,8 +226,7 @@ Napi::Value MetaGroupWrapper::metaMakeDump(const Napi::CallbackInfo& info) {
// NOTE: the keys have to be in ascii-sorted order:
combined.append("info", session::to_string(this->meta_group->info->make_dump()));
combined.append("keys", session::to_string(this->meta_group->keys->make_dump()));
combined.append(
"members", session::to_string(this->meta_group->members->make_dump()));
combined.append("members", session::to_string(this->meta_group->members->make_dump()));
auto to_dump = std::move(combined).str();

return session::to_vector(to_dump);
Expand Down Expand Up @@ -328,7 +327,7 @@ Napi::Value MetaGroupWrapper::metaMerge(const Napi::CallbackInfo& info) {
assertIsArray(groupInfo);
auto asArr = groupInfo.As<Napi::Array>();

std::vector<std::pair<std::string, std::span<const unsigned char>>> conf_strs;
std::vector<std::pair<std::string, std::vector<unsigned char>>> conf_strs;
conf_strs.reserve(asArr.Length());

for (uint32_t i = 0; i < asArr.Length(); i++) {
Expand All @@ -342,7 +341,7 @@ Napi::Value MetaGroupWrapper::metaMerge(const Napi::CallbackInfo& info) {
assertIsUInt8Array(itemObject.Get("data"), "groupInfo merge");
conf_strs.emplace_back(
toCppString(itemObject.Get("hash"), "meta.merge"),
toCppBufferView(itemObject.Get("data"), "meta.merge"));
toCppBuffer(itemObject.Get("data"), "meta.merge"));
}

if (conf_strs.size()) {
Expand All @@ -355,7 +354,7 @@ Napi::Value MetaGroupWrapper::metaMerge(const Napi::CallbackInfo& info) {
assertIsArray(groupMember);
auto asArr = groupMember.As<Napi::Array>();

std::vector<std::pair<std::string, std::span<const unsigned char>>> conf_strs;
std::vector<std::pair<std::string, std::vector<unsigned char>>> conf_strs;
conf_strs.reserve(asArr.Length());

for (uint32_t i = 0; i < asArr.Length(); i++) {
Expand All @@ -369,7 +368,7 @@ Napi::Value MetaGroupWrapper::metaMerge(const Napi::CallbackInfo& info) {
assertIsUInt8Array(itemObject.Get("data"), "groupMember merge");
conf_strs.emplace_back(
toCppString(itemObject.Get("hash"), "meta.merge"),
toCppBufferView(itemObject.Get("data"), "meta.merge"));
toCppBuffer(itemObject.Get("data"), "meta.merge"));
}

if (conf_strs.size()) {
Expand Down Expand Up @@ -822,7 +821,8 @@ Napi::Value MetaGroupWrapper::makeSwarmSubAccount(const Napi::CallbackInfo& info
assertIsString(info[0]);

auto memberPk = toCppString(info[0], "makeSwarmSubAccount");
std::vector<unsigned char> subaccount = this->meta_group->keys->swarm_make_subaccount(memberPk);
std::vector<unsigned char> subaccount =
this->meta_group->keys->swarm_make_subaccount(memberPk);

session::nodeapi::checkOrThrow(
subaccount.size() == 100, "expected subaccount to be 100 bytes long");
Expand All @@ -837,7 +837,8 @@ Napi::Value MetaGroupWrapper::swarmSubAccountToken(const Napi::CallbackInfo& inf
assertIsString(info[0]);

auto memberPk = toCppString(info[0], "swarmSubAccountToken");
std::vector<unsigned char> subaccount = this->meta_group->keys->swarm_subaccount_token(memberPk);
std::vector<unsigned char> subaccount =
this->meta_group->keys->swarm_subaccount_token(memberPk);

session::nodeapi::checkOrThrow(
subaccount.size() == 36, "expected subaccount token to be 36 bytes long");
Expand Down
28 changes: 18 additions & 10 deletions src/meta/meta_base_wrapper.hpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
#pragma once

#include <napi.h>
#include <vector>

#include <span>
#include <vector>

#include "../base_config.hpp"
#include "../groups/meta_group.hpp"
Expand Down Expand Up @@ -65,9 +66,9 @@ class MetaBaseWrapper {
std::optional<std::vector<unsigned char>> dumped_meta = maybeNonemptyBuffer(
obj.Get("metaDumped"), class_name + ":constructGroupWrapper.metaDumped");

std::optional<std::span<const unsigned char>> dumped_info;
std::optional<std::span<const unsigned char>> dumped_members;
std::optional<std::span<const unsigned char>> dumped_keys;
std::optional<std::string> dumped_info;
std::optional<std::string> dumped_members;
std::optional<std::string> dumped_keys;

if (dumped_meta) {
auto dumped_meta_str = to_string(*dumped_meta);
Expand All @@ -76,30 +77,37 @@ class MetaBaseWrapper {
// NB: must read in ascii-sorted order:
if (!combined.skip_until("info"))
throw std::runtime_error{"info dump not found in combined dump!"};
dumped_info = session::to_span(combined.consume_string_view());
dumped_info = combined.consume_string();

if (!combined.skip_until("keys"))
throw std::runtime_error{"keys dump not found in combined dump!"};
dumped_keys = session::to_span(combined.consume_string_view());
dumped_keys = combined.consume_string();

if (!combined.skip_until("members"))
throw std::runtime_error{"members dump not found in combined dump!"};
dumped_members = session::to_span(combined.consume_string_view());
dumped_members = combined.consume_string();
}

// Note, we keep shared_ptr for those as the Keys one need a reference to Members and
// Info on its own currently.
auto info = std::make_shared<config::groups::Info>(
group_ed25519_pubkey, group_ed25519_secretkey, dumped_info);
group_ed25519_pubkey,
group_ed25519_secretkey,
(dumped_info ? std::make_optional(session::to_span(*dumped_info))
: std::nullopt));

auto members = std::make_shared<config::groups::Members>(
group_ed25519_pubkey, group_ed25519_secretkey, dumped_members);
group_ed25519_pubkey,
group_ed25519_secretkey,
(dumped_members ? std::make_optional(session::to_span(*dumped_members))
: std::nullopt));

auto keys = std::make_shared<config::groups::Keys>(
user_ed25519_secretkey,
group_ed25519_pubkey,
group_ed25519_secretkey,
dumped_keys,
(dumped_keys ? std::make_optional(session::to_span(*dumped_keys))
: std::nullopt),
*info,
*members);

Expand Down
3 changes: 2 additions & 1 deletion src/utilities.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ auto getStringArgs(const Napi::CallbackInfo& info) {

std::string toCppString(Napi::Value x, const std::string& identifier);
std::vector<unsigned char> toCppBuffer(Napi::Value x, const std::string& identifier);
std::span<const unsigned char> toCppBufferView(Napi::Value x, const std::string& identifier);


int64_t toCppInteger(Napi::Value x, const std::string& identifier, bool allowUndefined = false);
std::optional<int64_t> maybeNonemptyInt(Napi::Value x, const std::string& identifier);
std::optional<bool> maybeNonemptyBoolean(Napi::Value x, const std::string& identifier);
Expand Down