Skip to content

Commit

Permalink
pybricks.common.ble: fix compiler warning
Browse files Browse the repository at this point in the history
With GCC 13 and -flto disabled, we were getting warnings like:

    ../../pybricks/common/pb_type_ble.c:291:21: error: array subscript 5 is outside the bounds of an interior zero-length array 'uint8_t[0]' {aka 'unsigned char[]'} [-Werror=zero-length-bounds]
    291 |         value.v.data[5] = PB_BLE_BROADCAST_DATA_TYPE_SINGLE_OBJECT << 5;
        |         ~

Instead of using the zero-length array, access the d member directly.
This allows the compiler to actually check for out of bounds access
and warn us about it.
  • Loading branch information
dlech committed Jan 19, 2025
1 parent 2284669 commit a2c2653
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions pybricks/common/pb_type_ble.c
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ static mp_obj_t pb_module_ble_broadcast(size_t n_args, const mp_obj_t *pos_args,
mp_obj_get_array(data_in, &n_objs, &objs);
} else {
// Set first type to indicate single object.
value.v.data[5] = PB_BLE_BROADCAST_DATA_TYPE_SINGLE_OBJECT << 5;
value.d[5] = PB_BLE_BROADCAST_DATA_TYPE_SINGLE_OBJECT << 5;
// The one and only value is included directly after.
index = 1;
n_objs = 1;
Expand All @@ -297,14 +297,14 @@ static mp_obj_t pb_module_ble_broadcast(size_t n_args, const mp_obj_t *pos_args,

// Encode all objects.
for (size_t i = 0; i < n_objs; i++) {
index = pb_module_ble_encode(&value.v.data[5], index, objs[i]);
index = pb_module_ble_encode(&value.d[5], index, objs[i]);
}

value.v.size = index + 5;
value.v.data[0] = index + 4; // length
value.v.data[1] = MFG_SPECIFIC;
pbio_set_uint16_le(&value.v.data[2], LEGO_CID);
value.v.data[4] = mp_obj_get_int(self->broadcast_channel);
value.d[0] = index + 4; // length
value.d[1] = MFG_SPECIFIC;
pbio_set_uint16_le(&value.d[2], LEGO_CID);
value.d[4] = mp_obj_get_int(self->broadcast_channel);

pbdrv_bluetooth_start_broadcasting(&broadcast_task, &value.v);
return pb_module_tools_pbio_task_wait_or_await(&broadcast_task);
Expand Down

0 comments on commit a2c2653

Please sign in to comment.