Skip to content

Commit

Permalink
src: avoid crash using uv.errname() binding
Browse files Browse the repository at this point in the history
Return undefined from uv binding when no args are provided and do the
libuv call with any arg (if provided) to the binding.

Fixes: #44400
  • Loading branch information
juanarbol committed Aug 25, 2022
1 parent ab89024 commit 5a6c520
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/uv.cc
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,10 @@ void ErrName(const FunctionCallbackInfo<Value>& 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<v8::Int32>()->Value();
const char* name = uv_err_name(err);
args.GetReturnValue().Set(OneByteString(env->isolate(), name));
}
Expand Down
14 changes: 14 additions & 0 deletions test/parallel/test-uv-errno.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

0 comments on commit 5a6c520

Please sign in to comment.