From c7c8806593ccc269a70bbbaf914f29942970aa6b Mon Sep 17 00:00:00 2001 From: theanarkh Date: Sun, 28 Aug 2022 02:40:02 +0800 Subject: [PATCH] src: fix uv_err_name memory leak --- src/util.cc | 9 +++++++++ src/util.h | 2 ++ src/uv.cc | 2 ++ test/parallel/test-uv-errno.js | 2 ++ 4 files changed, 15 insertions(+) diff --git a/src/util.cc b/src/util.cc index 584a6a32d6cc5b..854a8a254d37a6 100644 --- a/src/util.cc +++ b/src/util.cc @@ -475,4 +475,13 @@ void SetConstructorFunction(Local 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(const_cast(name))); + } +} + } // namespace node diff --git a/src/util.h b/src/util.h index 3f58dd0a86d57b..60bd26c72746bc 100644 --- a/src/util.h +++ b/src/util.h @@ -923,6 +923,8 @@ void SetConstructorFunction(v8::Local context, SetConstructorFunctionFlag flag = SetConstructorFunctionFlag::SET_CLASS_NAME); +void freeUVErrorNameMemoryIfNeed(const char* name, int code); + } // namespace node #endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS diff --git a/src/uv.cc b/src/uv.cc index 81e80711df8fc5..f863f3f9818254 100644 --- a/src/uv.cc +++ b/src/uv.cc @@ -24,6 +24,7 @@ #include "node.h" #include "node_external_reference.h" #include "node_process-inl.h" +#include "util.h" namespace node { @@ -75,6 +76,7 @@ void ErrName(const FunctionCallbackInfo& 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& args) { diff --git a/test/parallel/test-uv-errno.js b/test/parallel/test-uv-errno.js index e46b365e4b69e4..f30518fb474657 100644 --- a/test/parallel/test-uv-errno.js +++ b/test/parallel/test-uv-errno.js @@ -12,6 +12,8 @@ const { internalBinding } = require('internal/test/binding'); const uv = internalBinding('uv'); const keys = Object.keys(uv); +assert.strictEqual(uv.errname(-111111), 'Unknown system error -111111'); + keys.forEach((key) => { if (!key.startsWith('UV_')) return;