Skip to content

Commit

Permalink
lib: add empty buffer case to read and readSync function in fs
Browse files Browse the repository at this point in the history
While using read() or readSync() function, it should be verified that
the arguments of the function are in proper range and hold legitimate
values, for a successful read process.
For this validateOffsetLengthRead() function is called, which gives an
error message for offset and length passed by the user, if not in
order. But there should also be an error when an empty buffer is passed
as argument, when it cannot be written over.
The empty buffer case should be checked before calling
validateOffsetLengthRead() and ERR_INVALID_ARG_VALUE should be thrown
corresponding to the empty buffer argument in read and readSync
function.

Fixes: nodejs#21193
  • Loading branch information
AdityaSrivast committed Jun 16, 2018
1 parent 7c1d365 commit 8043755
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 9 deletions.
11 changes: 11 additions & 0 deletions lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ const { Buffer, kMaxLength } = require('buffer');
const errors = require('internal/errors');
const {
ERR_FS_FILE_TOO_LARGE,
ERR_INVALID_ARG_VALUE,
ERR_INVALID_ARG_TYPE,
ERR_INVALID_CALLBACK
} = errors.codes;
Expand Down Expand Up @@ -457,6 +458,11 @@ function read(fd, buffer, offset, length, position, callback) {
});
}

if (buffer.length === 0) {
throw new ERR_INVALID_ARG_VALUE('buffer', buffer,
'is empty and cannot be written');
}

validateOffsetLengthRead(offset, length, buffer.length);

if (!Number.isSafeInteger(position))
Expand Down Expand Up @@ -487,6 +493,11 @@ function readSync(fd, buffer, offset, length, position) {
return 0;
}

if (buffer.length === 0) {
throw new ERR_INVALID_ARG_VALUE('buffer', buffer,
'is empty and cannot be written');
}

validateOffsetLengthRead(offset, length, buffer.length);

if (!Number.isSafeInteger(position))
Expand Down
6 changes: 6 additions & 0 deletions lib/internal/fs/promises.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const { Buffer, kMaxLength } = require('buffer');
const {
ERR_FS_FILE_TOO_LARGE,
ERR_INVALID_ARG_TYPE,
ERR_INVALID_ARG_VALUE,
ERR_METHOD_NOT_IMPLEMENTED
} = require('internal/errors').codes;
const { getPathFromURL } = require('internal/url');
Expand Down Expand Up @@ -207,6 +208,11 @@ async function read(handle, buffer, offset, length, position) {
if (length === 0)
return { bytesRead: length, buffer };

if (buffer.length === 0) {
throw new ERR_INVALID_ARG_VALUE('buffer', buffer,
'is empty and cannot be written');
}

validateOffsetLengthRead(offset, length, buffer.length);

if (!Number.isSafeInteger(position))
Expand Down
8 changes: 2 additions & 6 deletions lib/internal/fs/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -293,12 +293,8 @@ function validateOffsetLengthRead(offset, length, bufferLength) {
let err;

if (offset < 0 || offset >= bufferLength) {
if (bufferLength === 0) {
err = new ERR_OUT_OF_RANGE('bufferLength', '> 0', bufferLength);
} else {
err = new ERR_OUT_OF_RANGE('offset',
`>= 0 && <= ${bufferLength}`, offset);
}
err = new ERR_OUT_OF_RANGE('offset',
`>= 0 && <= ${bufferLength}`, offset);
} else if (length < 0 || offset + length > bufferLength) {
err = new ERR_OUT_OF_RANGE('length',
`>= 0 && <= ${bufferLength - offset}`, length);
Expand Down
4 changes: 1 addition & 3 deletions test/parallel/test-fs-read-empty-buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ const buffer = new Uint8Array();
assert.throws(
() => fs.readSync(fd, buffer, 0, 10, 0),
{
code: 'ERR_OUT_OF_RANGE',
message: 'The value of "bufferLength" is out of range. ' +
'It must be > 0. Received 0'
code: 'ERR_INVALID_ARG_VALUE',
}
);

0 comments on commit 8043755

Please sign in to comment.