Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/lib/libc/rpc Avoid casting gymnastics that lead to pointer a...



details:   https://anonhg.NetBSD.org/src/rev/920781700897
branches:  trunk
changeset: 790596:920781700897
user:      christos <christos%NetBSD.org@localhost>
date:      Thu Oct 17 23:58:05 2013 +0000

description:
Avoid casting gymnastics that lead to pointer aliasing by introducing an
inline function.

diffstat:

 lib/libc/rpc/clnt_vc.c |  54 +++++++++++++++++++++++++++++++++----------------
 1 files changed, 36 insertions(+), 18 deletions(-)

diffs (105 lines):

diff -r 4f2fb3a590b2 -r 920781700897 lib/libc/rpc/clnt_vc.c
--- a/lib/libc/rpc/clnt_vc.c    Thu Oct 17 23:56:17 2013 +0000
+++ b/lib/libc/rpc/clnt_vc.c    Thu Oct 17 23:58:05 2013 +0000
@@ -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  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 @@
 #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 @@
                 * 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 @@
                 * 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 @@
                 * 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:



Home | Main Index | Thread Index | Old Index