Skip to content

Commit d453fac

Browse files
committed
fs: move type checking for fs.ftruncate to js
PR-URL: #17334 Reviewed-By: Refael Ackermann <[email protected]> Reviewed-By: Timothy Gu <[email protected]> Reviewed-By: Jon Moss <[email protected]> Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Joyee Cheung <[email protected]> Reviewed-By: Matteo Collina <[email protected]>
1 parent 8cb080c commit d453fac

File tree

3 files changed

+68
-20
lines changed

3 files changed

+68
-20
lines changed

lib/fs.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -864,11 +864,11 @@ fs.ftruncate = function(fd, len, callback) {
864864
} else if (len === undefined) {
865865
len = 0;
866866
}
867-
if (typeof fd !== 'number')
867+
if (!Number.isInteger(fd))
868868
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'fd', 'number');
869869
if (fd < 0 || fd > 0xFFFFFFFF)
870870
throw new errors.RangeError('ERR_OUT_OF_RANGE', 'fd');
871-
if (typeof len !== 'number')
871+
if (!Number.isInteger(len))
872872
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'len', 'number');
873873
len = Math.max(0, len);
874874
const req = new FSReqWrap();
@@ -880,11 +880,11 @@ fs.ftruncateSync = function(fd, len) {
880880
if (len === undefined) {
881881
len = 0;
882882
}
883-
if (typeof fd !== 'number')
883+
if (!Number.isInteger(fd))
884884
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'fd', 'number');
885885
if (fd < 0 || fd > 0xFFFFFFFF)
886886
throw new errors.RangeError('ERR_OUT_OF_RANGE', 'fd');
887-
if (typeof len !== 'number')
887+
if (!Number.isInteger(len))
888888
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'len', 'number');
889889
len = Math.max(0, len);
890890
return binding.ftruncate(fd, len);

src/node_file.cc

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -763,24 +763,11 @@ static void Rename(const FunctionCallbackInfo<Value>& args) {
763763
static void FTruncate(const FunctionCallbackInfo<Value>& args) {
764764
Environment* env = Environment::GetCurrent(args);
765765

766-
if (args.Length() < 2)
767-
return TYPE_ERROR("fd and length are required");
768-
if (!args[0]->IsInt32())
769-
return TYPE_ERROR("fd must be a file descriptor");
766+
CHECK(args[0]->IsInt32());
767+
CHECK(args[1]->IsNumber());
770768

771769
int fd = args[0]->Int32Value();
772-
773-
// FIXME(bnoordhuis) It's questionable to reject non-ints here but still
774-
// allow implicit coercion from null or undefined to zero. Probably best
775-
// handled in lib/fs.js.
776-
Local<Value> len_v(args[1]);
777-
if (!len_v->IsUndefined() &&
778-
!len_v->IsNull() &&
779-
!IsInt64(len_v->NumberValue())) {
780-
return env->ThrowTypeError("Not an integer");
781-
}
782-
783-
const int64_t len = len_v->IntegerValue();
770+
const int64_t len = args[1]->IntegerValue();
784771

785772
if (args[2]->IsObject()) {
786773
ASYNC_CALL(ftruncate, args[2], UTF8, fd, len)

test/parallel/test-fs-truncate.js

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,8 +180,69 @@ function testFtruncate(cb) {
180180
fs.writeFileSync(file5, 'Hi');
181181
const fd = fs.openSync(file5, 'r+');
182182
process.on('exit', () => fs.closeSync(fd));
183+
184+
['', false, null, {}, []].forEach((i) => {
185+
common.expectsError(
186+
() => fs.ftruncate(fd, i),
187+
{
188+
code: 'ERR_INVALID_ARG_TYPE',
189+
type: TypeError,
190+
message: 'The "len" argument must be of type number'
191+
}
192+
);
193+
});
194+
183195
fs.ftruncate(fd, undefined, common.mustCall(function(err) {
184196
assert.ifError(err);
185197
assert(fs.readFileSync(file5).equals(Buffer.from('')));
186198
}));
187199
}
200+
201+
{
202+
const file6 = path.resolve(tmp, 'truncate-file-6.txt');
203+
fs.writeFileSync(file6, 'Hi');
204+
const fd = fs.openSync(file6, 'r+');
205+
process.on('exit', () => fs.closeSync(fd));
206+
fs.ftruncate(fd, -1, common.mustCall(function(err) {
207+
assert.ifError(err);
208+
assert(fs.readFileSync(file6).equals(Buffer.from('')));
209+
}));
210+
}
211+
212+
['', false, null, undefined, {}, []].forEach((i) => {
213+
common.expectsError(
214+
() => fs.ftruncate(i),
215+
{
216+
code: 'ERR_INVALID_ARG_TYPE',
217+
type: TypeError,
218+
message: 'The "fd" argument must be of type number'
219+
}
220+
);
221+
common.expectsError(
222+
() => fs.ftruncateSync(i),
223+
{
224+
code: 'ERR_INVALID_ARG_TYPE',
225+
type: TypeError,
226+
message: 'The "fd" argument must be of type number'
227+
}
228+
);
229+
});
230+
231+
[-1, 0xFFFFFFFF + 1].forEach((i) => {
232+
common.expectsError(
233+
() => fs.ftruncate(i),
234+
{
235+
code: 'ERR_OUT_OF_RANGE',
236+
type: RangeError,
237+
message: 'The "fd" argument is out of range'
238+
}
239+
);
240+
common.expectsError(
241+
() => fs.ftruncateSync(i),
242+
{
243+
code: 'ERR_OUT_OF_RANGE',
244+
type: RangeError,
245+
message: 'The "fd" argument is out of range'
246+
}
247+
);
248+
});

0 commit comments

Comments
 (0)