Skip to content

Commit 21196a4

Browse files
committed
fs: move validateFd to c++
1 parent 2e458d9 commit 21196a4

File tree

4 files changed

+45
-41
lines changed

4 files changed

+45
-41
lines changed

lib/fs.js

+2-29
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,6 @@ const {
101101
getDirent,
102102
getDirents,
103103
getOptions,
104-
getValidatedFd,
105104
getValidatedPath,
106105
getValidMode,
107106
handleErrorFromBinding,
@@ -513,7 +512,6 @@ function defaultCloseCallback(err) {
513512
* @returns {void}
514513
*/
515514
function close(fd, callback = defaultCloseCallback) {
516-
fd = getValidatedFd(fd);
517515
if (callback !== defaultCloseCallback)
518516
callback = makeCallback(callback);
519517

@@ -528,8 +526,6 @@ function close(fd, callback = defaultCloseCallback) {
528526
* @returns {void}
529527
*/
530528
function closeSync(fd) {
531-
fd = getValidatedFd(fd);
532-
533529
binding.close(fd);
534530
}
535531

@@ -618,8 +614,6 @@ function openAsBlob(path, options = kEmptyObject) {
618614
* @returns {void}
619615
*/
620616
function read(fd, buffer, offsetOrOptions, length, position, callback) {
621-
fd = getValidatedFd(fd);
622-
623617
let offset = offsetOrOptions;
624618
let params = null;
625619
if (arguments.length <= 4) {
@@ -709,8 +703,6 @@ ObjectDefineProperty(read, kCustomPromisifyArgsSymbol,
709703
* @returns {number}
710704
*/
711705
function readSync(fd, buffer, offsetOrOptions, length, position) {
712-
fd = getValidatedFd(fd);
713-
714706
validateBuffer(buffer);
715707

716708
let offset = offsetOrOptions;
@@ -772,7 +764,6 @@ function readv(fd, buffers, position, callback) {
772764
callback(err, read || 0, buffers);
773765
}
774766

775-
fd = getValidatedFd(fd);
776767
validateBufferArray(buffers);
777768
callback = maybeCallback(callback || position);
778769

@@ -798,7 +789,6 @@ ObjectDefineProperty(readv, kCustomPromisifyArgsSymbol,
798789
* @returns {number}
799790
*/
800791
function readvSync(fd, buffers, position) {
801-
fd = getValidatedFd(fd);
802792
validateBufferArray(buffers);
803793

804794
const ctx = {};
@@ -831,8 +821,6 @@ function write(fd, buffer, offsetOrOptions, length, position, callback) {
831821
callback(err, written || 0, buffer);
832822
}
833823

834-
fd = getValidatedFd(fd);
835-
836824
let offset = offsetOrOptions;
837825
if (isArrayBufferView(buffer)) {
838826
callback = maybeCallback(callback || position || length || offset);
@@ -899,7 +887,6 @@ ObjectDefineProperty(write, kCustomPromisifyArgsSymbol,
899887
* @returns {number}
900888
*/
901889
function writeSync(fd, buffer, offsetOrOptions, length, position) {
902-
fd = getValidatedFd(fd);
903890
const ctx = {};
904891
let result;
905892

@@ -955,7 +942,6 @@ function writev(fd, buffers, position, callback) {
955942
callback(err, written || 0, buffers);
956943
}
957944

958-
fd = getValidatedFd(fd);
959945
validateBufferArray(buffers);
960946
callback = maybeCallback(callback || position);
961947

@@ -988,7 +974,6 @@ ObjectDefineProperty(writev, kCustomPromisifyArgsSymbol, {
988974
* @returns {number}
989975
*/
990976
function writevSync(fd, buffers, position) {
991-
fd = getValidatedFd(fd);
992977
validateBufferArray(buffers);
993978

994979
if (buffers.length === 0) {
@@ -1110,7 +1095,6 @@ function ftruncate(fd, len = 0, callback) {
11101095
callback = len;
11111096
len = 0;
11121097
}
1113-
fd = getValidatedFd(fd);
11141098
validateInteger(len, 'len');
11151099
len = MathMax(0, len);
11161100
callback = makeCallback(callback);
@@ -1127,7 +1111,6 @@ function ftruncate(fd, len = 0, callback) {
11271111
* @returns {void}
11281112
*/
11291113
function ftruncateSync(fd, len = 0) {
1130-
fd = getValidatedFd(fd);
11311114
validateInteger(len, 'len');
11321115
len = MathMax(0, len);
11331116
binding.ftruncate(fd, len);
@@ -1279,7 +1262,6 @@ function rmSync(path, options) {
12791262
* @returns {void}
12801263
*/
12811264
function fdatasync(fd, callback) {
1282-
fd = getValidatedFd(fd);
12831265
const req = new FSReqCallback();
12841266
req.oncomplete = makeCallback(callback);
12851267
binding.fdatasync(fd, req);
@@ -1293,7 +1275,6 @@ function fdatasync(fd, callback) {
12931275
* @returns {void}
12941276
*/
12951277
function fdatasyncSync(fd) {
1296-
fd = getValidatedFd(fd);
12971278
binding.fdatasync(fd);
12981279
}
12991280

@@ -1305,7 +1286,6 @@ function fdatasyncSync(fd) {
13051286
* @returns {void}
13061287
*/
13071288
function fsync(fd, callback) {
1308-
fd = getValidatedFd(fd);
13091289
const req = new FSReqCallback();
13101290
req.oncomplete = makeCallback(callback);
13111291
binding.fsync(fd, req);
@@ -1318,7 +1298,6 @@ function fsync(fd, callback) {
13181298
* @returns {void}
13191299
*/
13201300
function fsyncSync(fd) {
1321-
fd = getValidatedFd(fd);
13221301
binding.fsync(fd);
13231302
}
13241303

@@ -1539,7 +1518,6 @@ function fstat(fd, options = { bigint: false }, callback) {
15391518
callback = options;
15401519
options = kEmptyObject;
15411520
}
1542-
fd = getValidatedFd(fd);
15431521
callback = makeStatsCallback(callback);
15441522

15451523
const req = new FSReqCallback(options.bigint);
@@ -1622,7 +1600,6 @@ function statfs(path, options = { bigint: false }, callback) {
16221600
* @returns {Stats | undefined}
16231601
*/
16241602
function fstatSync(fd, options = { bigint: false }) {
1625-
fd = getValidatedFd(fd);
16261603
const stats = binding.fstat(fd, options.bigint, undefined, false);
16271604
if (stats === undefined) {
16281605
return;
@@ -1888,7 +1865,6 @@ function unlinkSync(path) {
18881865
* @returns {void}
18891866
*/
18901867
function fchmod(fd, mode, callback) {
1891-
fd = getValidatedFd(fd);
18921868
mode = parseFileMode(mode, 'mode');
18931869
callback = makeCallback(callback);
18941870

@@ -1905,7 +1881,7 @@ function fchmod(fd, mode, callback) {
19051881
*/
19061882
function fchmodSync(fd, mode) {
19071883
binding.fchmod(
1908-
getValidatedFd(fd),
1884+
fd,
19091885
parseFileMode(mode, 'mode'),
19101886
);
19111887
}
@@ -2033,7 +2009,6 @@ function lchownSync(path, uid, gid) {
20332009
* @returns {void}
20342010
*/
20352011
function fchown(fd, uid, gid, callback) {
2036-
fd = getValidatedFd(fd);
20372012
validateInteger(uid, 'uid', -1, kMaxUserId);
20382013
validateInteger(gid, 'gid', -1, kMaxUserId);
20392014
callback = makeCallback(callback);
@@ -2051,7 +2026,6 @@ function fchown(fd, uid, gid, callback) {
20512026
* @returns {void}
20522027
*/
20532028
function fchownSync(fd, uid, gid) {
2054-
fd = getValidatedFd(fd);
20552029
validateInteger(uid, 'uid', -1, kMaxUserId);
20562030
validateInteger(gid, 'gid', -1, kMaxUserId);
20572031

@@ -2147,7 +2121,6 @@ function utimesSync(path, atime, mtime) {
21472121
* @returns {void}
21482122
*/
21492123
function futimes(fd, atime, mtime, callback) {
2150-
fd = getValidatedFd(fd);
21512124
atime = toUnixTimestamp(atime, 'atime');
21522125
mtime = toUnixTimestamp(mtime, 'mtime');
21532126
callback = makeCallback(callback);
@@ -2168,7 +2141,7 @@ function futimes(fd, atime, mtime, callback) {
21682141
*/
21692142
function futimesSync(fd, atime, mtime) {
21702143
binding.futimes(
2171-
getValidatedFd(fd),
2144+
fd,
21722145
toUnixTimestamp(atime, 'atime'),
21732146
toUnixTimestamp(mtime, 'mtime'),
21742147
);

src/node_file.cc

+12-11
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
#include "req_wrap-inl.h"
3939
#include "stream_base-inl.h"
4040
#include "string_bytes.h"
41+
#include "util.h"
4142

4243
#include <fcntl.h>
4344
#include <sys/types.h>
@@ -1405,7 +1406,7 @@ static void FTruncate(const FunctionCallbackInfo<Value>& args) {
14051406
const int argc = args.Length();
14061407
CHECK_GE(argc, 2);
14071408

1408-
CHECK(args[0]->IsInt32());
1409+
VALIDATE_FD(env, args[0]);
14091410
const int fd = args[0].As<Int32>()->Value();
14101411

14111412
CHECK(IsSafeJsInt(args[1]));
@@ -1430,7 +1431,7 @@ static void Fdatasync(const FunctionCallbackInfo<Value>& args) {
14301431
const int argc = args.Length();
14311432
CHECK_GE(argc, 1);
14321433

1433-
CHECK(args[0]->IsInt32());
1434+
VALIDATE_FD(env, args[0]);
14341435
const int fd = args[0].As<Int32>()->Value();
14351436

14361437
if (argc > 1) { // fdatasync(fd, req)
@@ -1453,7 +1454,7 @@ static void Fsync(const FunctionCallbackInfo<Value>& args) {
14531454
const int argc = args.Length();
14541455
CHECK_GE(argc, 1);
14551456

1456-
CHECK(args[0]->IsInt32());
1457+
VALIDATE_FD(env, args[0]);
14571458
const int fd = args[0].As<Int32>()->Value();
14581459

14591460
if (argc > 1) {
@@ -2033,7 +2034,7 @@ static void WriteBuffer(const FunctionCallbackInfo<Value>& args) {
20332034
const int argc = args.Length();
20342035
CHECK_GE(argc, 4);
20352036

2036-
CHECK(args[0]->IsInt32());
2037+
VALIDATE_FD(env, args[0]);
20372038
const int fd = args[0].As<Int32>()->Value();
20382039

20392040
CHECK(Buffer::HasInstance(args[1]));
@@ -2088,7 +2089,7 @@ static void WriteBuffers(const FunctionCallbackInfo<Value>& args) {
20882089
const int argc = args.Length();
20892090
CHECK_GE(argc, 3);
20902091

2091-
CHECK(args[0]->IsInt32());
2092+
VALIDATE_FD(env, args[0]);
20922093
const int fd = args[0].As<Int32>()->Value();
20932094

20942095
CHECK(args[1]->IsArray());
@@ -2146,7 +2147,7 @@ static void WriteString(const FunctionCallbackInfo<Value>& args) {
21462147

21472148
const int argc = args.Length();
21482149
CHECK_GE(argc, 4);
2149-
CHECK(args[0]->IsInt32());
2150+
VALIDATE_FD(env, args[0]);
21502151
const int fd = args[0].As<Int32>()->Value();
21512152

21522153
const int64_t pos = GetOffset(args[2]);
@@ -2326,7 +2327,7 @@ static void Read(const FunctionCallbackInfo<Value>& args) {
23262327
const int argc = args.Length();
23272328
CHECK_GE(argc, 5);
23282329

2329-
CHECK(args[0]->IsInt32());
2330+
VALIDATE_FD(env, args[0]);
23302331
const int fd = args[0].As<Int32>()->Value();
23312332

23322333
CHECK(Buffer::HasInstance(args[1]));
@@ -2450,7 +2451,7 @@ static void ReadBuffers(const FunctionCallbackInfo<Value>& args) {
24502451
const int argc = args.Length();
24512452
CHECK_GE(argc, 3);
24522453

2453-
CHECK(args[0]->IsInt32());
2454+
VALIDATE_FD(env, args[0]);
24542455
const int fd = args[0].As<Int32>()->Value();
24552456

24562457
CHECK(args[1]->IsArray());
@@ -2525,7 +2526,7 @@ static void FChmod(const FunctionCallbackInfo<Value>& args) {
25252526
const int argc = args.Length();
25262527
CHECK_GE(argc, 2);
25272528

2528-
CHECK(args[0]->IsInt32());
2529+
VALIDATE_FD(env, args[0]);
25292530
const int fd = args[0].As<Int32>()->Value();
25302531

25312532
CHECK(args[1]->IsInt32());
@@ -2588,7 +2589,7 @@ static void FChown(const FunctionCallbackInfo<Value>& args) {
25882589
const int argc = args.Length();
25892590
CHECK_GE(argc, 3);
25902591

2591-
CHECK(args[0]->IsInt32());
2592+
VALIDATE_FD(env, args[0]);
25922593
const int fd = args[0].As<Int32>()->Value();
25932594

25942595
CHECK(IsSafeJsInt(args[1]));
@@ -2683,7 +2684,7 @@ static void FUTimes(const FunctionCallbackInfo<Value>& args) {
26832684
const int argc = args.Length();
26842685
CHECK_GE(argc, 3);
26852686

2686-
CHECK(args[0]->IsInt32());
2687+
VALIDATE_FD(env, args[0]);
26872688
const int fd = args[0].As<Int32>()->Value();
26882689

26892690
CHECK(args[1]->IsNumber());

src/util.h

+24
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,30 @@ void DumpJavaScriptBacktrace(FILE* fp);
195195
#define UNREACHABLE(...) \
196196
ERROR_AND_ABORT("Unreachable code reached" __VA_OPT__(": ") __VA_ARGS__)
197197

198+
#define VALIDATE_FD(env, input) \
199+
do { \
200+
if (!input->IsInt32()) { \
201+
Utf8Value value(env->isolate(), \
202+
input->ToString(env->context()).ToLocalChecked()); \
203+
Utf8Value type(env->isolate(), input->TypeOf(env->isolate())); \
204+
THROW_ERR_INVALID_ARG_TYPE(env, \
205+
"The \"fd\" argument must be of type " \
206+
"number. Received type %s (%s)", \
207+
type.out(), \
208+
value.out()); \
209+
return; \
210+
} \
211+
const int fd = input.As<Int32>()->Value(); \
212+
if (fd < 0 || fd > INT32_MAX) { \
213+
THROW_ERR_OUT_OF_RANGE(env, \
214+
"The value of \"fd\" is out of range. " \
215+
"It must be >= 0 && <= %s. Received %d", \
216+
std::to_string(INT32_MAX), \
217+
fd); \
218+
return; \
219+
} \
220+
} while (0);
221+
198222
// ECMA262 20.1.2.6 Number.MAX_SAFE_INTEGER (2^53-1)
199223
constexpr int64_t kMaxSafeJsInteger = 9007199254740991;
200224

test/parallel/test-fs-truncate.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,13 @@ function testFtruncate(cb) {
270270
['', false, null, undefined, {}, []].forEach((input) => {
271271
['ftruncate', 'ftruncateSync'].forEach((fnName) => {
272272
assert.throws(
273-
() => fs[fnName](input),
273+
() => {
274+
if (fnName === 'ftruncate') {
275+
fs[fnName](input, () => {});
276+
} else {
277+
fs[fnName](input)
278+
}
279+
},
274280
{
275281
code: 'ERR_INVALID_ARG_TYPE',
276282
name: 'TypeError',

0 commit comments

Comments
 (0)