fs: improve mode validation · nodejs/node@1cdeb9f (original) (raw)

4 files changed

lines changed

Original file line number Diff line number Diff line change
@@ -35,24 +35,17 @@ function validateMode(value, name, def) {
35 35 }
36 36
37 37 if (typeof value === 'number') {
38 -if (!Number.isInteger(value)) {
39 -throw new ERR_OUT_OF_RANGE(name, 'an integer', value);
40 -} else {
41 -// 2 ** 32 === 4294967296
42 -throw new ERR_OUT_OF_RANGE(name, '>= 0 && < 4294967296', value);
43 -}
38 +validateInt32(value, name, 0, 2 ** 32 - 1);
44 39 }
45 40
46 41 if (typeof value === 'string') {
47 42 if (!octalReg.test(value)) {
48 43 throw new ERR_INVALID_ARG_VALUE(name, value, modeDesc);
49 44 }
50 -const parsed = parseInt(value, 8);
51 -return parsed;
45 +return parseInt(value, 8);
52 46 }
53 47
54 -// TODO(BridgeAR): Only return `def` in case `value == null`
55 -if (def !== undefined) {
48 +if (def !== undefined && value == null) {
56 49 return def;
57 50 }
58 51
Original file line number Diff line number Diff line change
@@ -46,8 +46,8 @@ const fs = require('fs');
46 46 const errObj = {
47 47 code: 'ERR_OUT_OF_RANGE',
48 48 name: 'RangeError [ERR_OUT_OF_RANGE]',
49 -message: 'The value of "mode" is out of range. It must be >= 0 && < ' +
50 -`4294967296. Received ${input}`
49 +message: 'The value of "mode" is out of range. It must be >= 0 && <= ' +
50 +`4294967295. Received ${input}`
51 51 };
52 52
53 53 assert.throws(() => fs.fchmod(1, input), errObj);
Original file line number Diff line number Diff line change
@@ -46,22 +46,18 @@ assert.throws(() => fs.lchmod(f, {}), { code: 'ERR_INVALID_CALLBACK' });
46 46 `octal string. Received ${util.inspect(input)}`
47 47 };
48 48
49 -promises.lchmod(f, input, () => {})
50 -.then(common.mustNotCall())
51 -.catch(common.expectsError(errObj));
49 +assert.rejects(promises.lchmod(f, input, () => {}), errObj);
52 50 assert.throws(() => fs.lchmodSync(f, input), errObj);
53 51 });
54 52
55 53 [-1, 2 ** 32].forEach((input) => {
56 54 const errObj = {
57 55 code: 'ERR_OUT_OF_RANGE',
58 56 name: 'RangeError [ERR_OUT_OF_RANGE]',
59 -message: 'The value of "mode" is out of range. It must be >= 0 && < ' +
60 -`4294967296. Received ${input}`
57 +message: 'The value of "mode" is out of range. It must be >= 0 && <= ' +
58 +`4294967295. Received ${input}`
61 59 };
62 60
63 -promises.lchmod(f, input, () => {})
64 -.then(common.mustNotCall())
65 -.catch(common.expectsError(errObj));
61 +assert.rejects(promises.lchmod(f, input, () => {}), errObj);
66 62 assert.throws(() => fs.lchmodSync(f, input), errObj);
67 63 });
Original file line number Diff line number Diff line change
@@ -98,15 +98,36 @@ for (const extra of [[], ['r'], ['r', 0], ['r', 0, 'bad callback']]) {
98 98 type: TypeError
99 99 }
100 100 );
101 -fs.promises.open(i, 'r')
102 -.then(common.mustNotCall())
103 -.catch(common.mustCall((err) => {
104 -common.expectsError(
105 -() => { throw err; },
106 -{
107 -code: 'ERR_INVALID_ARG_TYPE',
108 -type: TypeError
109 -}
110 -);
111 -}));
101 +assert.rejects(
102 +fs.promises.open(i, 'r'),
103 +{
104 +code: 'ERR_INVALID_ARG_TYPE',
105 +name: 'TypeError [ERR_INVALID_ARG_TYPE]'
106 +}
107 +);
108 +});
109 +
110 +// Check invalid modes.
111 +[false, [], {}].forEach((mode) => {
112 +assert.throws(
113 +() => fs.open(__filename, 'r', mode, common.mustNotCall()),
114 +{
115 +message: /'mode' must be a 32-bit/,
116 +code: 'ERR_INVALID_ARG_VALUE'
117 +}
118 +);
119 +assert.throws(
120 +() => fs.openSync(__filename, 'r', mode, common.mustNotCall()),
121 +{
122 +message: /'mode' must be a 32-bit/,
123 +code: 'ERR_INVALID_ARG_VALUE'
124 +}
125 +);
126 +assert.rejects(
127 +fs.promises.open(__filename, 'r', mode),
128 +{
129 +message: /'mode' must be a 32-bit/,
130 +code: 'ERR_INVALID_ARG_VALUE'
131 +}
132 +);
112 133 });