Skip to content

Commit f194626

Browse files
cjihrigtargos
authored andcommitted
fs: validate fds as int32s
This commit updates the JS layer's validation of file descriptors to check for int32s >= 0 instead of uint32s. PR-URL: #28984 Fixes: #28980 Reviewed-By: Michaël Zasso <[email protected]> Reviewed-By: Gus Caplan <[email protected]> Reviewed-By: Ben Noordhuis <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Rich Trott <[email protected]>
1 parent 17d9495 commit f194626

File tree

3 files changed

+43
-26
lines changed

3 files changed

+43
-26
lines changed

lib/fs.js

+18-18
Original file line numberDiff line numberDiff line change
@@ -395,14 +395,14 @@ function readFileSync(path, options) {
395395
}
396396

397397
function close(fd, callback) {
398-
validateUint32(fd, 'fd');
398+
validateInt32(fd, 'fd', 0);
399399
const req = new FSReqCallback();
400400
req.oncomplete = makeCallback(callback);
401401
binding.close(fd, req);
402402
}
403403

404404
function closeSync(fd) {
405-
validateUint32(fd, 'fd');
405+
validateInt32(fd, 'fd', 0);
406406

407407
const ctx = {};
408408
binding.close(fd, undefined, ctx);
@@ -449,7 +449,7 @@ function openSync(path, flags, mode) {
449449
}
450450

451451
function read(fd, buffer, offset, length, position, callback) {
452-
validateUint32(fd, 'fd');
452+
validateInt32(fd, 'fd', 0);
453453
validateBuffer(buffer);
454454
callback = maybeCallback(callback);
455455

@@ -487,7 +487,7 @@ Object.defineProperty(read, internalUtil.customPromisifyArgs,
487487
{ value: ['bytesRead', 'buffer'], enumerable: false });
488488

489489
function readSync(fd, buffer, offset, length, position) {
490-
validateUint32(fd, 'fd');
490+
validateInt32(fd, 'fd', 0);
491491
validateBuffer(buffer);
492492

493493
offset |= 0;
@@ -524,7 +524,7 @@ function write(fd, buffer, offset, length, position, callback) {
524524
callback(err, written || 0, buffer);
525525
}
526526

527-
validateUint32(fd, 'fd');
527+
validateInt32(fd, 'fd', 0);
528528

529529
const req = new FSReqCallback();
530530
req.oncomplete = wrapper;
@@ -564,7 +564,7 @@ Object.defineProperty(write, internalUtil.customPromisifyArgs,
564564
// OR
565565
// fs.writeSync(fd, string[, position[, encoding]]);
566566
function writeSync(fd, buffer, offset, length, position) {
567-
validateUint32(fd, 'fd');
567+
validateInt32(fd, 'fd', 0);
568568
const ctx = {};
569569
let result;
570570
if (isArrayBufferView(buffer)) {
@@ -661,7 +661,7 @@ function ftruncate(fd, len = 0, callback) {
661661
callback = len;
662662
len = 0;
663663
}
664-
validateUint32(fd, 'fd');
664+
validateInt32(fd, 'fd', 0);
665665
validateInteger(len, 'len');
666666
len = Math.max(0, len);
667667
const req = new FSReqCallback();
@@ -670,7 +670,7 @@ function ftruncate(fd, len = 0, callback) {
670670
}
671671

672672
function ftruncateSync(fd, len = 0) {
673-
validateUint32(fd, 'fd');
673+
validateInt32(fd, 'fd', 0);
674674
validateInteger(len, 'len');
675675
len = Math.max(0, len);
676676
const ctx = {};
@@ -694,28 +694,28 @@ function rmdirSync(path) {
694694
}
695695

696696
function fdatasync(fd, callback) {
697-
validateUint32(fd, 'fd');
697+
validateInt32(fd, 'fd', 0);
698698
const req = new FSReqCallback();
699699
req.oncomplete = makeCallback(callback);
700700
binding.fdatasync(fd, req);
701701
}
702702

703703
function fdatasyncSync(fd) {
704-
validateUint32(fd, 'fd');
704+
validateInt32(fd, 'fd', 0);
705705
const ctx = {};
706706
binding.fdatasync(fd, undefined, ctx);
707707
handleErrorFromBinding(ctx);
708708
}
709709

710710
function fsync(fd, callback) {
711-
validateUint32(fd, 'fd');
711+
validateInt32(fd, 'fd', 0);
712712
const req = new FSReqCallback();
713713
req.oncomplete = makeCallback(callback);
714714
binding.fsync(fd, req);
715715
}
716716

717717
function fsyncSync(fd) {
718-
validateUint32(fd, 'fd');
718+
validateInt32(fd, 'fd', 0);
719719
const ctx = {};
720720
binding.fsync(fd, undefined, ctx);
721721
handleErrorFromBinding(ctx);
@@ -801,7 +801,7 @@ function fstat(fd, options, callback) {
801801
callback = options;
802802
options = {};
803803
}
804-
validateUint32(fd, 'fd');
804+
validateInt32(fd, 'fd', 0);
805805
const req = new FSReqCallback(options.bigint);
806806
req.oncomplete = makeStatsCallback(callback);
807807
binding.fstat(fd, options.bigint, req);
@@ -832,7 +832,7 @@ function stat(path, options, callback) {
832832
}
833833

834834
function fstatSync(fd, options = {}) {
835-
validateUint32(fd, 'fd');
835+
validateInt32(fd, 'fd', 0);
836836
const ctx = { fd };
837837
const stats = binding.fstat(fd, options.bigint, undefined, ctx);
838838
handleErrorFromBinding(ctx);
@@ -1065,7 +1065,7 @@ function lchownSync(path, uid, gid) {
10651065
}
10661066

10671067
function fchown(fd, uid, gid, callback) {
1068-
validateUint32(fd, 'fd');
1068+
validateInt32(fd, 'fd', 0);
10691069
validateUint32(uid, 'uid');
10701070
validateUint32(gid, 'gid');
10711071

@@ -1075,7 +1075,7 @@ function fchown(fd, uid, gid, callback) {
10751075
}
10761076

10771077
function fchownSync(fd, uid, gid) {
1078-
validateUint32(fd, 'fd');
1078+
validateInt32(fd, 'fd', 0);
10791079
validateUint32(uid, 'uid');
10801080
validateUint32(gid, 'gid');
10811081

@@ -1126,7 +1126,7 @@ function utimesSync(path, atime, mtime) {
11261126
}
11271127

11281128
function futimes(fd, atime, mtime, callback) {
1129-
validateUint32(fd, 'fd');
1129+
validateInt32(fd, 'fd', 0);
11301130
atime = toUnixTimestamp(atime, 'atime');
11311131
mtime = toUnixTimestamp(mtime, 'mtime');
11321132
const req = new FSReqCallback();
@@ -1135,7 +1135,7 @@ function futimes(fd, atime, mtime, callback) {
11351135
}
11361136

11371137
function futimesSync(fd, atime, mtime) {
1138-
validateUint32(fd, 'fd');
1138+
validateInt32(fd, 'fd', 0);
11391139
atime = toUnixTimestamp(atime, 'atime');
11401140
mtime = toUnixTimestamp(mtime, 'mtime');
11411141
const ctx = {};

test/parallel/test-fs-fchown.js

+24-7
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,17 @@ require('../common');
44
const assert = require('assert');
55
const fs = require('fs');
66

7-
function test(input, errObj) {
7+
function testFd(input, errObj) {
88
assert.throws(() => fs.fchown(input), errObj);
99
assert.throws(() => fs.fchownSync(input), errObj);
10-
errObj.message = errObj.message.replace('fd', 'uid');
10+
}
11+
12+
function testUid(input, errObj) {
1113
assert.throws(() => fs.fchown(1, input), errObj);
1214
assert.throws(() => fs.fchownSync(1, input), errObj);
13-
errObj.message = errObj.message.replace('uid', 'gid');
15+
}
16+
17+
function testGid(input, errObj) {
1418
assert.throws(() => fs.fchown(1, 1, input), errObj);
1519
assert.throws(() => fs.fchownSync(1, 1, input), errObj);
1620
}
@@ -22,7 +26,11 @@ function test(input, errObj) {
2226
message: 'The "fd" argument must be of type number. Received type ' +
2327
typeof input
2428
};
25-
test(input, errObj);
29+
testFd(input, errObj);
30+
errObj.message = errObj.message.replace('fd', 'uid');
31+
testUid(input, errObj);
32+
errObj.message = errObj.message.replace('uid', 'gid');
33+
testGid(input, errObj);
2634
});
2735

2836
[Infinity, NaN].forEach((input) => {
@@ -32,15 +40,24 @@ function test(input, errObj) {
3240
message: 'The value of "fd" is out of range. It must be an integer. ' +
3341
`Received ${input}`
3442
};
35-
test(input, errObj);
43+
testFd(input, errObj);
44+
errObj.message = errObj.message.replace('fd', 'uid');
45+
testUid(input, errObj);
46+
errObj.message = errObj.message.replace('uid', 'gid');
47+
testGid(input, errObj);
3648
});
3749

3850
[-1, 2 ** 32].forEach((input) => {
3951
const errObj = {
4052
code: 'ERR_OUT_OF_RANGE',
4153
name: 'RangeError',
4254
message: 'The value of "fd" is out of range. It must be ' +
43-
`>= 0 && < 4294967296. Received ${input}`
55+
`>= 0 && <= 2147483647. Received ${input}`
4456
};
45-
test(input, errObj);
57+
testFd(input, errObj);
58+
errObj.message = 'The value of "uid" is out of range. It must be >= 0 && ' +
59+
`< 4294967296. Received ${input}`;
60+
testUid(input, errObj);
61+
errObj.message = errObj.message.replace('uid', 'gid');
62+
testGid(input, errObj);
4663
});

test/parallel/test-fs-utimes.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ const expectRangeError = {
210210
code: 'ERR_OUT_OF_RANGE',
211211
type: RangeError,
212212
message: 'The value of "fd" is out of range. ' +
213-
'It must be >= 0 && < 4294967296. Received -1'
213+
'It must be >= 0 && <= 2147483647. Received -1'
214214
};
215215
// futimes-only error cases
216216
{

0 commit comments

Comments
 (0)