Ignore invalid entries in /etc/resolv.conf when parsing (#9697)
Motivation: We should just ignore (and so skip) invalid entries in /etc/resolver.conf. Modifications: - Skip invalid entries - Add unit test Result: Fix https://github.com/netty/netty/issues/9684
This commit is contained in:
parent
402537cab0
commit
3577a52809
@ -167,6 +167,7 @@ public final class UnixResolverDnsServerAddressStreamProvider implements DnsServ
|
|||||||
String line;
|
String line;
|
||||||
while ((line = br.readLine()) != null) {
|
while ((line = br.readLine()) != null) {
|
||||||
line = line.trim();
|
line = line.trim();
|
||||||
|
try {
|
||||||
char c;
|
char c;
|
||||||
if (line.isEmpty() || (c = line.charAt(0)) == '#' || c == ';') {
|
if (line.isEmpty() || (c = line.charAt(0)) == '#' || c == ';') {
|
||||||
continue;
|
continue;
|
||||||
@ -177,6 +178,7 @@ public final class UnixResolverDnsServerAddressStreamProvider implements DnsServ
|
|||||||
throw new IllegalArgumentException("error parsing label " + NAMESERVER_ROW_LABEL +
|
throw new IllegalArgumentException("error parsing label " + NAMESERVER_ROW_LABEL +
|
||||||
" in file " + etcResolverFile + ". value: " + line);
|
" in file " + etcResolverFile + ". value: " + line);
|
||||||
}
|
}
|
||||||
|
|
||||||
String maybeIP = line.substring(i);
|
String maybeIP = line.substring(i);
|
||||||
// There may be a port appended onto the IP address so we attempt to extract it.
|
// There may be a port appended onto the IP address so we attempt to extract it.
|
||||||
if (!NetUtil.isValidIpV4Address(maybeIP) && !NetUtil.isValidIpV6Address(maybeIP)) {
|
if (!NetUtil.isValidIpV4Address(maybeIP) && !NetUtil.isValidIpV6Address(maybeIP)) {
|
||||||
@ -208,7 +210,10 @@ public final class UnixResolverDnsServerAddressStreamProvider implements DnsServ
|
|||||||
}
|
}
|
||||||
port = Integer.parseInt(line.substring(i));
|
port = Integer.parseInt(line.substring(i));
|
||||||
} else if (line.startsWith(SORTLIST_ROW_LABEL)) {
|
} else if (line.startsWith(SORTLIST_ROW_LABEL)) {
|
||||||
logger.info("row type {} not supported. ignoring line: {}", SORTLIST_ROW_LABEL, line);
|
logger.info("row type {} not supported. Ignoring line: {}", SORTLIST_ROW_LABEL, line);
|
||||||
|
}
|
||||||
|
} catch (IllegalArgumentException e) {
|
||||||
|
logger.warn("Could not parse entry. Ignoring line: {}", line, e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (!addresses.isEmpty()) {
|
if (!addresses.isEmpty()) {
|
||||||
|
@ -164,6 +164,19 @@ public class UnixResolverDnsServerAddressStreamProviderTest {
|
|||||||
assertEquals(Collections.singletonList("squarecorp.local"), domains);
|
assertEquals(Collections.singletonList("squarecorp.local"), domains);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void ignoreInvalidEntries() throws Exception {
|
||||||
|
File f = buildFile("domain netty.local\n" +
|
||||||
|
"nameserver nil\n" +
|
||||||
|
"nameserver 127.0.0.3\n");
|
||||||
|
UnixResolverDnsServerAddressStreamProvider p =
|
||||||
|
new UnixResolverDnsServerAddressStreamProvider(f, null);
|
||||||
|
|
||||||
|
DnsServerAddressStream stream = p.nameServerAddressStream("somehost");
|
||||||
|
assertEquals(1, stream.size());
|
||||||
|
assertHostNameEquals("127.0.0.3", stream.next());
|
||||||
|
}
|
||||||
|
|
||||||
private File buildFile(String contents) throws IOException {
|
private File buildFile(String contents) throws IOException {
|
||||||
File f = folder.newFile();
|
File f = folder.newFile();
|
||||||
OutputStream out = new FileOutputStream(f);
|
OutputStream out = new FileOutputStream(f);
|
||||||
|
Loading…
Reference in New Issue
Block a user