Clarify alignment requirements for the control transfer buffer
authorPaul Fertser <fercerpav@gmail.com>
Mon, 1 Jul 2013 17:07:24 +0000 (21:07 +0400)
committerHans de Goede <hdegoede@redhat.com>
Wed, 21 Aug 2013 14:04:08 +0000 (16:04 +0200)
Since the buffer pointer will later be casted to ``struct
libusb_control_setup *'', it should point to memory aligned to at least
2 bytes boundary as that's the strictest requirement of the struct fields.

Also, use a (void *) casting trick to convince the compiler the cast is
safe, to fix warnings such as:

/usr/local/include/libusb-1.0/libusb.h: In function 'libusb_control_transfer_get_setup':
/usr/local/include/libusb-1.0/libusb.h:1435:9: error: cast increases required alignment of target type [-Werror=cast-align]
/usr/local/include/libusb-1.0/libusb.h: In function 'libusb_fill_control_setup':
/usr/local/include/libusb-1.0/libusb.h:1464:39: error: cast increases required alignment of target type [-Werror=cast-align]
/usr/local/include/libusb-1.0/libusb.h: In function 'libusb_fill_control_transfer':
/usr/local/include/libusb-1.0/libusb.h:1509:39: error: cast increases required alignment of target type [-Werror=cast-align]
cc1: all warnings being treated as errors

This actually can lead to failure to build from the sources for certain
projects which use -Werror=cast-align on ARM.

Signed-off-by: Paul Fertser <fercerpav@gmail.com>
Signed-off-by: Hans de Goede <hdegoede@redhat.com>
libusb/io.c
libusb/libusb.h
libusb/version_nano.h

index d766ccf..4f22963 100644 (file)
@@ -787,7 +787,7 @@ void cb(struct libusb_transfer *transfer)
 
 void myfunc() {
        struct libusb_transfer *transfer;
-       unsigned char buffer[LIBUSB_CONTROL_SETUP_SIZE];
+       unsigned char buffer[LIBUSB_CONTROL_SETUP_SIZE] __attribute__ ((aligned (2)));
        int completed = 0;
 
        transfer = libusb_alloc_transfer(0);
index 97aeecc..da3f1ef 100644 (file)
@@ -1428,7 +1428,7 @@ static inline unsigned char *libusb_control_transfer_get_data(
 static inline struct libusb_control_setup *libusb_control_transfer_get_setup(
        struct libusb_transfer *transfer)
 {
-       return (struct libusb_control_setup *) transfer->buffer;
+       return (struct libusb_control_setup *)(void *) transfer->buffer;
 }
 
 /** \ingroup asyncio
@@ -1437,6 +1437,7 @@ static inline struct libusb_control_setup *libusb_control_transfer_get_setup(
  * be given in host-endian byte order.
  *
  * \param buffer buffer to output the setup packet into
+ * This pointer must be aligned to at least 2 bytes boundary.
  * \param bmRequestType see the
  * \ref libusb_control_setup::bmRequestType "bmRequestType" field of
  * \ref libusb_control_setup
@@ -1457,7 +1458,7 @@ static inline void libusb_fill_control_setup(unsigned char *buffer,
        uint8_t bmRequestType, uint8_t bRequest, uint16_t wValue, uint16_t wIndex,
        uint16_t wLength)
 {
-       struct libusb_control_setup *setup = (struct libusb_control_setup *) buffer;
+       struct libusb_control_setup *setup = (struct libusb_control_setup *)(void *) buffer;
        setup->bmRequestType = bmRequestType;
        setup->bRequest = bRequest;
        setup->wValue = libusb_cpu_to_le16(wValue);
@@ -1493,6 +1494,7 @@ void LIBUSB_CALL libusb_free_transfer(struct libusb_transfer *transfer);
  * \param dev_handle handle of the device that will handle the transfer
  * \param buffer data buffer. If provided, this function will interpret the
  * first 8 bytes as a setup packet and infer the transfer length from that.
+ * This pointer must be aligned to at least 2 bytes boundary.
  * \param callback callback function to be invoked on transfer completion
  * \param user_data user data to pass to callback function
  * \param timeout timeout for the transfer in milliseconds
@@ -1502,7 +1504,7 @@ static inline void libusb_fill_control_transfer(
        unsigned char *buffer, libusb_transfer_cb_fn callback, void *user_data,
        unsigned int timeout)
 {
-       struct libusb_control_setup *setup = (struct libusb_control_setup *) buffer;
+       struct libusb_control_setup *setup = (struct libusb_control_setup *)(void *) buffer;
        transfer->dev_handle = dev_handle;
        transfer->endpoint = 0;
        transfer->type = LIBUSB_TRANSFER_TYPE_CONTROL;
index b5a3ca9..c4c38a9 100644 (file)
@@ -1 +1 @@
-#define LIBUSB_NANO 10817
+#define LIBUSB_NANO 10818