Skip to content

src: fixup fs SyncCall to propagate errors correctly #57711

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

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 16 additions & 10 deletions src/node_file-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -344,23 +344,29 @@ FSReqBase* AsyncCall(Environment* env,
// creating an error in the C++ land.
// ctx must be checked using value->IsObject() before being passed.
template <typename Func, typename... Args>
int SyncCall(Environment* env, v8::Local<v8::Value> ctx,
FSReqWrapSync* req_wrap, const char* syscall,
Func fn, Args... args) {
v8::Maybe<int> SyncCall(Environment* env,
v8::Local<v8::Value> ctx,
FSReqWrapSync* req_wrap,
const char* syscall,
Func fn,
Args... args) {
env->PrintSyncTrace();
int err = fn(env->event_loop(), &(req_wrap->req), args..., nullptr);
if (err < 0) {
v8::Local<v8::Context> context = env->context();
v8::Local<v8::Object> ctx_obj = ctx.As<v8::Object>();
v8::Isolate* isolate = env->isolate();
ctx_obj->Set(context,
env->errno_string(),
v8::Integer::New(isolate, err)).Check();
ctx_obj->Set(context,
env->syscall_string(),
OneByteString(isolate, syscall)).Check();
if (ctx_obj
->Set(context, env->errno_string(), v8::Integer::New(isolate, err))
.IsNothing() ||
ctx_obj
->Set(
context, env->syscall_string(), OneByteString(isolate, syscall))
.IsNothing()) {
return v8::Nothing<int>();
}
}
return err;
return v8::Just(err);
}

// Similar to SyncCall but throws immediately if there is an error.
Expand Down
47 changes: 41 additions & 6 deletions src/node_file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2239,8 +2239,19 @@ static void OpenFileHandle(const FunctionCallbackInfo<Value>& args) {
CHECK_EQ(argc, 5);
FSReqWrapSync req_wrap_sync;
FS_SYNC_TRACE_BEGIN(open);
int result = SyncCall(env, args[4], &req_wrap_sync, "open",
uv_fs_open, *path, flags, mode);
int result;
if (!SyncCall(env,
args[4],
&req_wrap_sync,
"open",
uv_fs_open,
*path,
flags,
mode)
.To(&result)) {
// v8 error occurred while setting the context. propagate!
return;
}
FS_SYNC_TRACE_END(open);
if (result < 0) {
return; // syscall failed, no need to continue, error info is in ctx
Expand Down Expand Up @@ -2356,8 +2367,20 @@ static void WriteBuffer(const FunctionCallbackInfo<Value>& args) {
CHECK_EQ(argc, 7);
FSReqWrapSync req_wrap_sync;
FS_SYNC_TRACE_BEGIN(write);
int bytesWritten = SyncCall(env, args[6], &req_wrap_sync, "write",
uv_fs_write, fd, &uvbuf, 1, pos);
int bytesWritten;
if (!SyncCall(env,
args[6],
&req_wrap_sync,
"write",
uv_fs_write,
fd,
&uvbuf,
1,
pos)
.To(&bytesWritten)) {
FS_SYNC_TRACE_END(write, "bytesWritten", 0);
return;
}
FS_SYNC_TRACE_END(write, "bytesWritten", bytesWritten);
args.GetReturnValue().Set(bytesWritten);
}
Expand Down Expand Up @@ -2515,8 +2538,20 @@ static void WriteString(const FunctionCallbackInfo<Value>& args) {
uv_buf_t uvbuf = uv_buf_init(buf, len);
FSReqWrapSync req_wrap_sync("write");
FS_SYNC_TRACE_BEGIN(write);
int bytesWritten = SyncCall(env, args[5], &req_wrap_sync, "write",
uv_fs_write, fd, &uvbuf, 1, pos);
int bytesWritten;
if (!SyncCall(env,
args[5],
&req_wrap_sync,
"write",
uv_fs_write,
fd,
&uvbuf,
1,
pos)
.To(&bytesWritten)) {
FS_SYNC_TRACE_END(write, "bytesWritten", 0);
return;
}
FS_SYNC_TRACE_END(write, "bytesWritten", bytesWritten);
args.GetReturnValue().Set(bytesWritten);
}
Expand Down
9 changes: 6 additions & 3 deletions src/node_file.h
Original file line number Diff line number Diff line change
Expand Up @@ -512,9 +512,12 @@ inline FSReqBase* AsyncCall(Environment* env,
// creating an error in the C++ land.
// ctx must be checked using value->IsObject() before being passed.
template <typename Func, typename... Args>
inline int SyncCall(Environment* env, v8::Local<v8::Value> ctx,
FSReqWrapSync* req_wrap, const char* syscall,
Func fn, Args... args);
inline v8::Maybe<int> SyncCall(Environment* env,
v8::Local<v8::Value> ctx,
FSReqWrapSync* req_wrap,
const char* syscall,
Func fn,
Args... args);

// Similar to SyncCall but throws immediately if there is an error.
template <typename Predicate, typename Func, typename... Args>
Expand Down
42 changes: 42 additions & 0 deletions test/parallel/test-fs-writesync-crash.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
'use strict';

require('../common');

const {
writeSync,
writeFileSync,
chmodSync,
openSync,
} = require('node:fs');

const {
throws,
} = require('node:assert');

// If a file's mode change after it is opened but before it is written to,
// and the Object.prototype is manipulated to throw an error when the errno
// or fd property is set or accessed, then the writeSync call would crash
// the process. This test verifies that the error is properly propagated
// instead.

const tmpdir = require('../common/tmpdir');
console.log(tmpdir.path);
tmpdir.refresh();
const path = `${tmpdir.path}/foo`;
writeFileSync(path, '');

// Do this after calling tmpdir.refresh() or that call will fail
// before we get to the part we want to test.
const error = new Error();
Object.defineProperty(Object.prototype, 'errno', {
__proto__: null,
set() {
throw error;
},
get() { return 0; }
});

const fd = openSync(path);
chmodSync(path, 0o600);

throws(() => writeSync(fd, 'test'), error);
Loading