Hi, Here are some fixes for the NATIVE-LAYER branch concerning the exactness of exception which should be thrown.
Regards, Guilhem. 2006-07-09 Guilhem Lavaux <[EMAIL PROTECTED]> * native/jni/java-net/gnu_java_net_VMPlainDatagramSocketImpl.c (nativeReceive): Fixed the type of the arrays (use java types). (nativeSendTo): Force throwing an exception if port is 0. * native/jni/java-net/javanet.c: (_javanet_accept): Throw SocketTimeoutException if ETIMEDOUT is returned. (_javanet_recvfrom): Likewise. (_javanet_sendto): Throw a NullPointerException if the socket is not connected and no address is given. * native/jni/java-net/javanet.h (NULL_EXCEPTION): Defined.
Index: native/jni/java-net/gnu_java_net_VMPlainDatagramSocketImpl.c =================================================================== RCS file: /cvsroot/classpath/classpath/native/jni/java-net/gnu_java_net_VMPlainDatagramSocketImpl.c,v retrieving revision 1.5.2.1 diff -u -r1.5.2.1 gnu_java_net_VMPlainDatagramSocketImpl.c --- native/jni/java-net/gnu_java_net_VMPlainDatagramSocketImpl.c 28 Jan 2006 16:38:25 -0000 1.5.2.1 +++ native/jni/java-net/gnu_java_net_VMPlainDatagramSocketImpl.c 9 Jul 2006 16:23:20 -0000 @@ -185,20 +185,20 @@ jintArray receivedLength) { #ifndef WITHOUT_NETWORK - int *port, *bytes_read; + jint *port, *bytes_read; cpnet_address *addr; jbyte *addressBytes; addr = 0; - port = (int*)(*env)->GetIntArrayElements(env, receivedFromPort, NULL); + port = (jint*)(*env)->GetIntArrayElements(env, receivedFromPort, NULL); if (port == NULL) { JCL_ThrowException(env, IO_EXCEPTION, "Internal error: could not access receivedFromPort array"); return; } - bytes_read = (int*)(*env)->GetIntArrayElements(env, receivedLength, NULL); + bytes_read = (jint*)(*env)->GetIntArrayElements(env, receivedLength, NULL); if (bytes_read == NULL) { (*env)->ReleaseIntArrayElements(env, receivedFromPort, (jint*)port, 0); @@ -215,7 +215,6 @@ if (length == 0 && (*bytes_read) == -1) *bytes_read = 0; - if ((*bytes_read) == -1) { (*env)->ReleaseIntArrayElements(env, receivedFromPort, (jint*)port, 0); @@ -224,6 +223,9 @@ return; } + if ((*env)->ExceptionOccurred(env)) + return; + *port = cpnet_addressGetPort (addr); /* Store the address */ @@ -236,11 +238,6 @@ (*env)->ReleaseIntArrayElements(env, receivedFromPort, (jint*)port, 0); (*env)->ReleaseIntArrayElements(env, receivedLength, (jint*)bytes_read, 0); - if ((*env)->ExceptionOccurred(env)) - { - return; - } - DBG("PlainDatagramSocketImpl.receive(): Received packet\n"); #else /* not WITHOUT_NETWORK */ @@ -266,13 +263,20 @@ cpnet_address *netAddress; /* check if address given, tr 7.3.2005 */ - if (addr != NULL) + if (addr != NULL ) { netAddress = _javanet_get_ip_netaddr(env, addr); if ((*env)->ExceptionOccurred(env)) - { + { return; } + if (port == 0) + { + JCL_ThrowException(env, IO_EXCEPTION, + "Invalid port number"); + cpnet_freeAddress(env, netAddress); + return; + } cpnet_addressSetPort (netAddress, port); } else @@ -283,6 +287,8 @@ DBG("PlainDatagramSocketImpl.sendto(): have addr\n"); _javanet_sendto(env, obj, buf, offset, len, netAddress); + if (netAddress != NULL) + cpnet_freeAddress(env, netAddress); if ((*env)->ExceptionOccurred(env)) { return; Index: native/jni/java-net/javanet.c =================================================================== RCS file: /cvsroot/classpath/classpath/native/jni/java-net/javanet.c,v retrieving revision 1.32.2.10 diff -u -r1.32.2.10 javanet.c --- native/jni/java-net/javanet.c 16 Jun 2006 18:07:47 -0000 1.32.2.10 +++ native/jni/java-net/javanet.c 9 Jul 2006 16:23:20 -0000 @@ -800,9 +800,9 @@ result = cpnet_accept (env, fd, &newfd); if (result != CPNATIVE_OK && result != CPNATIVE_EINTR) { - if (result == ETIMEDOUT) + if (result == ETIMEDOUT || result == EAGAIN) JCL_ThrowException (env, "java/net/SocketTimeoutException", - "Timeout"); + "Accept operation timed out"); else JCL_ThrowException (env, IO_EXCEPTION, cpnative_getErrorString (result)); @@ -951,13 +951,11 @@ result = cpnet_recv (env, fd, p + offset, len, &received_bytes); } } - while ((received_bytes == -1) && - (result == CPNATIVE_EINTR)); - - if (received_bytes == -1) + while (result == CPNATIVE_EINTR); + if (result != 0) { - if (result == EAGAIN) - JCL_ThrowException (env, "java/net/SocketTimeoutException", "Timeout"); + if (result == EAGAIN || result == ETIMEDOUT) + JCL_ThrowException (env, "java/net/SocketTimeoutException", "Receive operation timed out"); else JCL_ThrowException (env, IO_EXCEPTION, cpnative_getErrorString (result)); @@ -980,7 +978,7 @@ if (received_bytes == 0) received_bytes = -1; - return (received_bytes); + return received_bytes; #else /* not WITHOUT_NETWORK */ #endif /* not WITHOUT_NETWORK */ } @@ -1024,7 +1022,7 @@ while (len > 0) { /* Send the data */ - if (addr == 0) + if (addr == NULL) { DBG ("_javanet_sendto(): Sending....\n"); result = cpnet_send (env, fd, p + offset, len, &bytes_sent); @@ -1035,6 +1033,13 @@ result = cpnet_sendTo (env, fd, p + offset, len, addr, &bytes_sent); } + if (result == EDESTADDRREQ) + { + JCL_ThrowException (env, NULL_EXCEPTION, + "Socket is not connected and no address is given"); + break; + } + if (bytes_sent < 0) { if (result != CPNATIVE_EINTR) Index: native/jni/java-net/javanet.h =================================================================== RCS file: /cvsroot/classpath/classpath/native/jni/java-net/javanet.h,v retrieving revision 1.12.2.3 diff -u -r1.12.2.3 javanet.h --- native/jni/java-net/javanet.h 6 Jun 2006 20:30:30 -0000 1.12.2.3 +++ native/jni/java-net/javanet.h 9 Jul 2006 16:23:20 -0000 @@ -55,6 +55,7 @@ #define CONNECT_EXCEPTION "java/net/ConnectException" #define SOCKET_EXCEPTION "java/net/SocketException" #define UNKNOWN_HOST_EXCEPTION "java/net/UnknownHostException" +#define NULL_EXCEPTION "java/lang/NullPointerException" /* Socket Option Identifiers - Don't change or binary compatibility with the JDK will be broken! These also need to