Skip to content

Commit c6acfdb

Browse files
committed
fs: throw readSync errors in JS
PR-URL: #19041 Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 7cadb57 commit c6acfdb

File tree

3 files changed

+51
-27
lines changed

3 files changed

+51
-27
lines changed

lib/fs.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -577,7 +577,11 @@ fs.readSync = function(fd, buffer, offset, length, position) {
577577
if (!isUint32(position))
578578
position = -1;
579579

580-
return binding.read(fd, buffer, offset, length, position);
580+
const ctx = {};
581+
const result = binding.read(fd, buffer, offset, length, position,
582+
undefined, ctx);
583+
handleErrorFromBinding(ctx);
584+
return result;
581585
};
582586

583587
// usage:

src/node_file.cc

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ using v8::Value;
110110
# define MIN(a, b) ((a) < (b) ? (a) : (b))
111111
#endif
112112

113-
#define GET_OFFSET(a) ((a)->IsNumber() ? (a)->IntegerValue() : -1)
113+
#define GET_OFFSET(a) ((a)->IsNumber() ? (a).As<Integer>()->Value() : -1)
114114

115115
// The FileHandle object wraps a file descriptor and will close it on garbage
116116
// collection if necessary. If that happens, a process warning will be
@@ -1411,51 +1411,50 @@ static void WriteString(const FunctionCallbackInfo<Value>& args) {
14111411
*
14121412
* bytesRead = fs.read(fd, buffer, offset, length, position)
14131413
*
1414-
* 0 fd integer. file descriptor
1414+
* 0 fd int32. file descriptor
14151415
* 1 buffer instance of Buffer
1416-
* 2 offset integer. offset to start reading into inside buffer
1417-
* 3 length integer. length to read
1418-
* 4 position file position - null for current position
1419-
*
1416+
* 2 offset int32. offset to start reading into inside buffer
1417+
* 3 length int32. length to read
1418+
* 4 position int64. file position - -1 for current position
14201419
*/
14211420
static void Read(const FunctionCallbackInfo<Value>& args) {
14221421
Environment* env = Environment::GetCurrent(args);
14231422

1424-
CHECK(args[0]->IsInt32());
1425-
CHECK(Buffer::HasInstance(args[1]));
1426-
1427-
int fd = args[0]->Int32Value();
1428-
1429-
Local<Value> req;
1430-
1431-
size_t len;
1432-
int64_t pos;
1423+
const int argc = args.Length();
1424+
CHECK_GE(argc, 5);
14331425

1434-
char * buf = nullptr;
1426+
CHECK(args[0]->IsInt32());
1427+
const int fd = args[0].As<Int32>()->Value();
14351428

1429+
CHECK(Buffer::HasInstance(args[1]));
14361430
Local<Object> buffer_obj = args[1].As<Object>();
1437-
char *buffer_data = Buffer::Data(buffer_obj);
1431+
char* buffer_data = Buffer::Data(buffer_obj);
14381432
size_t buffer_length = Buffer::Length(buffer_obj);
14391433

1440-
size_t off = args[2]->Int32Value();
1434+
CHECK(args[2]->IsInt32());
1435+
const size_t off = static_cast<size_t>(args[2].As<Int32>()->Value());
14411436
CHECK_LT(off, buffer_length);
14421437

1443-
len = args[3]->Int32Value();
1438+
CHECK(args[3]->IsInt32());
1439+
const size_t len = static_cast<size_t>(args[3].As<Int32>()->Value());
14441440
CHECK(Buffer::IsWithinBounds(off, len, buffer_length));
14451441

1446-
pos = GET_OFFSET(args[4]);
1447-
1448-
buf = buffer_data + off;
1442+
CHECK(args[4]->IsNumber());
1443+
const int64_t pos = args[4].As<Integer>()->Value();
14491444

1445+
char* buf = buffer_data + off;
14501446
uv_buf_t uvbuf = uv_buf_init(const_cast<char*>(buf), len);
14511447

14521448
FSReqBase* req_wrap = GetReqWrap(env, args[5]);
1453-
if (req_wrap != nullptr) {
1449+
if (req_wrap != nullptr) { // read(fd, buffer, offset, len, pos, req)
14541450
AsyncCall(env, req_wrap, args, "read", UTF8, AfterInteger,
14551451
uv_fs_read, fd, &uvbuf, 1, pos);
1456-
} else {
1457-
SYNC_CALL(read, 0, fd, &uvbuf, 1, pos)
1458-
args.GetReturnValue().Set(SYNC_RESULT);
1452+
} else { // read(fd, buffer, offset, len, pos, undefined, ctx)
1453+
CHECK_EQ(argc, 7);
1454+
fs_req_wrap req_wrap;
1455+
const int bytesRead = SyncCall(env, args[6], &req_wrap, "read",
1456+
uv_fs_read, fd, &uvbuf, 1, pos);
1457+
args.GetReturnValue().Set(bytesRead);
14591458
}
14601459
}
14611460

test/parallel/test-fs-error-messages.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -708,3 +708,24 @@ if (!common.isAIX) {
708708
validateError
709709
);
710710
}
711+
712+
// read
713+
{
714+
const validateError = (err) => {
715+
assert.strictEqual(err.message, 'EBADF: bad file descriptor, read');
716+
assert.strictEqual(err.errno, uv.UV_EBADF);
717+
assert.strictEqual(err.code, 'EBADF');
718+
assert.strictEqual(err.syscall, 'read');
719+
return true;
720+
};
721+
722+
common.runWithInvalidFD((fd) => {
723+
const buf = Buffer.alloc(5);
724+
fs.read(fd, buf, 0, 1, 1, common.mustCall(validateError));
725+
726+
assert.throws(
727+
() => fs.readSync(fd, buf, 0, 1, 1),
728+
validateError
729+
);
730+
});
731+
}

0 commit comments

Comments
 (0)