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

Eric Blake eblake at redhat.com
Fri Apr 5 21:06:46 UTC 2013


On 04/03/2013 09:06 AM, Daniel P. Berrange wrote:
> From: "Daniel P. Berrange" <berrange at redhat.com>
> 
> 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 at redhat.com>
> ---
>  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

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 621 bytes
Desc: OpenPGP digital signature
URL: <http://listman.redhat.com/archives/libvir-list/attachments/20130405/47a7016b/attachment-0001.sig>


More information about the libvir-list mailing list