Skip to content

Commit 09da11e

Browse files
committed
fs: throw errors from fs.readlinkSync in JS
PR-URL: #18348 Refs: #18106 Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 167e229 commit 09da11e

File tree

2 files changed

+26
-10
lines changed

2 files changed

+26
-10
lines changed

lib/fs.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1163,7 +1163,13 @@ fs.readlinkSync = function(path, options) {
11631163
handleError((path = getPathFromURL(path)));
11641164
nullCheck(path);
11651165
validatePath(path, 'oldPath');
1166-
return binding.readlink(pathModule.toNamespacedPath(path), options.encoding);
1166+
const ctx = { path };
1167+
const result = binding.readlink(pathModule.toNamespacedPath(path),
1168+
options.encoding, undefined, ctx);
1169+
if (ctx.errno !== undefined) {
1170+
throw new errors.uvException(ctx);
1171+
}
1172+
return result;
11671173
};
11681174

11691175
function preprocessSymlinkDestination(path, type, linkPath) {
@@ -1982,7 +1988,10 @@ fs.realpathSync = function realpathSync(p, options) {
19821988
if (ctx.errno !== undefined) {
19831989
throw new errors.uvException(ctx);
19841990
}
1985-
linkTarget = binding.readlink(baseLong);
1991+
linkTarget = binding.readlink(baseLong, undefined, undefined, ctx);
1992+
if (ctx.errno !== undefined) {
1993+
throw new errors.uvException(ctx);
1994+
}
19861995
}
19871996
resolvedLink = pathModule.resolve(previous, linkTarget);
19881997

src/node_file.cc

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -648,32 +648,39 @@ static void Link(const FunctionCallbackInfo<Value>& args) {
648648
static void ReadLink(const FunctionCallbackInfo<Value>& args) {
649649
Environment* env = Environment::GetCurrent(args);
650650

651-
const int argc = args.Length();
652-
653-
CHECK_GE(argc, 1);
651+
int argc = args.Length();
652+
CHECK_GE(argc, 3);
654653

655654
BufferValue path(env->isolate(), args[0]);
656655
CHECK_NE(*path, nullptr);
657656

658657
const enum encoding encoding = ParseEncoding(env->isolate(), args[1], UTF8);
659658

660659
if (args[2]->IsObject()) { // readlink(path, encoding, req)
661-
CHECK_EQ(args.Length(), 3);
660+
CHECK_EQ(argc, 3);
662661
AsyncCall(env, args, "readlink", encoding, AfterStringPtr,
663662
uv_fs_readlink, *path);
664-
} else {
665-
SYNC_CALL(readlink, *path, *path)
666-
const char* link_path = static_cast<const char*>(SYNC_REQ.ptr);
663+
} else { // readlink(path, encoding, undefined, ctx)
664+
CHECK_EQ(argc, 4);
665+
fs_req_wrap req_wrap;
666+
int err = SyncCall(env, args[3], &req_wrap, "readlink",
667+
uv_fs_readlink, *path);
668+
if (err) {
669+
return; // syscall failed, no need to continue, error info is in ctx
670+
}
671+
const char* link_path = static_cast<const char*>(req_wrap.req.ptr);
667672

668673
Local<Value> error;
669674
MaybeLocal<Value> rc = StringBytes::Encode(env->isolate(),
670675
link_path,
671676
encoding,
672677
&error);
673678
if (rc.IsEmpty()) {
674-
env->isolate()->ThrowException(error);
679+
Local<Object> ctx = args[3].As<Object>();
680+
ctx->Set(env->context(), env->error_string(), error).FromJust();
675681
return;
676682
}
683+
677684
args.GetReturnValue().Set(rc.ToLocalChecked());
678685
}
679686
}

0 commit comments

Comments
 (0)