Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions lib/internal/fs/streams.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const {
kEmptyObject,
} = require('internal/util');
const {
parseFileMode,
validateBoolean,
validateFunction,
validateInteger,
Expand Down Expand Up @@ -211,7 +212,7 @@ function ReadStream(path, options) {
// Path will be ignored when fd is specified, so it can be falsy
this.path = toPathIfFileURL(path);
this.flags = options.flags === undefined ? 'r' : options.flags;
this.mode = options.mode === undefined ? 0o666 : options.mode;
this.mode = parseFileMode(options.mode, 'options.mode', 0o666);

validatePath(this.path);
} else {
Expand Down Expand Up @@ -367,7 +368,7 @@ function WriteStream(path, options) {
// Path will be ignored when fd is specified, so it can be falsy
this.path = toPathIfFileURL(path);
this.flags = options.flags === undefined ? 'w' : options.flags;
this.mode = options.mode === undefined ? 0o666 : options.mode;
this.mode = parseFileMode(options.mode, 'options.mode', 0o666);

validatePath(this.path);
} else {
Expand Down
3 changes: 2 additions & 1 deletion lib/internal/validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ function parseFileMode(value, name, def) {
value = NumberParseInt(value, 8);
}

validateUint32(value, name);
// The C++ bindings read the mode as a signed 32-bit integer.
validateInt32(value, name, 0);
// Coerce -0 to +0.
return value + 0;
}
Expand Down
4 changes: 2 additions & 2 deletions test/parallel/test-fs-fchmod.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@ assert.throws(() => fs.fchmod(1, '123x'), {
assert.throws(() => fs.fchmodSync(input, 0o666), errObj);
});

[-1, 2 ** 32].forEach((input) => {
[-1, 2 ** 31].forEach((input) => {
const errObj = {
code: 'ERR_OUT_OF_RANGE',
name: 'RangeError',
message: 'The value of "mode" is out of range. It must be >= 0 && <= ' +
`4294967295. Received ${input}`
`2147483647. Received ${input}`
};

assert.throws(() => fs.fchmod(1, input, () => {}), errObj);
Expand Down
4 changes: 2 additions & 2 deletions test/parallel/test-fs-lchmod.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,12 @@ assert.throws(() => fs.lchmodSync(f, '123x'), {
code: 'ERR_INVALID_ARG_VALUE'
});

[-1, 2 ** 32].forEach((input) => {
[-1, 2 ** 31].forEach((input) => {
const errObj = {
code: 'ERR_OUT_OF_RANGE',
name: 'RangeError',
message: 'The value of "mode" is out of range. It must be >= 0 && <= ' +
`4294967295. Received ${input}`
`2147483647. Received ${input}`
};

assert.rejects(promises.lchmod(f, input, () => {}), errObj).then(common.mustCall());
Expand Down
45 changes: 45 additions & 0 deletions test/parallel/test-fs-mode-out-of-int32-range.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
'use strict';

// This tests that a mode above INT32_MAX is reported to the caller as a
// RangeError instead of failing a CHECK in the native binding and aborting
// the process.

const common = require('../common');
const assert = require('assert');
const fs = require('fs');

const tmpdir = require('../common/tmpdir');
tmpdir.refresh();

const file = tmpdir.resolve('file.txt');
fs.writeFileSync(file, 'x');

for (const mode of [2 ** 31, 2176057344, 2 ** 32 - 1]) {
const expected = (name) => ({
code: 'ERR_OUT_OF_RANGE',
name: 'RangeError',
message: `The value of "${name}" is out of range. It must be >= 0 && <= ` +
`2147483647. Received ${mode}`,
});
assert.throws(() => fs.openSync(file, 'w', mode), expected('mode'));
assert.throws(() => fs.open(file, 'w', mode, common.mustNotCall()),
expected('mode'));
assert.rejects(fs.promises.open(file, 'w', mode), expected('mode'))
.then(common.mustCall());
assert.throws(() => fs.chmodSync(file, mode), expected('mode'));
assert.throws(() => fs.mkdirSync(tmpdir.resolve('dir'), { mode }),
expected('options.mode'));
assert.throws(
() => fs.mkdirSync(tmpdir.resolve('a/b'), { recursive: true, mode }),
expected('options.mode'),
);
assert.throws(() => fs.createReadStream(file, { mode }),
expected('options.mode'));
assert.throws(() => fs.createWriteStream(file, { mode }),
expected('options.mode'));
}

// The largest signed 32-bit value is still accepted.
fs.closeSync(fs.openSync(file, 'w', 2 ** 31 - 1));
fs.createWriteStream(file, { mode: 0o644 })
.on('open', common.mustCall(function() { this.close(); }));
Loading