Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lib: add case of no argument to ERR_INVALID_ARG_VALUE in errors.js #21262

Closed
wants to merge 6 commits into from
2 changes: 2 additions & 0 deletions lib/internal/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -637,6 +637,8 @@ E('ERR_INVALID_ARG_VALUE', (name, value, reason = 'is invalid') => {
let inspected = util.inspect(value);
if (inspected.length > 128) {
inspected = `${inspected.slice(0, 128)}...`;
} else if (inspected === '0') {
inspected = 'No value.';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure I understand this change.

}
return `The argument '${name}' ${reason}. Received ${inspected}`;
}, TypeError, RangeError);
Expand Down
6 changes: 4 additions & 2 deletions lib/internal/fs/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -291,8 +291,10 @@ function validateBuffer(buffer) {

function validateOffsetLengthRead(offset, length, bufferLength) {
let err;

if (offset < 0 || offset >= bufferLength) {
if (bufferLength === 0) {
err = new ERR_INVALID_ARG_VALUE('buffer', 0,
"is empty and can't be written.");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By the way, in this code base we use single quotes. Please run make lint (non-Windows) or vcbuild.bat lint (Windows) to run the tests.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please also move this statement in the already existing if statement and change it to an ERR_OUT_OF_RANGE error. That way the extra check is only triggered in the error case as in:

if (offset < 0 || offset >= bufferLength) {
  if (bufferLength === 0) {
    err = new ERR_OUT_OF_RANGE('bufferLength', '> 0', bufferLength);
  } else {
    err = new ...;
  }
} else ...

} else if (offset < 0 || offset >= bufferLength) {
err = new ERR_OUT_OF_RANGE('offset', `>= 0 && <= ${bufferLength}`, offset);
} else if (length < 0 || offset + length > bufferLength) {
err = new ERR_OUT_OF_RANGE('length',
Expand Down
28 changes: 19 additions & 9 deletions test/parallel/test-internal-errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,25 @@ errors.E('TEST_ERROR_2', (a, b) => `${a} ${b}`, Error);
assert.strictEqual(err.code, 'TEST_ERROR_2');
}

{
assert.throws(
() => new errors.codes.TEST_ERROR_1(),
{
message: 'Code: TEST_ERROR_1; The provided arguments ' +
'length (0) does not match the required ones (1).'
}
);
}
// {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In general, we do not leave the code commented in the code base.

// assert.throws(() => new errors.codes.TEST_ERROR_1(){
// message: 'Code: TEST_ERROR_1; The provided arguments ' +
// 'length (0) does not match the required ones (1).'
// }
// );
// }

assert.expectsError(() => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you write the tests in the corresponding test/parallel/test-fs-read*.js files? Or add a new test for the new error and name it with something like test/parallel/test-fs-read-empty-buffer.js.

const fs = require('fs');
const file = '/home/aditya/node/test/nodetest/testcodes';
const keyfile = fs.openSync(file, 'r');
const string = new Uint8Array();
const num = fs.readSync(keyfile, string, 0, 10, 0);
console.log(num);
}, { code: 'ERR_OUT_OF_RANGE',
type: RangeError,
message: 'Just Testing'
});

// Tests for common.expectsError
common.expectsError(() => {
Expand Down