On Mon, Jan 31, 2011 at 04:56:06PM -0800, Marcel Moolenaar wrote:
> 
> Take the statement at line 116 for example:
>       *((int *)CMSG_DATA(cmsg)) = fd;
> 
> We're effectively casting from a (char *) to a (int *) and then doing
> a 32-bit access (write). The easy fix (casting through (void *) is not
> possible, because you cannot guarantee that the address is properly
> aligned. cmsg points to memory set aside by the following local
> variable:
>       unsigned char ctrl[CMSG_SPACE(sizeof(fd))];
> 
> There's no guarantee that the compiler will align the character array
> at a 32-bit boundary (though in practice it seems to be). I have seen
> this kind of construct fail on ARM and PowerPC for example.
> 

Why not to use such declaration:

        union {
                struct cmsghdr cm;
                char ctrl[CMSG_SPACE(sizeof(fd))];
        } control_un;

At least this is necessary to satisfy that CMSG_FIRSTHDR() will give
address of correctly aligned struct cmsghdr{}.
_______________________________________________
freebsd-current@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-current
To unsubscribe, send any mail to "freebsd-current-unsubscr...@freebsd.org"

Reply via email to