Skip to content

Commit

Permalink
Merge pull request #7048 from nimrod-becker/backport_to_5_11
Browse files Browse the repository at this point in the history
[Backport to 5.11] fix small mem leaks that accumulate
  • Loading branch information
nimrod-becker authored Aug 22, 2022
2 parents 92069ea + 3e0a9de commit 1a18c33
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
9 changes: 8 additions & 1 deletion src/native/chunk/splitter_napi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ namespace noobaa
static Napi::Value _chunk_splitter(const Napi::CallbackInfo& info);
static Napi::Value _splitter_finish(Napi::Env env, Splitter* splitter);
static Napi::Value _splitter_result(Napi::Env env, Splitter* splitter);
static void _free_splitter(Napi::Env env, Splitter* splitter);

void
splitter_napi(Napi::Env env, Napi::Object exports)
Expand Down Expand Up @@ -111,7 +112,7 @@ _chunk_splitter(const Napi::CallbackInfo& info)
throw Napi::Error::New(info.Env(), "Invalid splitter config");
}
splitter = new Splitter(min_chunk, max_chunk, avg_chunk_bits, calc_md5, calc_sha256);
state["splitter"] = Napi::External<Splitter>::New(info.Env(), splitter);
state["splitter"] = Napi::External<Splitter>::New(info.Env(), splitter, _free_splitter);
}

auto buffers = info[1].As<Napi::Array>();
Expand Down Expand Up @@ -169,4 +170,10 @@ _splitter_result(Napi::Env env, Splitter* splitter)
return arr;
}

static void
_free_splitter(Napi::Env env, Splitter* splitter)
{
delete splitter;
}

} // namespace noobaa
13 changes: 11 additions & 2 deletions src/native/fs/fs_napi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1156,21 +1156,30 @@ struct RealPath : public FSWorker

RealPath(const Napi::CallbackInfo& info)
: FSWorker(info)
, _full_path(0)
{
_path = info[1].As<Napi::String>();
Begin(XSTR() << DVAL(_path));
}

~RealPath() {
if (_full_path) {
free(_full_path);
_full_path = 0;
}
}

virtual void Work()
{
// realpath called with resolved_path = NULL so it will malloc as needed for the result
// and we just need to make sure to capture this result and remember to free it on dtor.
_full_path = realpath(_path.c_str(), NULL);
if (_full_path == NULL) SetSyscallError();
if (!_full_path) SetSyscallError();
}

virtual void OnOK()
{
DBG1("FS::RealPath::OnOK: " << DVAL(_path) << DVAL(_full_path));

Napi::Env env = Env();
auto res = Napi::String::New(env, _full_path);
_deferred.Resolve(res);
Expand Down

0 comments on commit 1a18c33

Please sign in to comment.