-
Notifications
You must be signed in to change notification settings - Fork 29.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
dns: fix crash while using dns.setServers after dns.resolve4 #13050
Changes from 3 commits
0c51d0a
c6ac529
fd610db
56aff34
00c2c11
7390ee0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -69,6 +69,8 @@ using v8::Value; | |
|
||
namespace { | ||
|
||
#define ARES_HOSTENT_COPY_ERROR -1000 | ||
|
||
inline const char* ToErrorCodeString(int status) { | ||
switch (status) { | ||
#define V(code) case ARES_##code: return #code; | ||
|
@@ -96,8 +98,10 @@ inline const char* ToErrorCodeString(int status) { | |
V(EREFUSED) | ||
V(ESERVFAIL) | ||
V(ETIMEOUT) | ||
V(HOSTENT_COPY_ERROR) | ||
#undef V | ||
} | ||
|
||
return "UNKNOWN_ARES_ERROR"; | ||
} | ||
|
||
|
@@ -296,6 +300,124 @@ Local<Array> HostentToNames(Environment* env, struct hostent* host) { | |
return scope.Escape(names); | ||
} | ||
|
||
void safe_free_hostent(struct hostent* host) { | ||
int idx; | ||
|
||
if (host->h_addr_list) { | ||
idx = 0; | ||
while (host->h_addr_list[idx]) { | ||
free(host->h_addr_list[idx++]); | ||
} | ||
free(host->h_addr_list); | ||
host->h_addr_list = 0; | ||
} | ||
|
||
if (host->h_aliases) { | ||
idx = 0; | ||
while (host->h_aliases[idx]) { | ||
free(host->h_aliases[idx++]); | ||
} | ||
free(host->h_aliases); | ||
host->h_aliases = 0; | ||
} | ||
|
||
if (host->h_name) { | ||
free(host->h_name); | ||
} | ||
|
||
host->h_addrtype = host->h_length = 0; | ||
} | ||
|
||
bool cares_wrap_hostent_cpy(struct hostent* dest, struct hostent* src) { | ||
dest->h_addr_list = nullptr; | ||
dest->h_addrtype = 0; | ||
dest->h_aliases = nullptr; | ||
dest->h_length = 0; | ||
dest->h_name = nullptr; | ||
|
||
/* copy `h_name` */ | ||
unsigned int name_size = (strlen(src->h_name) + 1) * sizeof(char); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
dest->h_name = reinterpret_cast<char*>(node::Malloc(name_size)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There should be no need to cast here |
||
if (!dest->h_name) return false; | ||
memcpy(dest->h_name, src->h_name, name_size); | ||
|
||
/* copy `h_aliases` */ | ||
unsigned int alias_count; | ||
unsigned int alias_size; | ||
unsigned int cur_alias_length; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think |
||
for (alias_count = 0; | ||
src->h_aliases[alias_count] != nullptr; | ||
alias_count++) { | ||
} | ||
alias_size = sizeof(char*) * (alias_count + 1); | ||
|
||
dest->h_aliases = reinterpret_cast<char**>(node::Malloc(alias_size)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
if (dest->h_aliases == nullptr) { | ||
safe_free_hostent(dest); | ||
return false; | ||
} | ||
|
||
memset(reinterpret_cast<void*>(dest->h_aliases), 0, alias_size); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. reinterpret_cast → static_cast? |
||
for (unsigned int i = 0; i < alias_count; i++) { | ||
cur_alias_length = strlen(src->h_aliases[i]); | ||
dest->h_aliases[i] = reinterpret_cast<char*>( | ||
node::Malloc(sizeof(char) * (cur_alias_length + 1))); | ||
|
||
if (dest->h_aliases[i] == nullptr) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. hmmmm, so what's your suggestion here? Use |
||
safe_free_hostent(dest); | ||
return false; | ||
} | ||
|
||
memcpy(dest->h_aliases[i], | ||
src->h_aliases[i], | ||
sizeof(char) * (cur_alias_length + 1)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We do like to align our arguments vertically :) (i.e. make |
||
} | ||
|
||
/* copy `h_addr_list` */ | ||
unsigned int list_count; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
unsigned int list_size; | ||
unsigned int node_size; | ||
for (list_count = 0; | ||
src->h_addr_list[list_count] != nullptr; | ||
list_count++) { | ||
} | ||
list_size = sizeof(char*) * (list_count + 1); | ||
|
||
dest->h_addr_list = reinterpret_cast<char**>(node::Malloc(list_size)); | ||
if (dest->h_addr_list == nullptr) { | ||
safe_free_hostent(dest); | ||
return false; | ||
} | ||
|
||
node_size = sizeof(char) * src->h_length; | ||
memset(reinterpret_cast<void*>(dest->h_addr_list), 0, list_size); | ||
for (unsigned int i = 0; i < list_count; i++) { | ||
dest->h_addr_list[i] = reinterpret_cast<char*>(node::Malloc(node_size)); | ||
if (dest->h_addr_list[i] == nullptr) { | ||
safe_free_hostent(dest); | ||
return false; | ||
} | ||
|
||
memcpy(dest->h_addr_list[i], src->h_addr_list, node_size); | ||
} | ||
|
||
/* work after work */ | ||
dest->h_length = src->h_length; | ||
dest->h_addrtype = src->h_addrtype; | ||
|
||
return true; | ||
} | ||
|
||
class QueryWrap; | ||
struct CaresAsyncData { | ||
QueryWrap* wrap; | ||
int status; | ||
bool is_host; | ||
void* buf; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It might be worth turning this into a |
||
int len; | ||
|
||
uv_async_t async_handle; | ||
}; | ||
|
||
class QueryWrap : public AsyncWrap { | ||
public: | ||
|
@@ -328,30 +450,91 @@ class QueryWrap : public AsyncWrap { | |
return static_cast<void*>(this); | ||
} | ||
|
||
static void Callback(void *arg, int status, int timeouts, | ||
unsigned char* answer_buf, int answer_len) { | ||
QueryWrap* wrap = static_cast<QueryWrap*>(arg); | ||
static void CaresAsyncClose(uv_handle_t* handle) { | ||
uv_async_t* async = reinterpret_cast<uv_async_t*>(handle); | ||
struct CaresAsyncData* data = | ||
static_cast<struct CaresAsyncData*>(async->data); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think in this case it should be fine to just use |
||
delete data->wrap; | ||
delete data; | ||
} | ||
|
||
static void CaresAsyncCb(uv_async_t* handle) { | ||
struct CaresAsyncData* data = | ||
static_cast<struct CaresAsyncData*>(handle->data); | ||
|
||
QueryWrap* wrap = data->wrap; | ||
int status = data->status; | ||
|
||
if (status != ARES_SUCCESS) { | ||
wrap->ParseError(status); | ||
} else if (!data->is_host) { | ||
unsigned char* buf = reinterpret_cast<unsigned char*>(data->buf); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
wrap->Parse(buf, data->len); | ||
free(buf); | ||
} else { | ||
wrap->Parse(answer_buf, answer_len); | ||
hostent* host = static_cast<struct hostent*>(data->buf); | ||
wrap->Parse(host); | ||
safe_free_hostent(host); | ||
free(host); | ||
} | ||
|
||
delete wrap; | ||
uv_close(reinterpret_cast<uv_handle_t*>(handle), CaresAsyncClose); | ||
} | ||
|
||
static void Callback(void *arg, int status, int timeouts, | ||
unsigned char* answer_buf, int answer_len) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Style nit: Can you align the arguments vertically here, too? |
||
QueryWrap* wrap = static_cast<QueryWrap*>(arg); | ||
|
||
unsigned char* buf_copy = nullptr; | ||
if (status == ARES_SUCCESS) { | ||
buf_copy = (unsigned char*)node::Malloc( | ||
sizeof(unsigned char) * answer_len); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
memcpy(buf_copy, answer_buf, sizeof(unsigned char) * answer_len); | ||
} | ||
|
||
struct CaresAsyncData* data = new struct CaresAsyncData(); | ||
data->status = status; | ||
data->wrap = wrap; | ||
data->is_host = false; | ||
data->buf = buf_copy; | ||
data->len = answer_len; | ||
|
||
uv_async_t* async_handle = &data->async_handle; | ||
uv_async_init(wrap->env()->event_loop(), async_handle, CaresAsyncCb); | ||
|
||
async_handle->data = data; | ||
uv_async_send(async_handle); | ||
} | ||
|
||
static void Callback(void *arg, int status, int timeouts, | ||
struct hostent* host) { | ||
QueryWrap* wrap = static_cast<QueryWrap*>(arg); | ||
|
||
if (status != ARES_SUCCESS) { | ||
wrap->ParseError(status); | ||
struct hostent* host_copy = nullptr; | ||
bool copy_ret = false; | ||
|
||
if (status == ARES_SUCCESS) { | ||
host_copy = (struct hostent*)node::Malloc(sizeof(hostent)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
copy_ret = cares_wrap_hostent_cpy(host_copy, host); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
} | ||
|
||
struct CaresAsyncData* data = new struct CaresAsyncData(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. style nit: drop the |
||
if (copy_ret) { | ||
data->status = status; | ||
data->buf = host_copy; | ||
} else { | ||
wrap->Parse(host); | ||
data->status = ARES_HOSTENT_COPY_ERROR; | ||
data->buf = nullptr; | ||
free(host_copy); | ||
} | ||
data->wrap = wrap; | ||
data->is_host = true; | ||
|
||
delete wrap; | ||
uv_async_t* async_handle = &data->async_handle; | ||
uv_async_init(wrap->env()->event_loop(), async_handle, CaresAsyncCb); | ||
|
||
async_handle->data = data; | ||
uv_async_send(async_handle); | ||
} | ||
|
||
void CallOnComplete(Local<Value> answer, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
'use strict'; | ||
|
||
const common = require('../common'); | ||
const dns = require('dns'); | ||
|
||
dns.resolve4('google.com', common.mustCall(function(/* err, nameServers */) { | ||
// do not care about `err` and `nameServers`, | ||
// both failed and succeeded would be OK. | ||
// | ||
// just to test crash | ||
|
||
dns.setServers([ '8.8.8.8' ]); | ||
|
||
// the test case shouldn't crash here | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These comments can probably be dropped, but if you want to keep them, could you move them to the top like this. Note the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Got it. √ |
||
// see https://github.com/nodejs/node/pull/13050 and | ||
// https://github.com/nodejs/node/issues/894 | ||
})); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just a style nit, but we do prefer explicit
!= nullptr
or== nullptr
for these checks