[PATCH]: Portability fixes

Martin Buchholz martinrb at google.com
Wed Jan 28 20:16:56 UTC 2009


Christos, thanks for the bug report.

For low level code like this, it's almost always a mistake
(my mistake, that is; I am the author)
to use the locale-dependent functions like isdigit.
Still, I am surprised to see a real failure with  isdigit(negative char).

I prefer the following fix for UNIXProcess_md.c:
(and would be willing to submit it on Christos' behalf)

(I'm not going to comment on the changes to AddressImpl.c,
except to suggest considering ASCII-specific functions as well.)

diff --git a/src/solaris/native/java/lang/UNIXProcess_md.c
b/src/solaris/native/java/lang/UNIXProcess_md.c
--- a/src/solaris/native/java/lang/UNIXProcess_md.c
+++ b/src/solaris/native/java/lang/UNIXProcess_md.c
@@ -260,6 +260,12 @@
 }

 static int
+isAsciiDigit(char c)
+{
+  return c >= '0' && c <= '9';
+}
+
+static int
 closeDescriptors(void)
 {
     DIR *dp;
@@ -284,7 +290,7 @@
      */
     while ((dirp = readdir64(dp)) != NULL) {
         int fd;
-        if (isdigit(dirp->d_name[0]) &&
+        if (isAsciiDigit(dirp->d_name[0]) &&
             (fd = strtol(dirp->d_name, NULL, 10)) >= from_fd + 2)
             close(fd);
     }


Martin

On Wed, Jan 28, 2009 at 11:44, Christos Zoulas <christos at zoulas.com> wrote:

> Hello,
>
> Here are some simple changes for your consideration:
>
> 1. passing possibly negative values to isdigit is undefined behavior:
>   http://www.opengroup.org/onlinepubs/009695399/functions/isdigit.html
> 2. passing possibly negative values to isspace is undefined behavior:
>   http://www.opengroup.org/onlinepubs/009695399/functions/isspace.html
> 3. last is possibly uninitialized.
> 4. recvfrom argument should be socklen_t not int:
>   http://www.opengroup.org/onlinepubs/007908775/xns/recvfrom.html
>
> Thanks,
>
> christos
>
> diff -r fc30e7f4b9b3 src/solaris/native/java/lang/UNIXProcess_md.c
> --- a/src/solaris/native/java/lang/UNIXProcess_md.c     Fri Jan 16 11:24:18
> 2009 -0500
> +++ b/src/solaris/native/java/lang/UNIXProcess_md.c     Mon Jan 22 16:25:36
> 2009 -0500
> @@ -377,7 +377,7 @@
>      */
>     while ((dirp = readdir(dp)) != NULL) {
>         int fd;
> -        if (isdigit(dirp->d_name[0]) &&
> +        if (isdigit((unsigned char)dirp->d_name[0]) &&
>             (fd = strtol(dirp->d_name, NULL, 10)) >= from_fd + 2)
>             close(fd);
>     }
> diff -r fc30e7f4b9b3 src/solaris/native/java/net/Inet4AddressImpl.c
> --- a/src/solaris/native/java/net/Inet4AddressImpl.c    Fri Jan 16 11:24:18
> 2009 -0500
> +++ b/src/solaris/native/java/net/Inet4AddressImpl.c    Mon Jan 22 16:25:36
> 2009 -0500
> @@ -155,7 +155,7 @@
>      * Workaround for Solaris bug 4160367 - if a hostname contains a
>      * white space then 0.0.0.0 is returned
>      */
> -    if (isspace(hostname[0])) {
> +    if (isspace((unsigned char)hostname[0])) {
>        JNU_ThrowByName(env, JNU_JAVANETPKG "UnknownHostException",
>                        (char *)hostname);
>        JNU_ReleaseStringPlatformChars(env, host, hostname);
> @@ -172,7 +172,7 @@
>        return NULL;
>     } else {
>        int i = 0;
> -       struct addrinfo *itr, *last, *iterator = res;
> +       struct addrinfo *itr, *last = NULL, *iterator = res;
>        while (iterator != NULL) {
>            int skip = 0;
>            itr = resNew;
> @@ -603,7 +603,8 @@
>  ping4(JNIEnv *env, jint fd, struct sockaddr_in* him, jint timeout,
>       struct sockaddr_in* netif, jint ttl) {
>     jint size;
> -    jint n, len, hlen1, icmplen;
> +    jint n, hlen1, icmplen;
> +    socklen_t len;
>     char sendbuf[1500];
>     char recvbuf[1500];
>     struct icmp *icmp;
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.openjdk.java.net/pipermail/core-libs-dev/attachments/20090128/cb67d5e2/attachment.html>


More information about the core-libs-dev mailing list