Skip to content

Commit

Permalink
src: fix uv_err_name memory leak
Browse files Browse the repository at this point in the history
  • Loading branch information
theanarkh committed Aug 27, 2022
1 parent ab89024 commit 79a6473
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -475,4 +475,13 @@ void SetConstructorFunction(Local<v8::Context> context,
that->Set(context, name, tmpl->GetFunction(context).ToLocalChecked()).Check();
}

void freeUVErrorNameMemoryIfNeed(const char* name, int code) {
// See uv__unknown_err_code in uv-common.c
std::string buf("Unknown system error ");
buf.append(std::to_string(code));
if (strcmp(name, buf.c_str()) == 0) {
free(reinterpret_cast<void*>(const_cast<char*>(name)));
}
}

} // namespace node
2 changes: 2 additions & 0 deletions src/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -923,6 +923,8 @@ void SetConstructorFunction(v8::Local<v8::Context> context,
SetConstructorFunctionFlag flag =
SetConstructorFunctionFlag::SET_CLASS_NAME);

void freeUVErrorNameMemoryIfNeed(const char* name, int code);

} // namespace node

#endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
Expand Down
2 changes: 2 additions & 0 deletions src/uv.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "node.h"
#include "node_external_reference.h"
#include "node_process-inl.h"
#include "util.h"

namespace node {

Expand Down Expand Up @@ -75,6 +76,7 @@ void ErrName(const FunctionCallbackInfo<Value>& args) {
CHECK_LT(err, 0);
const char* name = uv_err_name(err);
args.GetReturnValue().Set(OneByteString(env->isolate(), name));
freeUVErrorNameMemoryIfNeed(name, err);
}

void GetErrMap(const FunctionCallbackInfo<Value>& args) {
Expand Down
2 changes: 2 additions & 0 deletions test/parallel/test-uv-errno.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ const { internalBinding } = require('internal/test/binding');
const uv = internalBinding('uv');
const keys = Object.keys(uv);

assert.strictEqual("Unknown system error -111111", uv.errname(-111111));

keys.forEach((key) => {
if (!key.startsWith('UV_'))
return;
Expand Down

0 comments on commit 79a6473

Please sign in to comment.