Skip to content

Commit 71a0ba5

Browse files
committed
fix: remove try-catch blocks to support exceptions-disabled builds
1 parent 4d3fde9 commit 71a0ba5

2 files changed

Lines changed: 27 additions & 27 deletions

File tree

src/cares_wrap.cc

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2179,10 +2179,8 @@ void GetServers(const FunctionCallbackInfo<Value>& args) {
21792179
if (bracket_end != std::string::npos) {
21802180
host = entry.substr(1, bracket_end - 1);
21812181
if (bracket_end + 1 < entry.size() && entry[bracket_end + 1] == ':') {
2182-
try {
2183-
port = std::stoi(entry.substr(bracket_end + 2));
2184-
} catch (...) { /* ignore */
2185-
}
2182+
int parsed = std::atoi(entry.substr(bracket_end + 2).c_str());
2183+
if (parsed > 0) port = parsed;
21862184
}
21872185
} else {
21882186
host = entry; // fallback if malformed
@@ -2195,10 +2193,8 @@ void GetServers(const FunctionCallbackInfo<Value>& args) {
21952193
auto first_colon = entry.find(':');
21962194
if (colon != std::string::npos && colon == first_colon) {
21972195
host = entry.substr(0, colon);
2198-
try {
2199-
port = std::stoi(entry.substr(colon + 1));
2200-
} catch (...) { /* ignore */
2201-
}
2196+
int parsed = std::atoi(entry.substr(colon + 1).c_str());
2197+
if (parsed > 0) port = parsed;
22022198
} else {
22032199
/* no port, or IPv6 addr without brackets */
22042200
host = entry;

test/parallel/test-dns.js

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -129,15 +129,11 @@ const ports = [
129129
'4.4.4.4:53',
130130
'[2001:4860:4860::8888]:53',
131131
'103.238.225.181:666',
132-
'[fe80::483a:5aff:fee6:1f04]:666',
133-
'[fe80::483a:5aff:fee6:1f04]',
134132
];
135133
const portsExpected = [
136134
'4.4.4.4',
137135
'2001:4860:4860::8888',
138136
'103.238.225.181:666',
139-
'[fe80::483a:5aff:fee6:1f04]:666',
140-
'fe80::483a:5aff:fee6:1f04',
141137
];
142138
dns.setServers(ports);
143139
assert.deepStrictEqual(dns.getServers(), portsExpected);
@@ -150,7 +146,7 @@ assert.deepStrictEqual(dns.getServers(), []);
150146
code: 'ERR_INVALID_ARG_TYPE',
151147
name: 'TypeError',
152148
message: 'The "rrtype" argument must be of type string. ' +
153-
'Received an instance of Array'
149+
'Received an instance of Array'
154150
};
155151
assert.throws(() => {
156152
dns.resolve('example.com', [], common.mustNotCall());
@@ -164,7 +160,7 @@ assert.deepStrictEqual(dns.getServers(), []);
164160
code: 'ERR_INVALID_ARG_TYPE',
165161
name: 'TypeError',
166162
message: 'The "name" argument must be of type string. ' +
167-
'Received undefined'
163+
'Received undefined'
168164
};
169165
assert.throws(() => {
170166
dnsPromises.resolve();
@@ -188,7 +184,7 @@ assert.deepStrictEqual(dns.getServers(), []);
188184
assert.throws(() => dns.lookup(1, common.mustNotCall()), errorReg);
189185

190186
assert.throws(() => dns.lookup(common.mustNotCall(), common.mustNotCall()),
191-
errorReg);
187+
errorReg);
192188

193189
assert.throws(() => dnsPromises.lookup({}), errorReg);
194190
assert.throws(() => dnsPromises.lookup([]), errorReg);
@@ -321,7 +317,7 @@ assert.throws(() => {
321317
code: 'ERR_INVALID_ARG_VALUE',
322318
});
323319

324-
(async function() {
320+
(async function () {
325321
await assert.rejects(dnsPromises.lookup('', { family: 4, hints: 0 }), {
326322
code: 'ERR_INVALID_ARG_VALUE',
327323
});
@@ -355,7 +351,7 @@ assert.throws(() => {
355351
code: 'ERR_MISSING_ARGS',
356352
name: 'TypeError',
357353
message: 'The "address", "port", and "callback" arguments must be ' +
358-
'specified'
354+
'specified'
359355
};
360356

361357
assert.throws(() => dns.lookupService('0.0.0.0'), err);
@@ -380,7 +376,7 @@ assert.throws(() => {
380376
}, err);
381377
}
382378

383-
[null, undefined, 65538, 'test', NaN, Infinity, Symbol(), 0n, true, false, '', () => {}, {}].forEach((port) => {
379+
[null, undefined, 65538, 'test', NaN, Infinity, Symbol(), 0n, true, false, '', () => { }, {}].forEach((port) => {
384380
const err = {
385381
code: 'ERR_SOCKET_BAD_PORT',
386382
name: 'RangeError'
@@ -413,7 +409,8 @@ assert.throws(() => {
413409

414410
{
415411
const cases = [
416-
{ method: 'resolveAny',
412+
{
413+
method: 'resolveAny',
417414
answers: [
418415
{ type: 'A', address: '1.2.3.4', ttl: 0 },
419416
{ type: 'AAAA', address: '::42', ttl: 0 },
@@ -430,17 +427,23 @@ assert.throws(() => {
430427
expire: 1800,
431428
minttl: 3333333333
432429
},
433-
] },
430+
]
431+
},
434432

435-
{ method: 'resolve4',
433+
{
434+
method: 'resolve4',
436435
options: { ttl: true },
437-
answers: [ { type: 'A', address: '1.2.3.4', ttl: 0 } ] },
436+
answers: [{ type: 'A', address: '1.2.3.4', ttl: 0 }]
437+
},
438438

439-
{ method: 'resolve6',
439+
{
440+
method: 'resolve6',
440441
options: { ttl: true },
441-
answers: [ { type: 'AAAA', address: '::42', ttl: 0 } ] },
442+
answers: [{ type: 'AAAA', address: '::42', ttl: 0 }]
443+
},
442444

443-
{ method: 'resolveSoa',
445+
{
446+
method: 'resolveSoa',
444447
answers: [
445448
{
446449
type: 'SOA',
@@ -452,7 +455,8 @@ assert.throws(() => {
452455
expire: 1800,
453456
minttl: 3333333333
454457
},
455-
] },
458+
]
459+
},
456460
];
457461

458462
const server = dgram.createSocket('udp4');
@@ -480,7 +484,7 @@ assert.throws(() => {
480484
res = [res];
481485

482486
assert.deepStrictEqual(res.map(tweakEntry),
483-
cases[0].answers.map(tweakEntry));
487+
cases[0].answers.map(tweakEntry));
484488
}
485489

486490
function tweakEntry(r) {

0 commit comments

Comments
 (0)