提交 60919efd 编写于 作者: D dholmes

7022386: gethostbyname_r needs a pointer aligned buffer passed to it

Summary: Change buffer type to ensure pointer-alignment
Reviewed-by: alanb, chegar
上级 19b2720e
/* /*
* Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -77,22 +77,24 @@ Java_java_net_Inet4AddressImpl_getLocalHostName(JNIEnv *env, jobject this) { ...@@ -77,22 +77,24 @@ Java_java_net_Inet4AddressImpl_getLocalHostName(JNIEnv *env, jobject this) {
*/ */
#endif /* __linux__ */ #endif /* __linux__ */
struct hostent res, res2, *hp; struct hostent res, res2, *hp;
char buf[HENT_BUF_SIZE]; // these buffers must be pointer-aligned so they are declared
char buf2[HENT_BUF_SIZE]; // with pointer type
char *buf[HENT_BUF_SIZE/(sizeof (char *))];
char *buf2[HENT_BUF_SIZE/(sizeof (char *))];
int h_error=0; int h_error=0;
#ifdef __GLIBC__ #ifdef __GLIBC__
gethostbyname_r(hostname, &res, buf, sizeof(buf), &hp, &h_error); gethostbyname_r(hostname, &res, (char*)buf, sizeof(buf), &hp, &h_error);
#else #else
hp = gethostbyname_r(hostname, &res, buf, sizeof(buf), &h_error); hp = gethostbyname_r(hostname, &res, (char*)buf, sizeof(buf), &h_error);
#endif #endif
if (hp) { if (hp) {
#ifdef __GLIBC__ #ifdef __GLIBC__
gethostbyaddr_r(hp->h_addr, hp->h_length, AF_INET, gethostbyaddr_r(hp->h_addr, hp->h_length, AF_INET,
&res2, buf2, sizeof(buf2), &hp, &h_error); &res2, (char*)buf2, sizeof(buf2), &hp, &h_error);
#else #else
hp = gethostbyaddr_r(hp->h_addr, hp->h_length, AF_INET, hp = gethostbyaddr_r(hp->h_addr, hp->h_length, AF_INET,
&res2, buf2, sizeof(buf2), &h_error); &res2, (char*)buf2, sizeof(buf2), &h_error);
#endif #endif
if (hp) { if (hp) {
/* /*
...@@ -136,7 +138,9 @@ Java_java_net_Inet4AddressImpl_lookupAllHostAddr(JNIEnv *env, jobject this, ...@@ -136,7 +138,9 @@ Java_java_net_Inet4AddressImpl_lookupAllHostAddr(JNIEnv *env, jobject this,
const char *hostname; const char *hostname;
jobjectArray ret = 0; jobjectArray ret = 0;
struct hostent res, *hp = 0; struct hostent res, *hp = 0;
char buf[HENT_BUF_SIZE]; // this buffer must be pointer-aligned so is declared
// with pointer type
char *buf[HENT_BUF_SIZE/(sizeof (char *))];
/* temporary buffer, on the off chance we need to expand */ /* temporary buffer, on the off chance we need to expand */
char *tmp = NULL; char *tmp = NULL;
...@@ -176,9 +180,9 @@ Java_java_net_Inet4AddressImpl_lookupAllHostAddr(JNIEnv *env, jobject this, ...@@ -176,9 +180,9 @@ Java_java_net_Inet4AddressImpl_lookupAllHostAddr(JNIEnv *env, jobject this,
/* Try once, with our static buffer. */ /* Try once, with our static buffer. */
#ifdef __GLIBC__ #ifdef __GLIBC__
gethostbyname_r(hostname, &res, buf, sizeof(buf), &hp, &h_error); gethostbyname_r(hostname, &res, (char*)buf, sizeof(buf), &hp, &h_error);
#else #else
hp = gethostbyname_r(hostname, &res, buf, sizeof(buf), &h_error); hp = gethostbyname_r(hostname, &res, (char*)buf, sizeof(buf), &h_error);
#endif #endif
/* With the re-entrant system calls, it's possible that the buffer /* With the re-entrant system calls, it's possible that the buffer
...@@ -251,7 +255,9 @@ Java_java_net_Inet4AddressImpl_getHostByAddr(JNIEnv *env, jobject this, ...@@ -251,7 +255,9 @@ Java_java_net_Inet4AddressImpl_getHostByAddr(JNIEnv *env, jobject this,
jstring ret = NULL; jstring ret = NULL;
jint addr; jint addr;
struct hostent hent, *hp = 0; struct hostent hent, *hp = 0;
char buf[HENT_BUF_SIZE]; // this buffer must be pointer-aligned so is declared
// with pointer type
char *buf[HENT_BUF_SIZE/(sizeof (char *))];
int h_error = 0; int h_error = 0;
char *tmp = NULL; char *tmp = NULL;
...@@ -273,10 +279,10 @@ Java_java_net_Inet4AddressImpl_getHostByAddr(JNIEnv *env, jobject this, ...@@ -273,10 +279,10 @@ Java_java_net_Inet4AddressImpl_getHostByAddr(JNIEnv *env, jobject this,
addr = htonl(addr); addr = htonl(addr);
#ifdef __GLIBC__ #ifdef __GLIBC__
gethostbyaddr_r((char *)&addr, sizeof(addr), AF_INET, &hent, gethostbyaddr_r((char *)&addr, sizeof(addr), AF_INET, &hent,
buf, sizeof(buf), &hp, &h_error); (char*)buf, sizeof(buf), &hp, &h_error);
#else #else
hp = gethostbyaddr_r((char *)&addr, sizeof(addr), AF_INET, &hent, hp = gethostbyaddr_r((char *)&addr, sizeof(addr), AF_INET, &hent,
buf, sizeof(buf), &h_error); (char*)buf, sizeof(buf), &h_error);
#endif #endif
/* With the re-entrant system calls, it's possible that the buffer /* With the re-entrant system calls, it's possible that the buffer
* we pass to it is not large enough to hold an exceptionally * we pass to it is not large enough to hold an exceptionally
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册