From 5a6c5209c92b643458018ce6bfd69774966cc4e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20Jos=C3=A9=20Arboleda?= Date: Thu, 25 Aug 2022 14:08:18 -0500 Subject: [PATCH] src: avoid crash using `uv.errname()` binding Return undefined from uv binding when no args are provided and do the libuv call with any arg (if provided) to the binding. Fixes: https://github.com/nodejs/node/issues/44400 --- src/uv.cc | 7 ++++--- test/parallel/test-uv-errno.js | 14 ++++++++++++++ 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/src/uv.cc b/src/uv.cc index 81e80711df8fc5..2464a9e0547827 100644 --- a/src/uv.cc +++ b/src/uv.cc @@ -70,9 +70,10 @@ void ErrName(const FunctionCallbackInfo& args) { "DEP0119").IsNothing()) return; } - int err; - if (!args[0]->Int32Value(env->context()).To(&err)) return; - CHECK_LT(err, 0); + if (args[0]->IsNullOrUndefined()) { + return args.GetReturnValue().SetUndefined(); + } + const int err = args[0].As()->Value(); const char* name = uv_err_name(err); args.GetReturnValue().Set(OneByteString(env->isolate(), name)); } diff --git a/test/parallel/test-uv-errno.js b/test/parallel/test-uv-errno.js index e46b365e4b69e4..764d5a64ba90fe 100644 --- a/test/parallel/test-uv-errno.js +++ b/test/parallel/test-uv-errno.js @@ -47,7 +47,21 @@ function runTest(fn) { `Received ${err}` }); }); + +} + +function errNameTest(fn) { + // uv.errname should not cause crash with invalid args + [0, 1, 2, NaN, {}, false].forEach((err) => { + assert.match(fn(err), /Unknown system error/); + }); + + // uv.errname should return undefined with null or undefined args + [null, undefined].forEach((err) => { + assert.strictEqual(fn(err), undefined); + }); } runTest(_errnoException); runTest(getSystemErrorName); +errNameTest(uv.errname);