Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-7022386

gethostbyname_r needs a pointer aligned buffer passed to it

XMLWordPrintable

    • b134
    • generic
    • generic
    • Verified

      In Inet4AddressImpl.c the functions getLocalHostName and lookupAllHostAddr call gethostbyname_r passing in a buffer declared as a simple char array:

      char buf[HENT_BUF_SIZE];

      gethostbyname_r will use this buffer to get the requested info and will then update the hostent structure it was also passed, to point to that buffer. In effect given the hostent struct contains:

                char **h_addr_list; /* list of addresses */

      the code will do:

            h->h_addr_list = buf

      This requires that buf be pointer-aligned (as it is expected to contain pointers), but the char[] declaration means that buf may only be byte-aligned, and hence on an architecture that requires pointer alignment we can get an access error due to the unaligned pointer access when h is passed to another function.

      The fix is to declare buf as being an array of pointers:

      char *buf[HENT_BUF_SIZE/(sizeof (char *))];

      and to cast it back to char* when passing into other functions.

            dholmes David Holmes
            dholmes David Holmes
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: