fs: allow passing negative zero fd · nodejs/node@c302450 · GitHub
Skip to content

Commit c302450

Browse files
RaisinTendanielleadams
authored andcommitted
fs: allow passing negative zero fd
Fixes: #37122 PR-URL: #37123 Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Zijian Liu <lxxyxzj@gmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com>
1 parent 48a634e commit c302450

3 files changed

Lines changed: 39 additions & 25 deletions

File tree

lib/fs.js

Lines changed: 25 additions & 25 deletions

lib/internal/fs/utils.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ const {
1212
NumberIsFinite,
1313
NumberIsInteger,
1414
MathMin,
15+
ObjectIs,
1516
ObjectPrototypeHasOwnProperty,
1617
ObjectSetPrototypeOf,
1718
ReflectApply,
@@ -649,6 +650,16 @@ const getValidatedPath = hideStackFrames((fileURLOrPath, propName = 'path') => {
649650
return path;
650651
});
651652

653+
const getValidatedFd = hideStackFrames((fd, propName = 'fd') => {
654+
if (ObjectIs(fd, -0)) {
655+
return 0;
656+
}
657+
658+
validateInt32(fd, propName, 0);
659+
660+
return fd;
661+
});
662+
652663
const validateBufferArray = hideStackFrames((buffers, propName = 'buffers') => {
653664
if (!ArrayIsArray(buffers))
654665
throw new ERR_INVALID_ARG_TYPE(propName, 'ArrayBufferView[]', buffers);
@@ -843,6 +854,7 @@ module.exports = {
843854
getDirent,
844855
getDirents,
845856
getOptions,
857+
getValidatedFd,
846858
getValidatedPath,
847859
getValidMode,
848860
handleErrorFromBinding,

test/parallel/test-fs-stat.js

Lines changed: 2 additions & 0 deletions

0 commit comments

Comments
 (0)