Skip to content

Commit bee477b

Browse files
joyeecheungrichardlau
authored andcommitted
src: throw error in LoadBuiltinModuleSource when reading fails
- Move the file reading code in LoadBuiltinModuleSource into util.h so that it can be reused by other C++ code, and return an error code from it when there is a failure for the caller to generate an error. - Throw an error when reading local builtins fails in LoadBulitinModuleSource. PR-URL: #38904 Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
1 parent b6942a6 commit bee477b

File tree

3 files changed

+46
-25
lines changed

3 files changed

+46
-25
lines changed

src/node_native_module.cc

+9-25
Original file line numberDiff line numberDiff line change
@@ -205,33 +205,17 @@ MaybeLocal<String> NativeModuleLoader::LoadBuiltinModuleSource(Isolate* isolate,
205205
#ifdef NODE_BUILTIN_MODULES_PATH
206206
std::string filename = OnDiskFileName(id);
207207

208-
uv_fs_t req;
209-
uv_file file =
210-
uv_fs_open(nullptr, &req, filename.c_str(), O_RDONLY, 0, nullptr);
211-
CHECK_GE(req.result, 0);
212-
uv_fs_req_cleanup(&req);
213-
214-
auto defer_close = OnScopeLeave([file]() {
215-
uv_fs_t close_req;
216-
CHECK_EQ(0, uv_fs_close(nullptr, &close_req, file, nullptr));
217-
uv_fs_req_cleanup(&close_req);
218-
});
219-
220208
std::string contents;
221-
char buffer[4096];
222-
uv_buf_t buf = uv_buf_init(buffer, sizeof(buffer));
223-
224-
while (true) {
225-
const int r =
226-
uv_fs_read(nullptr, &req, file, &buf, 1, contents.length(), nullptr);
227-
CHECK_GE(req.result, 0);
228-
uv_fs_req_cleanup(&req);
229-
if (r <= 0) {
230-
break;
231-
}
232-
contents.append(buf.base, r);
209+
int r = ReadFileSync(&contents, filename.c_str());
210+
if (r != 0) {
211+
const std::string buf = SPrintF("Cannot read local builtin. %s: %s \"%s\"",
212+
uv_err_name(r),
213+
uv_strerror(r),
214+
filename);
215+
Local<String> message = OneByteString(isolate, buf.c_str());
216+
isolate->ThrowException(v8::Exception::Error(message));
217+
return MaybeLocal<String>();
233218
}
234-
235219
return String::NewFromUtf8(
236220
isolate, contents.c_str(), v8::NewStringType::kNormal, contents.length());
237221
#else

src/util.cc

+34
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,40 @@ int WriteFileSync(v8::Isolate* isolate,
221221
return WriteFileSync(path, buf);
222222
}
223223

224+
int ReadFileSync(std::string* result, const char* path) {
225+
uv_fs_t req;
226+
uv_file file = uv_fs_open(nullptr, &req, path, O_RDONLY, 0, nullptr);
227+
if (req.result < 0) {
228+
return req.result;
229+
}
230+
uv_fs_req_cleanup(&req);
231+
232+
auto defer_close = OnScopeLeave([file]() {
233+
uv_fs_t close_req;
234+
CHECK_EQ(0, uv_fs_close(nullptr, &close_req, file, nullptr));
235+
uv_fs_req_cleanup(&close_req);
236+
});
237+
238+
*result = std::string("");
239+
char buffer[4096];
240+
uv_buf_t buf = uv_buf_init(buffer, sizeof(buffer));
241+
242+
while (true) {
243+
const int r =
244+
uv_fs_read(nullptr, &req, file, &buf, 1, result->length(), nullptr);
245+
if (req.result < 0) {
246+
uv_fs_req_cleanup(&req);
247+
return req.result;
248+
}
249+
uv_fs_req_cleanup(&req);
250+
if (r <= 0) {
251+
break;
252+
}
253+
result->append(buf.base, r);
254+
}
255+
return 0;
256+
}
257+
224258
void DiagnosticFilename::LocalTime(TIME_TYPE* tm_struct) {
225259
#ifdef _WIN32
226260
GetLocalTime(tm_struct);

src/util.h

+3
Original file line numberDiff line numberDiff line change
@@ -800,6 +800,9 @@ std::unique_ptr<T> static_unique_pointer_cast(std::unique_ptr<U>&& ptr) {
800800
return std::unique_ptr<T>(static_cast<T*>(ptr.release()));
801801
}
802802

803+
// Returns a non-zero code if it fails to open or read the file,
804+
// aborts if it fails to close the file.
805+
int ReadFileSync(std::string* result, const char* path);
803806
} // namespace node
804807

805808
#endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS

0 commit comments

Comments
 (0)