Skip to content

Commit 23e9e2e

Browse files
Fix cpnet_getHostByName return value on error
In platforms lacking gethostbyname_r, such as Darwin (macOS), cpnet_getHostByName incorrectly returns -errno when gethostbyname fails. It should return -h_errno instead. Since errno is not set by gethostbyname, the value returned by cpnet_getHostByName is undefined at that point. It could be 0 or a stale value from a previous unrelated system call. If cpnet_getHostByName returns 0 (CPNATIVE_OK) the caller (Java_java_net_VMInetAddress_getHostByName) then proceeds to access the addresses pointer, which was never initialized, typically leading to a crash (SIGBUS/SIGSEGV). Fix by returning -h_errno on error. Fixes #37 (BZ#124082) Signed-off-by: Guillermo Rodríguez <grodriguez@ingelabs.com>
1 parent b1510ee commit 23e9e2e

1 file changed

Lines changed: 1 addition & 1 deletion

File tree

native/jni/native-lib/cpnet.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -661,7 +661,7 @@ jint cpnet_getHostByName (JNIEnv *env, const char *hostname, cpnet_address ***ad
661661

662662
result = gethostbyname (hostname);
663663
if (result == NULL)
664-
return -errno;
664+
return -h_errno;
665665
memcpy (&hret, result, sizeof (struct hostent));
666666
#endif
667667
if (ret != 0 || result == NULL)

0 commit comments

Comments
 (0)