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.';
}
return `The argument '${name}' ${reason}. Received ${inspected}`;
}, TypeError, RangeError);
Expand Down
8 changes: 7 additions & 1 deletion lib/internal/fs/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,13 @@ function validateOffsetLengthRead(offset, length, bufferLength) {
let err;

if (offset < 0 || offset >= bufferLength) {
err = new ERR_OUT_OF_RANGE('offset', `>= 0 && <= ${bufferLength}`, offset);
if (bufferLength === 0) {
throw ERR_INVALID_ARG_VALUE('buffer', 0,
Copy link
Member

Choose a reason for hiding this comment

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

Please change this to an ERR_OUT_OF_RANGE error that corresponds to the bufferLength. I do not know what buffer should stand for in this case and invalid argument value errors are normally used in different contexts. Here it should definitely be an out of range error as I also suggested in my code example in my earlier comment.

'is empty and can\'t be written');
} else {
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