From 59d5bbbb1a012c1dd2201eb7bee5e36d6b32491c Mon Sep 17 00:00:00 2001 From: Joel Dice Date: Wed, 18 Dec 2013 10:43:11 -0700 Subject: [PATCH] throw UnknownHostException if host is not found in InetAddress.getByName --- classpath/java-net.cpp | 18 ++++++++++-------- test/Misc.java | 8 ++++++++ 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/classpath/java-net.cpp b/classpath/java-net.cpp index b92ed16935..eefbfced03 100644 --- a/classpath/java-net.cpp +++ b/classpath/java-net.cpp @@ -9,8 +9,8 @@ details. */ #include "jni.h" +#include "jni-util.h" #include "avian/machine.h" - #include "sockets.h" using namespace avian::classpath::sockets; @@ -88,7 +88,8 @@ Java_java_net_InetAddress_ipv4AddressForName(JNIEnv* e, if (host) { return ntohl(reinterpret_cast(host->h_addr_list[0])->s_addr); } else { - fprintf(stderr, "trouble %d\n", WSAGetLastError()); + throwNew(e, "java/net/UnknownHostException", 0); + return 0; } #else addrinfo hints; @@ -100,19 +101,20 @@ Java_java_net_InetAddress_ipv4AddressForName(JNIEnv* e, int r = getaddrinfo(chars, 0, &hints, &result); e->ReleaseStringUTFChars(name, chars); - jint address; if (r != 0) { - address = 0; + throwNew(e, "java/net/UnknownHostException", 0); + return 0; } else { - address = ntohl + int address = ntohl (reinterpret_cast(result->ai_addr)->sin_addr.s_addr); freeaddrinfo(result); + return address; } - - return address; #endif + } else { + throwNew(e, "java/lang/OutOfMemoryError", 0); + return 0; } - return 0; } diff --git a/test/Misc.java b/test/Misc.java index 0c91c5cb5b..276fb56c21 100644 --- a/test/Misc.java +++ b/test/Misc.java @@ -291,6 +291,14 @@ public class Misc { expect(java.util.Arrays.equals (new byte[] { 0, 0, 0, 0 }, java.net.InetAddress.getByName("0.0.0.0").getAddress())); + + try { + java.net.InetAddress.getByName + ("bs.thisdomaindoesntexistseriouslynoway"); + throw new AssertionError(); + } catch (java.net.UnknownHostException e) { + // cool + } } protected class Protected { }