Skip to content

Commit c7c8806

Browse files
committed
src: fix uv_err_name memory leak
1 parent ab89024 commit c7c8806

File tree

4 files changed

+15
-0
lines changed

4 files changed

+15
-0
lines changed

src/util.cc

+9
Original file line numberDiff line numberDiff line change
@@ -475,4 +475,13 @@ void SetConstructorFunction(Local<v8::Context> context,
475475
that->Set(context, name, tmpl->GetFunction(context).ToLocalChecked()).Check();
476476
}
477477

478+
void freeUVErrorNameMemoryIfNeed(const char* name, int code) {
479+
// See uv__unknown_err_code in uv-common.c
480+
std::string buf("Unknown system error ");
481+
buf.append(std::to_string(code));
482+
if (strcmp(name, buf.c_str()) == 0) {
483+
free(reinterpret_cast<void*>(const_cast<char*>(name)));
484+
}
485+
}
486+
478487
} // namespace node

src/util.h

+2
Original file line numberDiff line numberDiff line change
@@ -923,6 +923,8 @@ void SetConstructorFunction(v8::Local<v8::Context> context,
923923
SetConstructorFunctionFlag flag =
924924
SetConstructorFunctionFlag::SET_CLASS_NAME);
925925

926+
void freeUVErrorNameMemoryIfNeed(const char* name, int code);
927+
926928
} // namespace node
927929

928930
#endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS

src/uv.cc

+2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "node.h"
2525
#include "node_external_reference.h"
2626
#include "node_process-inl.h"
27+
#include "util.h"
2728

2829
namespace node {
2930

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

8082
void GetErrMap(const FunctionCallbackInfo<Value>& args) {

test/parallel/test-uv-errno.js

+2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ const { internalBinding } = require('internal/test/binding');
1212
const uv = internalBinding('uv');
1313
const keys = Object.keys(uv);
1414

15+
assert.strictEqual(uv.errname(-111111), 'Unknown system error -111111');
16+
1517
keys.forEach((key) => {
1618
if (!key.startsWith('UV_'))
1719
return;

0 commit comments

Comments
 (0)