JDK-7022386 : gethostbyname_r needs a pointer aligned buffer passed to it
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.net
  • Affected Version: 7
  • Priority: P3
  • Status: Closed
  • Resolution: Fixed
  • OS: generic
  • CPU: generic
  • Submitted: 2011-02-25
  • Updated: 2011-03-17
  • Resolved: 2011-03-17
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 7
7 b134Fixed
Related Reports
Relates :  
Description
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.

Comments
EVALUATION See description.
28-02-2011