tfix type-punned pointer warnings from gcc - plan9port - [fork] Plan 9 from user space
 (HTM) git clone git://src.adamsgaard.dk/plan9port
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) README
 (DIR) LICENSE
       ---
 (DIR) commit 0c9c620f39e56c42802504003fd05664aba670a4
 (DIR) parent 2fe14f1315eb8d3235ea8f978e5bd9444f467c2b
 (HTM) Author: Russ Cox <rsc@swtch.com>
       Date:   Wed, 10 Mar 2010 14:50:33 -0800
       
       fix type-punned pointer warnings from gcc
       
       R=rsc
       http://codereview.appspot.com/396042
       
       Diffstat:
         M src/cmd/acid/expr.c                 |       3 ++-
         M src/cmd/db/format.c                 |       5 +++--
         M src/cmd/vbackup/vnfs.c              |      19 ++++++++++++++-----
         M src/lib9/_p9dialparse.c             |       2 +-
         M src/lib9/sendfd.c                   |       2 +-
         M src/lib9/truerand.c                 |       4 +++-
         M src/libauthsrv/readnvram.c          |       6 ++----
         M src/libavl/avl.c                    |       4 ++++
         M src/libip/udp.c                     |       8 ++++----
         M src/libmach/Linux.c                 |       6 +++---
         M src/libmemdraw/draw.c               |      27 ++++++++++++++++++++-------
       
       11 files changed, 57 insertions(+), 29 deletions(-)
       ---
 (DIR) diff --git a/src/cmd/acid/expr.c b/src/cmd/acid/expr.c
       t@@ -855,11 +855,12 @@ ocand(Node *n, Node *res)
        {
                Node l, r;
        
       -        res->store.fmt = l.store.fmt;
       +        res->store.fmt = 'D';
                res->op = OCONST;
                res->type = TINT;
                res->store.u.ival = 0;
                expr(n->left, &l);
       +        res->store.fmt = l.store.fmt;
                if(bool(&l) == 0)
                        return;
                expr(n->right, &r);
 (DIR) diff --git a/src/cmd/db/format.c b/src/cmd/db/format.c
       t@@ -60,7 +60,7 @@ exform(int fcount, int prt, char *ifp, Map *map, int literal, int firstpass)
                char        *fp;
                char        c, modifier;
                int        i;
       -        ushort sh, *sp;
       +        ushort sh;
                uchar ch, *cp;
                Symbol s;
                char buf[512];
       t@@ -205,7 +205,8 @@ exform(int fcount, int prt, char *ifp, Map *map, int literal, int firstpass)
        
                        case 'R':
                                if (literal) {
       -                                sp = (u16int*)(void*)&dot;
       +                                u16int sp[2];
       +                                memmove(&sp, &dot, 4);
                                        dprint("%C%C", sp[0], sp[1]);
                                        endline();
                                        dotinc = 4;
 (DIR) diff --git a/src/cmd/vbackup/vnfs.c b/src/cmd/vbackup/vnfs.c
       t@@ -322,10 +322,14 @@ cryptinit(void)
        {
                uchar key[32], ivec[AESbsize];
                int i;
       +        u32int u32;
                
       -        *(u32int*)sessid = truerand();
       -        for(i=0; i<nelem(key); i+=4)
       -                *(u32int*)&key[i] = truerand();
       +        u32 = truerand();
       +        memmove(sessid, &u32, 4);
       +        for(i=0; i<nelem(key); i+=4) {
       +                u32 = truerand();
       +                memmove(key+i, &u32, 4);
       +        }
                for(i=0; i<nelem(ivec); i++)
                        ivec[i] = fastrand();
                setupAESstate(&aesstate, key, sizeof key, ivec);
       t@@ -793,6 +797,8 @@ cnodelookup(Ctree *t, Cnode **np, char *name)
        Nfs3Status
        cnodegetattr(Cnode *n, Nfs3Attr *attr)
        {
       +        uint64 u64;
       +
                memset(attr, 0, sizeof *attr);
                if(n->read){
                        attr->type = Nfs3FileReg;
       t@@ -805,7 +811,8 @@ cnodegetattr(Cnode *n, Nfs3Attr *attr)
                        attr->size = 1024;
                        attr->nlink = 10;
                }
       -        attr->fileid = *(u64int*)n->handle;
       +        memmove(&u64, n->handle, 8);
       +        attr->fileid = u64;
                attr->atime.sec = n->mtime;
                attr->mtime.sec = n->mtime;
                attr->ctime.sec = n->mtime;
       t@@ -817,6 +824,7 @@ cnodereaddir(Cnode *n, u32int count, u64int cookie, uchar **pdata, u32int *pcoun
        {
                uchar *data, *p, *ep, *np;
                u64int c;
       +        u64int u64;
                Nfs3Entry ne;
                
                n = n->kidlist;
       t@@ -842,7 +850,8 @@ cnodereaddir(Cnode *n, u32int count, u64int cookie, uchar **pdata, u32int *pcoun
                        ne.name = n->name;
                        ne.namelen = strlen(n->name);
                        ne.cookie = ++cookie;
       -                ne.fileid = *(u64int*)n->handle;
       +                memmove(&u64, n->handle, 8);
       +                ne.fileid = u64;
                        if(nfs3entrypack(p, ep, &np, &ne) < 0)
                                break;
                        p = np;
 (DIR) diff --git a/src/lib9/_p9dialparse.c b/src/lib9/_p9dialparse.c
       t@@ -71,7 +71,7 @@ parseip(char *host, u32int *pip)
                                return -1;
                        break;
                }
       -        *pip = *(u32int*)addr;
       +        memmove(pip, addr, 4);
                return 0;
        }
        
 (DIR) diff --git a/src/lib9/sendfd.c b/src/lib9/sendfd.c
       t@@ -46,7 +46,7 @@ sendfd(int s, int fd)
                cmsg->cmsg_len = CMSG_LEN(sizeof(int));
                cmsg->cmsg_level = SOL_SOCKET;
                cmsg->cmsg_type = SCM_RIGHTS;
       -        *(int*)CMSG_DATA(cmsg) = fd;
       +        memmove(CMSG_DATA(cmsg), &fd, sizeof(int));
        
                if((n=sendmsg(s, &msg, 0)) != iov.iov_len)
                        return -1;
 (DIR) diff --git a/src/lib9/truerand.c b/src/lib9/truerand.c
       t@@ -6,6 +6,7 @@ truerand(void)
        {
                int i, n;
                uchar buf[sizeof(ulong)];
       +        ulong x;
                static int randfd = -1;
                static char *randfile;
        
       t@@ -21,5 +22,6 @@ truerand(void)
                for(i=0; i<sizeof(buf); i += n)
                        if((n = readn(randfd, buf+i, sizeof(buf)-i)) < 0)
                                sysfatal("can't read %s: %r", randfile);
       -        return *((ulong*)buf);
       +        memmove(&x, buf, sizeof x);
       +        return x;
        }
 (DIR) diff --git a/src/libauthsrv/readnvram.c b/src/libauthsrv/readnvram.c
       t@@ -76,8 +76,6 @@ readnvram(Nvrsafe *safep, int flag)
                if(strcmp(cputype, "386")==0 || strcmp(cputype, "alpha")==0)
                        cputype = "pc";
        
       -        safe = (Nvrsafe*)buf;
       -
                fd = -1;
                safeoff = -1;
                safelen = -1;
       t@@ -140,7 +138,7 @@ readnvram(Nvrsafe *safep, int flag)
                        memset(safep, 0, sizeof(*safep));
                        safe = safep;
                }else{
       -                *safep = *safe;
       +                memmove(safep, buf, sizeof *safep);
                        safe = safep;
        
                        err |= check(safe->machkey, DESKEYLEN, safe->machsum, "bad nvram key");
       t@@ -163,7 +161,7 @@ readnvram(Nvrsafe *safep, int flag)
                        safe->configsum = nvcsum(safe->config, CONFIGLEN);
                        safe->authidsum = nvcsum(safe->authid, sizeof(safe->authid));
                        safe->authdomsum = nvcsum(safe->authdom, sizeof(safe->authdom));
       -                *(Nvrsafe*)buf = *safe;
       +                memmove(buf, safe, sizeof *safe);
                        if(seek(fd, safeoff, 0) < 0
                        || write(fd, buf, safelen) != safelen){
                                fprint(2, "can't write key to nvram: %r\n");
 (DIR) diff --git a/src/libavl/avl.c b/src/libavl/avl.c
       t@@ -211,6 +211,7 @@ _deleteavl(Avl **tp, Avl *p, Avl *rx, int(*cmp)(Avl*,Avl*), Avl **del,
                return -(ob != 0 && (*tp)->bal == 0);
        }
        
       +/*
        static void
        checkparents(Avl *a, Avl *p)
        {
       t@@ -221,6 +222,7 @@ checkparents(Avl *a, Avl *p)
                checkparents(a->n[0], a);
                checkparents(a->n[1], a);
        }
       +*/
        
        struct Avltree
        {
       t@@ -403,6 +405,7 @@ endwalk(Avlwalk *w)
                free(w);
        }
        
       +/*
        static void
        walkavl(Avl *t, void (*f)(Avl*, void*), void *v)
        {
       t@@ -412,3 +415,4 @@ walkavl(Avl *t, void (*f)(Avl*, void*), void *v)
                f(t, v);
                walkavl(t->n[1], f, v);
        }
       +*/
 (DIR) diff --git a/src/libip/udp.c b/src/libip/udp.c
       t@@ -21,8 +21,8 @@ udpread(int fd, Udphdr *hdr, void *buf, long n)
                }
                memset(hdr, 0, sizeof *hdr);
                memmove(hdr->laddr, v4prefix, IPaddrlen);
       -        *(u32int*)(hdr->laddr+12) = *(u32int*)&sin.sin_addr;
       -        *(u16int*)hdr->lport = *(u16int*)&sin.sin_port;
       +        memmove(hdr->laddr+12, &sin.sin_addr, sizeof(u32int));
       +        memmove(hdr->lport, &sin.sin_port, sizeof(u16int));
        
                len = sizeof sin;
                n = recvfrom(fd, buf, n, 0, (struct sockaddr*)&sin, &len);
       t@@ -33,8 +33,8 @@ udpread(int fd, Udphdr *hdr, void *buf, long n)
                        return -1;
                }
                memmove(hdr->raddr, v4prefix, IPaddrlen);
       -        *(u32int*)(hdr->raddr+12) = *(u32int*)&sin.sin_addr;
       -        *(u16int*)hdr->rport = *(u16int*)&sin.sin_port;
       +        memmove(hdr->raddr+12, &sin.sin_addr, sizeof(u32int));
       +        memmove(hdr->rport, &sin.sin_port, sizeof(u16int));
        
                return n;
        }
 (DIR) diff --git a/src/libmach/Linux.c b/src/libmach/Linux.c
       t@@ -146,7 +146,7 @@ ptracerw(int type, int xtype, int isr, int pid, ulong addr, void *v, uint n)
                                if(n-i >= 4)
                                        *(u32int*)((char*)v+i) = u;
                                else{
       -                                *(u32int*)buf = u;
       +                                memmove(buf, &u, 4);
                                        memmove((char*)v+i, buf, n-i);
                                }
                        }else{
       t@@ -157,9 +157,9 @@ ptracerw(int type, int xtype, int isr, int pid, ulong addr, void *v, uint n)
                                        u = ptrace(xtype, pid, addr+i, 0);
                                        if(errno)
                                                return -1;
       -                                *(u32int*)buf = u;
       +                                memmove(buf, &u, 4);
                                        memmove(buf, (char*)v+i, n-i);
       -                                u = *(u32int*)buf;
       +                                memmove(&u, buf, 4);
                                }
                                if(ptrace(type, pid, addr+i, u) < 0)
                                        goto ptraceerr;
 (DIR) diff --git a/src/libmemdraw/draw.c b/src/libmemdraw/draw.c
       t@@ -2109,6 +2109,7 @@ memoptdraw(Memdrawparam *par)
        {
                int m, y, dy, dx, op;
                u32int v;
       +        u16int u16;
                Memimage *src;
                Memimage *dst;
        
       t@@ -2198,7 +2199,8 @@ DBG print("dp %p v %lux lm %ux (v ^ *dp) & lm %lux\n", dp, v, lm, (v^*dp)&lm);
                        case 16:
                                p[0] = v;                /* make little endian */
                                p[1] = v>>8;
       -                        v = *(ushort*)p;
       +                        memmove(&u16, p, 2);
       +                        v = u16;
        DBG print("dp=%p; dx=%d; for(y=0; y<%d; y++, dp+=%d)\nmemsets(dp, v, dx);\n",
                dp, dx, dy, dwid);
                                for(y=0; y<dy; y++, dp+=dwid)
       t@@ -2213,7 +2215,7 @@ DBG print("dp=%p; dx=%d; for(y=0; y<%d; y++, dp+=%d)\nmemsets(dp, v, dx);\n",
                                p[1] = v>>8;
                                p[2] = v>>16;
                                p[3] = v>>24;
       -                        v = *(u32int*)p;
       +                        memmove(&v, p, 4);
                                for(y=0; y<dy; y++, dp+=dwid)
                                        memsetl(dp, v, dx);
                                return 1;
       t@@ -2371,6 +2373,13 @@ chardraw(Memdrawparam *par)
                uchar sp[4];
                Rectangle r, mr;
                Memimage *mask, *src, *dst;
       +        union {
       +                // black box to hide pointer conversions from gcc.
       +                // we'll see how long this works.
       +                uchar *u8;
       +                u16int *u16;
       +                u32int *u32;
       +        } gcc_black_box;
        
        if(0) if(drawdebug) iprint("chardraw? mf %lux md %d sf %lux dxs %d dys %d dd %d ddat %p sdat %p\n",
                        par->mask->flags, par->mask->depth, par->src->flags, 
       t@@ -2443,8 +2452,10 @@ DBG print("bits %lux sh %d...", bits, i);
                                }
                                break;
                        case 16:
       -                        ws = (ushort*)wp;
       -                        v = *(ushort*)sp;
       +                        gcc_black_box.u8 = wp;
       +                        ws = gcc_black_box.u16;
       +                        gcc_black_box.u8 = sp;
       +                        v = *gcc_black_box.u16;
                                for(x=bx; x>ex; x--, ws++){
                                        i = x&7;
                                        if(i == 8-1)
       t@@ -2469,8 +2480,10 @@ DBG print("bits %lux sh %d...", bits, i);
                                }
                                break;
                        case 32:
       -                        wl = (u32int*)wp;
       -                        v = *(u32int*)sp;
       +                        gcc_black_box.u8 = wp;
       +                        wl = gcc_black_box.u32;
       +                        gcc_black_box.u8 = sp;
       +                        v = *gcc_black_box.u32;
                                for(x=bx; x>ex; x--, wl++){
                                        i = x&7;
                                        if(i == 8-1)
       t@@ -2537,7 +2550,7 @@ _memfillcolor(Memimage *i, u32int val)
                        p[1] = bits>>8;
                        p[2] = bits>>16;
                        p[3] = bits>>24;
       -                bits = *(u32int*)p;
       +                memmove(&bits, p, 4);
                        memsetl(wordaddr(i, i->r.min), bits, i->width*Dy(i->r));
                        break;
                }