Skip to content

Commit f0192ec

Browse files
bnoordhuisMyles Borins
authored and
Myles Borins
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 cec5e36 commit f0192ec

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
@@ -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)