Skip to content

Commit 5cb5b1f

Browse files
committed
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 <[email protected]> Reviewed-By: Franziska Hinkelmann <[email protected]> Reviewed-By: Ilkka Myller <[email protected]>
1 parent a5994f7 commit 5cb5b1f

File tree

1 file changed

+40
-37
lines changed

1 file changed

+40
-37
lines changed

src/cares_wrap.cc

+40-37
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,38 @@ using v8::String;
4747
using v8::Value;
4848

4949

50+
inline const char* ToErrorCodeString(int status) {
51+
switch (status) {
52+
#define V(code) case ARES_##code: return #code;
53+
V(EADDRGETNETWORKPARAMS)
54+
V(EBADFAMILY)
55+
V(EBADFLAGS)
56+
V(EBADHINTS)
57+
V(EBADNAME)
58+
V(EBADQUERY)
59+
V(EBADRESP)
60+
V(EBADSTR)
61+
V(ECANCELLED)
62+
V(ECONNREFUSED)
63+
V(EDESTRUCTION)
64+
V(EFILE)
65+
V(EFORMERR)
66+
V(ELOADIPHLPAPI)
67+
V(ENODATA)
68+
V(ENOMEM)
69+
V(ENONAME)
70+
V(ENOTFOUND)
71+
V(ENOTIMP)
72+
V(ENOTINITIALIZED)
73+
V(EOF)
74+
V(EREFUSED)
75+
V(ESERVFAIL)
76+
V(ETIMEOUT)
77+
#undef V
78+
}
79+
return "UNKNOWN_ARES_ERROR";
80+
}
81+
5082
class GetAddrInfoReqWrap : public ReqWrap<uv_getaddrinfo_t> {
5183
public:
5284
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

@@ -1311,7 +1310,8 @@ static void Initialize(Local<Object> target,
13111310
Environment* env = Environment::GetCurrent(context);
13121311

13131312
int r = ares_library_init(ARES_LIB_INIT_ALL);
1314-
CHECK_EQ(r, ARES_SUCCESS);
1313+
if (r != ARES_SUCCESS)
1314+
return env->ThrowError(ToErrorCodeString(r));
13151315

13161316
struct ares_options options;
13171317
memset(&options, 0, sizeof(options));
@@ -1323,7 +1323,10 @@ static void Initialize(Local<Object> target,
13231323
r = ares_init_options(env->cares_channel_ptr(),
13241324
&options,
13251325
ARES_OPT_FLAGS | ARES_OPT_SOCK_STATE_CB);
1326-
CHECK_EQ(r, ARES_SUCCESS);
1326+
if (r != ARES_SUCCESS) {
1327+
ares_library_cleanup();
1328+
return env->ThrowError(ToErrorCodeString(r));
1329+
}
13271330

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

0 commit comments

Comments
 (0)