diff --git a/src/cares_wrap.h b/src/cares_wrap.h index fd66a67164b..7f297e74d9a 100644 --- a/src/cares_wrap.h +++ b/src/cares_wrap.h @@ -336,7 +336,7 @@ class QueryWrap final : public AsyncWrap { // No need to explicitly call ares_free_string here, // as it is a wrapper around free, which is already // invoked when MallocedBuffer is destructed. - ares_dns_write(dnsrec, &buf_copy, &answer_len); + status = ares_dns_write(dnsrec, &buf_copy, &answer_len); } wrap->response_data_ = std::make_unique(); diff --git a/test/parallel/test-dns-resolve-unwritable-reply.js b/test/parallel/test-dns-resolve-unwritable-reply.js new file mode 100644 index 00000000000..f8c2a666840 --- /dev/null +++ b/test/parallel/test-dns-resolve-unwritable-reply.js @@ -0,0 +1,35 @@ +'use strict'; +const common = require('../common'); +const dnstools = require('../common/dns'); +const assert = require('assert'); +const dgram = require('dgram'); +const dns = require('dns'); + +const server = dgram.createSocket('udp4'); + +server.on('message', common.mustCall((msg, { address, port }) => { + const parsed = dnstools.parseDNSPacket(msg); + const domain = parsed.questions[0].domain; + assert.strictEqual(domain, 'example.org'); + + server.send(dnstools.writeDNSPacket({ + id: parsed.id, + questions: parsed.questions, + answers: [{ type: 'A', address: '1.2.3.4', ttl: 123, domain }], + additionalRecords: [ + { type: 'A', address: '1.2.3.4', ttl: 123, domain: 'x y.example.org' }, + ], + }), port, address); +})); + +server.bind(0, common.mustCall(() => { + const resolver = new dns.Resolver({ timeout: 1000, tries: 1 }); + resolver.setServers([`127.0.0.1:${server.address().port}`]); + + resolver.resolve4('example.org', common.mustCall((err) => { + assert.strictEqual(err.code, 'EBADNAME'); + assert.strictEqual(err.syscall, 'queryA'); + assert.strictEqual(err.hostname, 'example.org'); + server.close(); + })); +}));