fs: throw ERR_INVALID_ARG_VALUE when buffer being written is empty
Fixes: https://github.com/nodejs/node/issues/21193 PR-URL: https://github.com/nodejs/node/pull/21262 Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
parent
c7707a1cca
commit
42bded83e8
11
lib/fs.js
11
lib/fs.js
@ -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;
|
||||
@ -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))
|
||||
@ -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))
|
||||
|
@ -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');
|
||||
@ -208,6 +209,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))
|
||||
|
@ -293,7 +293,8 @@ function validateOffsetLengthRead(offset, length, bufferLength) {
|
||||
let err;
|
||||
|
||||
if (offset < 0 || offset >= bufferLength) {
|
||||
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);
|
||||
|
18
test/parallel/test-fs-read-empty-buffer.js
Normal file
18
test/parallel/test-fs-read-empty-buffer.js
Normal file
@ -0,0 +1,18 @@
|
||||
'use strict';
|
||||
require('../common');
|
||||
const fixtures = require('../common/fixtures');
|
||||
const assert = require('assert');
|
||||
const fs = require('fs');
|
||||
const filepath = fixtures.path('x.txt');
|
||||
const fd = fs.openSync(filepath, 'r');
|
||||
|
||||
const buffer = new Uint8Array();
|
||||
|
||||
assert.throws(
|
||||
() => fs.readSync(fd, buffer, 0, 10, 0),
|
||||
{
|
||||
code: 'ERR_INVALID_ARG_VALUE',
|
||||
message: 'The argument \'buffer\' is empty and cannot be written. ' +
|
||||
'Received Uint8Array [ ]'
|
||||
}
|
||||
);
|
Loading…
x
Reference in New Issue
Block a user