Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit ff8313c

Browse filesBrowse files
joyeecheungtargos
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 99161b0 commit ff8313c
Copy full SHA for ff8313c

File tree

Expand file treeCollapse file tree

3 files changed

+47
-25
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

3 files changed

+47
-25
lines changed
Open diff view settings
Collapse file

‎src/node_native_module.cc‎

Copy file name to clipboardExpand all lines: src/node_native_module.cc
+9-25Lines changed: 9 additions & 25 deletions
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
Collapse file

‎src/util.cc‎

Copy file name to clipboardExpand all lines: src/util.cc
+34Lines changed: 34 additions & 0 deletions
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);
Collapse file

‎src/util.h‎

Copy file name to clipboardExpand all lines: src/util.h
+4Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -813,6 +813,10 @@ std::unique_ptr<T> static_unique_pointer_cast(std::unique_ptr<U>&& ptr) {
813813
}
814814

815815
#define MAYBE_FIELD_PTR(ptr, field) ptr == nullptr ? nullptr : &(ptr->field)
816+
817+
// Returns a non-zero code if it fails to open or read the file,
818+
// aborts if it fails to close the file.
819+
int ReadFileSync(std::string* result, const char* path);
816820
} // namespace node
817821

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

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.