From 301a7103a0f19fa747bff29cc6af0914b7e6b646 Mon Sep 17 00:00:00 2001 From: Carles Cufi Date: Wed, 29 Sep 2021 14:54:51 +0200 Subject: [PATCH] Bluetooth: common: Add build asserts for address struct sizes In some HCI packed structures we place non-packed structs inside (in particular the address structs). Alignment is not an issue for those because all their members are just byte-aligned, but size is. If the compiler ever packs those it would become a real problem, and so detect this at build time. See the link below for more info: https://stackoverflow.com/a/66389167 Signed-off-by: Carles Cufi --- include/bluetooth/addr.h | 9 ++++++++- subsys/bluetooth/common/dummy.c | 11 +++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/include/bluetooth/addr.h b/include/bluetooth/addr.h index 37e1bf268a0..bd6669baa08 100644 --- a/include/bluetooth/addr.h +++ b/include/bluetooth/addr.h @@ -30,10 +30,17 @@ extern "C" { #define BT_ADDR_LE_PUBLIC_ID 0x02 #define BT_ADDR_LE_RANDOM_ID 0x03 +/** Length in bytes of a standard Bluetooth address */ +#define BT_ADDR_SIZE 6 + /** Bluetooth Device Address */ typedef struct { - uint8_t val[6]; + uint8_t val[BT_ADDR_SIZE]; } bt_addr_t; +/**/ + +/** Length in bytes of an LE Bluetooth address. Not packed, so no sizeof() */ +#define BT_ADDR_LE_SIZE 7 /** Bluetooth LE Device Address */ typedef struct { diff --git a/subsys/bluetooth/common/dummy.c b/subsys/bluetooth/common/dummy.c index 7a2bdb81a92..dd3d45ff7c8 100644 --- a/subsys/bluetooth/common/dummy.c +++ b/subsys/bluetooth/common/dummy.c @@ -10,6 +10,17 @@ */ #include +#include + +/* + * The unpacked structs below are used inside __packed structures that reflect + * what goes on the wire. While alignment is not a problem for those, since all + * their members are bytes or byte arrays, the size is. They must not be padded + * by the compiler, otherwise the on-wire packet will not map the packed + * structure correctly. + */ +BUILD_ASSERT(sizeof(bt_addr_t) == BT_ADDR_SIZE); +BUILD_ASSERT(sizeof(bt_addr_le_t) == BT_ADDR_LE_SIZE); #if defined(CONFIG_BT_HCI_HOST) /* The Bluetooth subsystem requires the Tx thread to execute at higher priority