Re: [PATCH 7/7] Avoid cast alignment warnings in port allocator test

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

 



On 04/03/2013 09:06 AM, Daniel P. Berrange wrote:
> From: "Daniel P. Berrange" <berrange@xxxxxxxxxx>
> 
> To avoid
> 
> virportallocatortest.c: In function 'bind':
> virportallocatortest.c:34:33: warning: cast increases required alignment of target type [-Wcast-align]
>      struct sockaddr_in *saddr = (struct sockaddr_in *)addr;
>                                  ^
> 
> Signed-off-by: Daniel P. Berrange <berrange@xxxxxxxxxx>
> ---
>  tests/virportallocatortest.c | 12 +++++++-----
>  1 file changed, 7 insertions(+), 5 deletions(-)
> 
> diff --git a/tests/virportallocatortest.c b/tests/virportallocatortest.c
> index 9931e11..1935602 100644
> --- a/tests/virportallocatortest.c
> +++ b/tests/virportallocatortest.c
> @@ -31,12 +31,14 @@ int bind(int sockfd ATTRIBUTE_UNUSED,
>           const struct sockaddr *addr,
>           socklen_t addrlen ATTRIBUTE_UNUSED)
>  {
> -    struct sockaddr_in *saddr = (struct sockaddr_in *)addr;
> +    struct sockaddr_in saddr;

Hmm.  POSIX requires that apps use sockaddr_storage to guarantee
alignment before downcasting to struct sockaddr_in or further down to
struct sockaddr.  But since we are providing a replacement bind(), and
POSIX doesn't give us any way to specify that 'struct sockaddr' in our
signature should have been aligned as struct sockaddr_storage by any
sane caller, we're stuck playing realignment games, or else using a pragma.

>  
> -    if (saddr->sin_port == htons(5900) ||
> -        saddr->sin_port == htons(5904) ||
> -        saddr->sin_port == htons(5905) ||
> -        saddr->sin_port == htons(5906)) {
> +    memcpy(&saddr, addr, sizeof(saddr));

Since this is just test code, performance doesn't matter, and using
memcpy() for the realignment game makes sense.

ACK.

-- 
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org

Attachment: signature.asc
Description: OpenPGP digital signature

--
libvir-list mailing list
libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list

[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]