Avoid casting gymnastics that lead to pointer aliasing by introducing an

inline function.
This commit is contained in:
christos 2013-10-17 23:58:05 +00:00
parent b9ba14885c
commit fc8781bab9
1 changed files with 36 additions and 18 deletions

View File

@ -1,4 +1,4 @@
/* $NetBSD: clnt_vc.c,v 1.23 2013/05/07 21:08:45 christos Exp $ */
/* $NetBSD: clnt_vc.c,v 1.24 2013/10/17 23:58:05 christos Exp $ */
/*
* Copyright (c) 2010, Oracle America, Inc.
@ -38,7 +38,7 @@ static char *sccsid = "@(#)clnt_tcp.c 1.37 87/10/05 Copyr 1984 Sun Micro";
static char *sccsid = "@(#)clnt_tcp.c 2.2 88/08/01 4.0 RPCSRC";
static char sccsid[] = "@(#)clnt_vc.c 1.19 89/03/16 Copyr 1988 Sun Micro";
#else
__RCSID("$NetBSD: clnt_vc.c,v 1.23 2013/05/07 21:08:45 christos Exp $");
__RCSID("$NetBSD: clnt_vc.c,v 1.24 2013/10/17 23:58:05 christos Exp $");
#endif
#endif
@ -144,6 +144,33 @@ static cond_t *vc_cv;
#define __rpc_lock_value 0
#endif
static __inline void
htonlp(void *dst, const void *src)
{
#if 0
uint32_t tmp;
memcpy(&tmp, src, sizeof(tmp));
tmp = htonl(tmp);
memcpy(dst, &tmp, sizeof(tmp));
#else
/* We are aligned, so we think */
*(uint32_t *)dst = htonl(*(const uint32_t *)src);
#endif
}
static __inline void
ntohlp(void *dst, const void *src)
{
#if 0
uint32_t tmp;
memcpy(&tmp, src, sizeof(tmp));
tmp = ntohl(tmp);
memcpy(dst, &tmp, sizeof(tmp));
#else
/* We are aligned, so we think */
*(uint32_t *)dst = htonl(*(const uint32_t *)src);
#endif
}
/*
* Create a client handle for a connection.
@ -578,13 +605,12 @@ clnt_vc_control(
* first element in the call structure
* This will get the xid of the PREVIOUS call
*/
*(u_int32_t *)(void *)info =
ntohl(*(u_int32_t *)(void *)&ct->ct_u.ct_mcalli);
ntohlp(info, &ct->ct_u.ct_mcalli);
break;
case CLSET_XID:
/* This will set the xid of the NEXT call */
*(u_int32_t *)(void *)&ct->ct_u.ct_mcalli =
htonl(*((u_int32_t *)(void *)info) + 1);
htonlp(&ct->ct_u.ct_mcalli, (const char *)info +
sizeof(uint32_t));
/* increment by 1 as clnt_vc_call() decrements once */
break;
case CLGET_VERS:
@ -594,15 +620,11 @@ clnt_vc_control(
* begining of the RPC header. MUST be changed if the
* call_struct is changed
*/
*(u_int32_t *)(void *)info =
ntohl(*(u_int32_t *)(void *)(ct->ct_u.ct_mcallc +
4 * BYTES_PER_XDR_UNIT));
ntohlp(info, ct->ct_u.ct_mcallc + 4 * BYTES_PER_XDR_UNIT);
break;
case CLSET_VERS:
*(u_int32_t *)(void *)(ct->ct_u.ct_mcallc +
4 * BYTES_PER_XDR_UNIT) =
htonl(*(u_int32_t *)(void *)info);
htonlp(ct->ct_u.ct_mcallc + 4 * BYTES_PER_XDR_UNIT, info);
break;
case CLGET_PROG:
@ -612,15 +634,11 @@ clnt_vc_control(
* begining of the RPC header. MUST be changed if the
* call_struct is changed
*/
*(u_int32_t *)(void *)info =
ntohl(*(u_int32_t *)(void *)(ct->ct_u.ct_mcallc +
3 * BYTES_PER_XDR_UNIT));
ntohlp(info, ct->ct_u.ct_mcallc + 3 * BYTES_PER_XDR_UNIT);
break;
case CLSET_PROG:
*(u_int32_t *)(void *)(ct->ct_u.ct_mcallc +
3 * BYTES_PER_XDR_UNIT) =
htonl(*(u_int32_t *)(void *)info);
htonlp(ct->ct_u.ct_mcallc + 3 * BYTES_PER_XDR_UNIT, info);
break;
default: