[Spice-devel] [spice-protocol PATCH v1 11/12] macros: fix alignment issue reported by clang

Christophe Fergeau cfergeau at redhat.com
Thu Aug 6 03:54:24 PDT 2015


On Wed, Aug 05, 2015 at 02:23:25PM +0200, Victor Toso wrote:
> char_device.c:131:52: warning: cast from 'uint8_t *'
> (aka 'unsigned char *') to 'SpiceCharDeviceMsgToClientItem *'
> (aka 'struct SpiceCharDeviceMsgToClientItem *')
> increases required alignment from 1 to 8 [-Wcast-align]
> 
>  SpiceCharDeviceMsgToClientItem *msg_item = SPICE_CONTAINEROF(item,
>                                             ^~~~~~~~~~~~~~~~~~~~~~~
> 
> ../spice-common/spice-protocol/spice/macros.h:142:6: note: expanded
> from macro 'SPICE_CONTAINEROF'
> 
>  ((struct_type *)((uint8_t *)(ptr) - SPICE_OFFSETOF(struct_type, member)))
>   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> ---
>  spice/macros.h | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/spice/macros.h b/spice/macros.h
> index dad029c..bc9b648 100644
> --- a/spice/macros.h
> +++ b/spice/macros.h
> @@ -140,7 +140,7 @@
>  #endif
>  
>  #define SPICE_CONTAINEROF(ptr, struct_type, member) \
> -    ((struct_type *)((uint8_t *)(ptr) - SPICE_OFFSETOF(struct_type, member)))
> +    ((struct_type *)(void *)((uint8_t *)(ptr) - SPICE_OFFSETOF(struct_type, member)))

For this one, you can rely on implicit cast from (void *) to any pointer
type, ie make the macro

-    ((struct_type *)((uint8_t *)(ptr) - SPICE_OFFSETOF(struct_type, member)))
+    ((void *)((uint8_t *)(ptr) - SPICE_OFFSETOF(struct_type, member)))

The alignement constraints of the returned struct should have been
taken into consideration when it was allocated, so the warning here
can be silenced as alignment should be right when casting back.

Christophe
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 819 bytes
Desc: not available
URL: <http://lists.freedesktop.org/archives/spice-devel/attachments/20150806/bb36aab7/attachment.sig>


More information about the Spice-devel mailing list