From 3750520f6f49438bd5c00dceef70e71a2b752ae3 Mon Sep 17 00:00:00 2001
From: Hallvard Furuseth <hallvard@openldap.org>
Date: Sat, 9 Jun 2007 23:35:20 +0000
Subject: [PATCH] Fix HP-UX crash: Replace socklen_t with ber_socklen_t from
 configure. ITS#4629.

---
 configure.in                    | 39 ++++++++++++++++++++++++++++-----
 libraries/liblber/sockbuf.c     |  2 +-
 libraries/libldap/os-ip.c       |  8 +++----
 libraries/libldap/os-local.c    |  6 ++---
 libraries/liblutil/getpeereid.c |  8 +++----
 servers/slapd/daemon.c          |  2 +-
 6 files changed, 47 insertions(+), 18 deletions(-)

diff --git a/configure.in b/configure.in
index 28edb23f54..8793a97b4e 100644
--- a/configure.in
+++ b/configure.in
@@ -2133,14 +2133,43 @@ AC_CHECK_TYPE(size_t, unsigned)
 AC_CHECK_TYPES([long long])
 AC_CHECK_TYPES([ptrdiff_t])
 
-AC_CHECK_TYPE([socklen_t],,
-	[AC_DEFINE_UNQUOTED([socklen_t], [int],
-		[Define to `int' if <sys/socket.h> does not define.])],
-	[$ac_includes_default
+
+AC_CHECK_TYPE([socklen_t],,, [$ac_includes_default
+#ifdef HAVE_SYS_SOCKET_H
+#include <sys/socket.h>
+#endif])
+
+dnl socklen_t-like type in accept(), default socklen_t or int:
+dnl - The OS might define socklen_t without using it.  POSIX moved from
+dnl   int to size_t to socklen_t, hoping to stay at a 32-bit type, and
+dnl   HP-UX now has selectors for what to use.
+dnl - On Solaris 2.8 the prototype has void *len, but the default is OK.
+AC_MSG_CHECKING([the type of arg 3 to accept()])
+AC_CACHE_VAL(ol_cv_type_ber_socklen_t, [
+	set socklen_t int unsigned "unsigned long" long size_t
+	test "$ac_cv_type_socklen_t" = yes || shift
+	ol_cv_type_ber_socklen_t=$1 guessing="guessing "
+	for lentype in "$@" ; do for addrtype in "struct sockaddr" void ; do
+		AC_COMPILE_IFELSE([AC_LANG_PROGRAM([$ac_includes_default
 #ifdef HAVE_SYS_SOCKET_H
 #include <sys/socket.h>
 #endif
-	])
+extern int accept(int s, $addrtype *ap, $lentype *lp);
+], [
+accept(0, (struct sockaddr *) 0, ($lentype *) 0);
+])], [ol_cv_type_ber_socklen_t=$lentype guessing= ; break 2])
+	done ; done])
+AC_MSG_RESULT([$guessing$ol_cv_type_ber_socklen_t *])
+AC_DEFINE_UNQUOTED(ber_socklen_t, $ol_cv_type_ber_socklen_t,
+	[Define to the type of arg 3 for `accept'.])
+
+dnl Modules should use ber_socklen_t, not socklen_t.  Define socklen_t
+dnl for the time being anyway, for backwards compatibility.
+if test "$ac_cv_type_socklen_t" != yes; then
+	AC_DEFINE_UNQUOTED([socklen_t], [$ol_cv_type_ber_socklen_t],
+		[Define like ber_socklen_t if <sys/socket.h> does not define.])
+fi
+
 
 AC_TYPE_SIGNAL
 
diff --git a/libraries/liblber/sockbuf.c b/libraries/liblber/sockbuf.c
index 25403e16b2..9659d6a6f9 100644
--- a/libraries/liblber/sockbuf.c
+++ b/libraries/liblber/sockbuf.c
@@ -905,7 +905,7 @@ static ber_slen_t
 sb_dgram_read( Sockbuf_IO_Desc *sbiod, void *buf, ber_len_t len )
 {
 	ber_slen_t rc;
-	socklen_t  addrlen;
+	ber_socklen_t addrlen;
 	struct sockaddr *src;
    
 	assert( sbiod != NULL );
diff --git a/libraries/libldap/os-ip.c b/libraries/libldap/os-ip.c
index 4b7de9e690..60aa94ae3f 100644
--- a/libraries/libldap/os-ip.c
+++ b/libraries/libldap/os-ip.c
@@ -174,7 +174,7 @@ ldap_pvt_is_socket_ready(LDAP *ld, int s)
 #if defined( notyet ) /* && defined( SO_ERROR ) */
 {
 	int so_errno;
-	socklen_t dummy = sizeof(so_errno);
+	ber_socklen_t dummy = sizeof(so_errno);
 	if ( getsockopt( s, SOL_SOCKET, SO_ERROR, &so_errno, &dummy )
 		== AC_SOCKET_ERROR )
 	{
@@ -196,7 +196,7 @@ ldap_pvt_is_socket_ready(LDAP *ld, int s)
 	struct sockaddr_in sin;
 #endif
 	char ch;
-	socklen_t dummy = sizeof(sin);
+	ber_socklen_t dummy = sizeof(sin);
 	if ( getpeername( s, (struct sockaddr *) &sin, &dummy )
 		== AC_SOCKET_ERROR )
 	{
@@ -350,7 +350,7 @@ ldap_int_poll(
 
 static int
 ldap_pvt_connect(LDAP *ld, ber_socket_t s,
-	struct sockaddr *sin, socklen_t addrlen,
+	struct sockaddr *sin, ber_socklen_t addrlen,
 	int async)
 {
 	int rc, err;
@@ -621,7 +621,7 @@ ldap_connect_to_host(LDAP *ld, Sockbuf *sb,
 char *
 ldap_host_connected_to( Sockbuf *sb, const char *host )
 {
-	socklen_t		len;
+	ber_socklen_t	len;
 #ifdef LDAP_PF_INET6
 	struct sockaddr_storage sabuf;
 #else
diff --git a/libraries/libldap/os-local.c b/libraries/libldap/os-local.c
index 52f6745c5e..fbec2273e1 100644
--- a/libraries/libldap/os-local.c
+++ b/libraries/libldap/os-local.c
@@ -120,7 +120,7 @@ ldap_pvt_is_socket_ready(LDAP *ld, int s)
 #if defined( notyet ) /* && defined( SO_ERROR ) */
 {
 	int so_errno;
-	socklen_t dummy = sizeof(so_errno);
+	ber_socklen_t dummy = sizeof(so_errno);
 	if ( getsockopt( s, SOL_SOCKET, SO_ERROR, &so_errno, &dummy )
 		== AC_SOCKET_ERROR )
 	{
@@ -138,7 +138,7 @@ ldap_pvt_is_socket_ready(LDAP *ld, int s)
 	/* error slippery */
 	struct sockaddr_un sa;
 	char ch;
-	socklen_t dummy = sizeof(sa);
+	ber_socklen_t dummy = sizeof(sa);
 	if ( getpeername( s, (struct sockaddr *) &sa, &dummy )
 		== AC_SOCKET_ERROR )
 	{
@@ -189,7 +189,7 @@ ldap_pvt_connect(LDAP *ld, ber_socket_t s, struct sockaddr_un *sa, int async)
 sendcred:
 		{
 			int fds[2];
-			socklen_t salen = sizeof(*sa);
+			ber_socklen_t salen = sizeof(*sa);
 			if (pipe(fds) == 0) {
 				/* Abandon, noop, has no reply */
 				struct iovec iov;
diff --git a/libraries/liblutil/getpeereid.c b/libraries/liblutil/getpeereid.c
index 4619c280be..b96d8c808f 100644
--- a/libraries/liblutil/getpeereid.c
+++ b/libraries/liblutil/getpeereid.c
@@ -62,7 +62,7 @@ int lutil_getpeereid( int s, uid_t *euid, gid_t *egid
 
 #elif defined( SO_PEERCRED )
 	struct ucred peercred;
-	socklen_t peercredlen = sizeof peercred;
+	ber_socklen_t peercredlen = sizeof peercred;
 
 	if(( getsockopt( s, SOL_SOCKET, SO_PEERCRED,
 		(void *)&peercred, &peercredlen ) == 0 )
@@ -75,7 +75,7 @@ int lutil_getpeereid( int s, uid_t *euid, gid_t *egid
 
 #elif defined( LOCAL_PEERCRED )
 	struct xucred peercred;
-	socklen_t peercredlen = sizeof peercred;
+	ber_socklen_t peercredlen = sizeof peercred;
 
 	if(( getsockopt( s, LOCAL_PEERCRED, 1,
 		(void *)&peercred, &peercredlen ) == 0 )
@@ -104,7 +104,7 @@ int lutil_getpeereid( int s, uid_t *euid, gid_t *egid
 # endif /* HAVE_STRUCT_MSGHDR_MSG_CONTROL */
 	struct stat st;
 	struct sockaddr_un lname, rname;
-	socklen_t llen, rlen;
+	ber_socklen_t llen, rlen;
 
 	rlen = sizeof(rname);
 	llen = sizeof(lname);
@@ -166,7 +166,7 @@ int lutil_getpeereid( int s, uid_t *euid, gid_t *egid
 	}
 #elif defined(SOCKCREDSIZE)
 	struct msghdr msg;
-	socklen_t crmsgsize;
+	ber_socklen_t crmsgsize;
 	void *crmsg;
 	struct cmsghdr *cmp;
 	struct sockcred *sc;
diff --git a/servers/slapd/daemon.c b/servers/slapd/daemon.c
index 61c6b5935b..81f8d351c9 100644
--- a/servers/slapd/daemon.c
+++ b/servers/slapd/daemon.c
@@ -1571,7 +1571,7 @@ slap_listener(
 	Sockaddr		from;
 
 	ber_socket_t s;
-	socklen_t len = sizeof(from);
+	ber_socklen_t len = sizeof(from);
 	Connection *c;
 	slap_ssf_t ssf = 0;
 	struct berval authid = BER_BVNULL;
-- 
GitLab