Skip to content
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 getServers return undefined #43922

Merged
merged 1 commit into from
Jul 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/internal/dns/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class Resolver {
}

getServers() {
return ArrayPrototypeMap(this._handle.getServers(), (val) => {
return ArrayPrototypeMap(this._handle.getServers() || [], (val) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In which case getServers() will return null or undefined?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think when server_array->Set(env->context(), i, Array::New(env->isolate(), ret, arraysize(ret))) .IsNothing() is true in GetServers of cares_wrap.cc.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't reproduce it using javascript.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, i think this is difficult to trigger.

if (!val[1] || val[1] === IANA_DNS_PORT)
return val[0];

Expand All @@ -76,7 +76,7 @@ class Resolver {
// Cache the original servers because in the event of an error while
// setting the servers, c-ares won't have any servers available for
// resolution.
const orig = this._handle.getServers();
const orig = this._handle.getServers() || [];
const newSet = [];

ArrayPrototypeForEach(servers, (serv, index) => {
Expand Down
11 changes: 11 additions & 0 deletions test/parallel/test-dns-get-server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
'use strict';
const common = require('../common');
const assert = require('assert');

const { Resolver } = require('dns');

const resolver = new Resolver();
assert(resolver.getServers().length > 0);
// return undefined
resolver._handle.getServers = common.mustCall(() => {});
assert.strictEqual(resolver.getServers().length, 0);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
assert.strictEqual(resolver.getServers().length, 0);
assert.strictEqual(resolver.getServers().length, 0);