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);