You are viewing a plain text version of this content. The canonical link for it is here.
Posted to common-commits@hadoop.apache.org by si...@apache.org on 2026/10/27 20:47:15 UTC
(hadoop) 03/06: HADOOP-20020. DNS.reverseDns throws NullPointerException when the answer has no PTR record (#8820)
This is an automated email from the ASF dual-hosted git repository.
smengcl pushed a commit to branch branch-3.4
in repository https://gitbox.apache.org/repos/asf/hadoop.git
commit 3e7c7c83abd72b7a06287b8b0746f068fb85a3ef
Author: Siyao Meng <50...@users.noreply.github.com>
AuthorDate: Wed Oct 14 23:38:24 2026 -0700
HADOOP-20020. DNS.reverseDns throws NullPointerException when the answer has no PTR record (#8820)
Co-authored-by: Claude Opus 5.5 <no...@anthropic.com>
(cherry picked from commit df8e2a775ffac80e37ca8df09a7e4fa50b12a8dc)
---
.../src/main/java/org/apache/hadoop/net/DNS.java | 9 +++-
.../test/java/org/apache/hadoop/net/TestDNS.java | 57 ++++++++++++++++++++++
2 files changed, 65 insertions(+), 1 deletion(-)
diff --git a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/net/DNS.java b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/net/DNS.java
index b8ad1e62fdb..8cf56f0ca21 100644
--- a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/net/DNS.java
+++ b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/net/DNS.java
@@ -38,7 +38,9 @@
import java.util.Vector;
import javax.annotation.Nullable;
+import javax.naming.NameNotFoundException;
import javax.naming.NamingException;
+import javax.naming.directory.Attribute;
import javax.naming.directory.Attributes;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;
@@ -89,7 +91,12 @@ public static String reverseDns(InetAddress hostIp, @Nullable String ns)
ictx.close();
}
- String hostname = attribute.get("PTR").get().toString();
+ Attribute ptr = attribute.get("PTR");
+ if (ptr == null || ptr.size() == 0) {
+ // The server answered, but without a PTR record.
+ throw new NameNotFoundException("No PTR record for " + reverseIP);
+ }
+ String hostname = ptr.get().toString();
int hostnameLength = hostname.length();
if (hostname.charAt(hostnameLength - 1) == '.') {
hostname = hostname.substring(0, hostnameLength - 1);
diff --git a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/net/TestDNS.java b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/net/TestDNS.java
index fd1383129ad..162abdfa125 100644
--- a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/net/TestDNS.java
+++ b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/net/TestDNS.java
@@ -18,11 +18,16 @@
package org.apache.hadoop.net;
+import java.io.IOException;
+import java.io.UncheckedIOException;
+import java.net.DatagramPacket;
+import java.net.DatagramSocket;
import java.net.Inet6Address;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.net.InetAddress;
+import java.util.Arrays;
import javax.naming.CommunicationException;
import javax.naming.NameNotFoundException;
@@ -220,6 +225,58 @@ public void testReverseDnsIPv6WithInvalidServer() throws Exception {
.isInstanceOf(NamingException.class);
}
+ /**
+ * Test that the reverse lookup fails with a NamingException, which the
+ * callers handle, when the DNS server answers without a PTR record.
+ */
+ @Test
+ public void testReverseDnsWithoutPtrRecord() throws Exception {
+ try (DatagramSocket server = new DatagramSocket(0, InetAddress.getByName("127.0.0.1"))) {
+ Thread responder = new Thread(() -> answerWithoutRecords(server));
+ responder.setDaemon(true);
+ responder.start();
+ String ns = "127.0.0.1:" + server.getLocalPort();
+ for (String ip : new String[] {"192.0.2.1", "2001:db8::1"}) {
+ assertThatThrownBy(() -> DNS.reverseDns(InetAddress.getByName(ip), ns))
+ .isInstanceOf(NameNotFoundException.class)
+ .hasMessageContaining("No PTR record");
+ }
+ }
+ }
+
+ /**
+ * Answers every DNS query with NOERROR and no records, until the socket
+ * is closed.
+ */
+ private static void answerWithoutRecords(DatagramSocket server) {
+ byte[] buf = new byte[512];
+ try {
+ while (true) {
+ DatagramPacket query = new DatagramPacket(buf, buf.length);
+ server.receive(query);
+ // Keep the header and the question, drop anything after the question.
+ int length = query.getLength();
+ int end = 12;
+ while (end < length && buf[end] != 0) {
+ end += (buf[end] & 0xff) + 1;
+ }
+ end += 5; // the root label, QTYPE and QCLASS
+ if (end > length) {
+ continue; // not a well formed query
+ }
+ byte[] answer = Arrays.copyOf(buf, end);
+ answer[2] = (byte) 0x81; // QR and RD
+ answer[3] = (byte) 0x80; // RA, RCODE 0
+ Arrays.fill(answer, 6, 12, (byte) 0); // no answer, authority or additional records
+ server.send(new DatagramPacket(answer, end, query.getSocketAddress()));
+ }
+ } catch (IOException e) {
+ if (!server.isClosed()) {
+ throw new UncheckedIOException(e);
+ }
+ }
+ }
+
/**
* Test that when using an invalid DNS server with hosts file fallback,
* we are able to get the hostname from the hosts file.
---------------------------------------------------------------------
To unsubscribe, e-mail: common-commits-unsubscribe@hadoop.apache.org
For additional commands, e-mail: common-commits-help@hadoop.apache.org