[Bluetooth] Fixing undefined behaviour during cast 48/169148/2
authorMichal Bistyga <m.bistyga@samsung.com>
Fri, 2 Feb 2018 11:37:08 +0000 (12:37 +0100)
committerMichal Bistyga <m.bistyga@samsung.com>
Fri, 2 Feb 2018 11:48:31 +0000 (11:48 +0000)
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 <m.bistyga@samsung.com>
src/bluetooth/bluetooth_gatt_service.cc
src/bluetooth/bluetooth_health_channel.cc
src/bluetooth/bluetooth_socket.cc

index 14f4bc7..f6798a8 100644 (file)
@@ -424,7 +424,7 @@ void BluetoothGATTService::WriteValue(const picojson::value& args, picojson::obj
   int value_size = value_array.size();
   std::unique_ptr<char[]> value_data(new char[value_size]);
   for (int i = 0; i < value_size; ++i) {
-    value_data[i] = static_cast<char>(value_array[i].get<double>());
+    value_data[i] = (int) value_array[i].get<double>();
   }
 
   struct Data {
index 8d19279..e119693 100644 (file)
@@ -77,7 +77,7 @@ void BluetoothHealthChannel::SendData(const picojson::value& data, picojson::obj
   std::unique_ptr<char[]> data_ptr{new char[data_size]};
 
   for (std::size_t i = 0; i < data_size; ++i) {
-    data_ptr[i] = static_cast<char>(binary_data[i].get<double>());
+    data_ptr[i] = (int) binary_data[i].get<double>();
   }
 
   int ntv_ret = bt_hdp_send_data(channel, data_ptr.get(), data_size);
index 7625ae4..fea9ed1 100644 (file)
@@ -63,7 +63,7 @@ void BluetoothSocket::WriteData(const picojson::value& data, picojson::object& o
   std::unique_ptr<char[]> data_ptr{new char[data_size]};
 
   for (std::size_t i = 0; i < data_size; ++i) {
-    data_ptr[i] = static_cast<char>(binary_data[i].get<double>());
+    data_ptr[i] = (int) binary_data[i].get<double>();
   }
 
   if (kBluetoothError == bt_socket_send_data(socket, data_ptr.get(), data_size)) {