From ba2505210e9d1b649802676a8791f87e6d474c90 Mon Sep 17 00:00:00 2001 From: Michal Bistyga Date: Fri, 2 Feb 2018 12:37:08 +0100 Subject: [PATCH] [Bluetooth] Fixing undefined behaviour during cast Char is unsigned by default on ARM architecture. static_cast from double to unsigned char is undefined behaviour and complier overwritten any negative value with 0. [Validation] tests 100% pass rate Change-Id: Iec39a3c17b18ec7aa4f020fe5de709d66fa426fd Signed-off-by: Michal Bistyga --- src/bluetooth/bluetooth_gatt_service.cc | 2 +- src/bluetooth/bluetooth_health_channel.cc | 2 +- src/bluetooth/bluetooth_socket.cc | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/bluetooth/bluetooth_gatt_service.cc b/src/bluetooth/bluetooth_gatt_service.cc index 14f4bc7..f6798a8 100644 --- a/src/bluetooth/bluetooth_gatt_service.cc +++ b/src/bluetooth/bluetooth_gatt_service.cc @@ -424,7 +424,7 @@ void BluetoothGATTService::WriteValue(const picojson::value& args, picojson::obj int value_size = value_array.size(); std::unique_ptr value_data(new char[value_size]); for (int i = 0; i < value_size; ++i) { - value_data[i] = static_cast(value_array[i].get()); + value_data[i] = (int) value_array[i].get(); } struct Data { diff --git a/src/bluetooth/bluetooth_health_channel.cc b/src/bluetooth/bluetooth_health_channel.cc index 8d19279..e119693 100644 --- a/src/bluetooth/bluetooth_health_channel.cc +++ b/src/bluetooth/bluetooth_health_channel.cc @@ -77,7 +77,7 @@ void BluetoothHealthChannel::SendData(const picojson::value& data, picojson::obj std::unique_ptr data_ptr{new char[data_size]}; for (std::size_t i = 0; i < data_size; ++i) { - data_ptr[i] = static_cast(binary_data[i].get()); + data_ptr[i] = (int) binary_data[i].get(); } int ntv_ret = bt_hdp_send_data(channel, data_ptr.get(), data_size); diff --git a/src/bluetooth/bluetooth_socket.cc b/src/bluetooth/bluetooth_socket.cc index 7625ae4..fea9ed1 100644 --- a/src/bluetooth/bluetooth_socket.cc +++ b/src/bluetooth/bluetooth_socket.cc @@ -63,7 +63,7 @@ void BluetoothSocket::WriteData(const picojson::value& data, picojson::object& o std::unique_ptr data_ptr{new char[data_size]}; for (std::size_t i = 0; i < data_size; ++i) { - data_ptr[i] = static_cast(binary_data[i].get()); + data_ptr[i] = (int) binary_data[i].get(); } if (kBluetoothError == bt_socket_send_data(socket, data_ptr.get(), data_size)) { -- 2.7.4