Skip to content

Commit

Permalink
fs: move type checking for fs.read to js
Browse files Browse the repository at this point in the history
PR-URL: #17334
Reviewed-By: Refael Ackermann <refack@gmail.com>
Reviewed-By: Timothy Gu <timothygu99@gmail.com>
Reviewed-By: Jon Moss <me@jonathanmoss.me>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
  • Loading branch information
jasnell committed Dec 13, 2017
1 parent 448ec0b commit 1638698
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 23 deletions.
42 changes: 41 additions & 1 deletion lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -691,18 +691,38 @@ fs.openSync = function(path, flags, mode) {
};

fs.read = function(fd, buffer, offset, length, position, callback) {
if (!Number.isInteger(fd))
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'fd', 'number');
if (fd < 0 || fd > 0xFFFFFFFF)
throw new errors.RangeError('ERR_OUT_OF_RANGE', 'fd');
if (!isUint8Array(buffer))
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'buffer',
['Buffer', 'Uint8Array']);

offset |= 0;
length |= 0;

if (length === 0) {
return process.nextTick(function() {
callback && callback(null, 0, buffer);
});
}

if (offset < 0 || offset >= buffer.length)
throw new errors.RangeError('ERR_OUT_OF_RANGE', 'offset');

if (length < 0 || offset + length > buffer.length)
throw new errors.RangeError('ERR_OUT_OF_RANGE', 'length');

if (!Number.isInteger(position))
position = -1;

function wrapper(err, bytesRead) {
// Retain a reference to buffer so that it can't be GC'ed too soon.
callback && callback(err, bytesRead || 0, buffer);
}

var req = new FSReqWrap();
const req = new FSReqWrap();
req.oncomplete = wrapper;

binding.read(fd, buffer, offset, length, position, req);
Expand All @@ -712,10 +732,30 @@ Object.defineProperty(fs.read, internalUtil.customPromisifyArgs,
{ value: ['bytesRead', 'buffer'], enumerable: false });

fs.readSync = function(fd, buffer, offset, length, position) {
if (!Number.isInteger(fd))
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'fd', 'number');
if (fd < 0 || fd > 0xFFFFFFFF)
throw new errors.RangeError('ERR_OUT_OF_RANGE', 'fd');
if (!isUint8Array(buffer))
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'buffer',
['Buffer', 'Uint8Array']);

offset |= 0;
length |= 0;

if (length === 0) {
return 0;
}

if (offset < 0 || offset >= buffer.length)
throw new errors.RangeError('ERR_OUT_OF_RANGE', 'offset');

if (length < 0 || offset + length > buffer.length)
throw new errors.RangeError('ERR_OUT_OF_RANGE', 'length');

if (!Number.isInteger(position))
position = -1;

return binding.read(fd, buffer, offset, length, position);
};

Expand Down
15 changes: 4 additions & 11 deletions src/node_file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1178,12 +1178,8 @@ static void WriteString(const FunctionCallbackInfo<Value>& args) {
static void Read(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);

if (args.Length() < 2)
return TYPE_ERROR("fd and buffer are required");
if (!args[0]->IsInt32())
return TYPE_ERROR("fd must be a file descriptor");
if (!Buffer::HasInstance(args[1]))
return TYPE_ERROR("Second argument needs to be a buffer");
CHECK(args[0]->IsInt32());
CHECK(Buffer::HasInstance(args[1]));

int fd = args[0]->Int32Value();

Expand All @@ -1199,13 +1195,10 @@ static void Read(const FunctionCallbackInfo<Value>& args) {
size_t buffer_length = Buffer::Length(buffer_obj);

size_t off = args[2]->Int32Value();
if (off >= buffer_length) {
return env->ThrowError("Offset is out of bounds");
}
CHECK_LT(off, buffer_length);

len = args[3]->Int32Value();
if (!Buffer::IsWithinBounds(off, len, buffer_length))
return env->ThrowRangeError("Length extends beyond buffer");
CHECK(Buffer::IsWithinBounds(off, len, buffer_length));

pos = GET_OFFSET(args[4]);

Expand Down
27 changes: 16 additions & 11 deletions test/parallel/test-fs-read-type.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
'use strict';
const common = require('../common');
const assert = require('assert');
const fs = require('fs');
const fixtures = require('../common/fixtures');

Expand All @@ -9,14 +8,20 @@ const fd = fs.openSync(filepath, 'r');
const expected = 'xyz\n';

// Error must be thrown with string
assert.throws(() => {
fs.read(fd,
expected.length,
0,
'utf-8',
common.mustNotCall());
}, /^TypeError: Second argument needs to be a buffer$/);
common.expectsError(
() => fs.read(fd, expected.length, 0, 'utf-8', common.mustNotCall()),
{
code: 'ERR_INVALID_ARG_TYPE',
type: TypeError,
message: 'The "buffer" argument must be one of type Buffer or Uint8Array'
}
);

assert.throws(() => {
fs.readSync(fd, expected.length, 0, 'utf-8');
}, /^TypeError: Second argument needs to be a buffer$/);
common.expectsError(
() => fs.readSync(fd, expected.length, 0, 'utf-8'),
{
code: 'ERR_INVALID_ARG_TYPE',
type: TypeError,
message: 'The "buffer" argument must be one of type Buffer or Uint8Array'
}
);

0 comments on commit 1638698

Please sign in to comment.