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 f0192ec

Browse filesBrowse files
bnoordhuisMyles Borins
authored andcommitted
src: don't abort when c-ares initialization fails
Throw a JS exception instead of aborting so the user at least has a fighting chance of understanding what went wrong. Fixes: #8699 PR-URL: #8710 Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Franziska Hinkelmann <franziska.hinkelmann@gmail.com> Reviewed-By: Ilkka Myller <ilkka.myller@nodefield.com>
1 parent cec5e36 commit f0192ec
Copy full SHA for f0192ec

File tree

Expand file treeCollapse file tree

1 file changed

+40
-37
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

1 file changed

+40
-37
lines changed
Open diff view settings
Collapse file

‎src/cares_wrap.cc‎

Copy file name to clipboardExpand all lines: src/cares_wrap.cc
+40-37Lines changed: 40 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,38 @@ using v8::String;
4848
using v8::Value;
4949

5050

51+
inline const char* ToErrorCodeString(int status) {
52+
switch (status) {
53+
#define V(code) case ARES_##code: return #code;
54+
V(EADDRGETNETWORKPARAMS)
55+
V(EBADFAMILY)
56+
V(EBADFLAGS)
57+
V(EBADHINTS)
58+
V(EBADNAME)
59+
V(EBADQUERY)
60+
V(EBADRESP)
61+
V(EBADSTR)
62+
V(ECANCELLED)
63+
V(ECONNREFUSED)
64+
V(EDESTRUCTION)
65+
V(EFILE)
66+
V(EFORMERR)
67+
V(ELOADIPHLPAPI)
68+
V(ENODATA)
69+
V(ENOMEM)
70+
V(ENONAME)
71+
V(ENOTFOUND)
72+
V(ENOTIMP)
73+
V(ENOTINITIALIZED)
74+
V(EOF)
75+
V(EREFUSED)
76+
V(ESERVFAIL)
77+
V(ETIMEOUT)
78+
#undef V
79+
}
80+
return "UNKNOWN_ARES_ERROR";
81+
}
82+
5183
class GetAddrInfoReqWrap : public ReqWrap<uv_getaddrinfo_t> {
5284
public:
5385
GetAddrInfoReqWrap(Environment* env, Local<Object> req_wrap_obj);
@@ -330,41 +362,8 @@ class QueryWrap : public AsyncWrap {
330362
CHECK_NE(status, ARES_SUCCESS);
331363
HandleScope handle_scope(env()->isolate());
332364
Context::Scope context_scope(env()->context());
333-
Local<Value> arg;
334-
switch (status) {
335-
#define V(code) \
336-
case ARES_ ## code: \
337-
arg = FIXED_ONE_BYTE_STRING(env()->isolate(), #code); \
338-
break;
339-
V(ENODATA)
340-
V(EFORMERR)
341-
V(ESERVFAIL)
342-
V(ENOTFOUND)
343-
V(ENOTIMP)
344-
V(EREFUSED)
345-
V(EBADQUERY)
346-
V(EBADNAME)
347-
V(EBADFAMILY)
348-
V(EBADRESP)
349-
V(ECONNREFUSED)
350-
V(ETIMEOUT)
351-
V(EOF)
352-
V(EFILE)
353-
V(ENOMEM)
354-
V(EDESTRUCTION)
355-
V(EBADSTR)
356-
V(EBADFLAGS)
357-
V(ENONAME)
358-
V(EBADHINTS)
359-
V(ENOTINITIALIZED)
360-
V(ELOADIPHLPAPI)
361-
V(EADDRGETNETWORKPARAMS)
362-
V(ECANCELLED)
363-
#undef V
364-
default:
365-
arg = FIXED_ONE_BYTE_STRING(env()->isolate(), "UNKNOWN_ARES_ERROR");
366-
break;
367-
}
365+
const char* code = ToErrorCodeString(status);
366+
Local<Value> arg = OneByteString(env()->isolate(), code);
368367
MakeCallback(env()->oncomplete_string(), 1, &arg);
369368
}
370369

@@ -1270,7 +1269,8 @@ static void Initialize(Local<Object> target,
12701269
Environment* env = Environment::GetCurrent(context);
12711270

12721271
int r = ares_library_init(ARES_LIB_INIT_ALL);
1273-
CHECK_EQ(r, ARES_SUCCESS);
1272+
if (r != ARES_SUCCESS)
1273+
return env->ThrowError(ToErrorCodeString(r));
12741274

12751275
struct ares_options options;
12761276
memset(&options, 0, sizeof(options));
@@ -1282,7 +1282,10 @@ static void Initialize(Local<Object> target,
12821282
r = ares_init_options(env->cares_channel_ptr(),
12831283
&options,
12841284
ARES_OPT_FLAGS | ARES_OPT_SOCK_STATE_CB);
1285-
CHECK_EQ(r, ARES_SUCCESS);
1285+
if (r != ARES_SUCCESS) {
1286+
ares_library_cleanup();
1287+
return env->ThrowError(ToErrorCodeString(r));
1288+
}
12861289

12871290
/* Initialize the timeout timer. The timer won't be started until the */
12881291
/* first socket is opened. */

0 commit comments

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