[Bluetooth][Bugfix] Copy value we don't own before passing to a worker 85/244185/1
authorPawel Wasowski <p.wasowski2@samsung.com>
Tue, 15 Sep 2020 15:24:04 +0000 (17:24 +0200)
committerPawel Wasowski <p.wasowski2@samsung.com>
Tue, 15 Sep 2020 15:47:27 +0000 (17:47 +0200)
const char *remote_address is passed as an argument from
Native API to our callback, that adds a job to the Bluetooth's
worker.
Previously, the remote_address pointer was passed straight
to the worker. When referenced, it was already garbage.
This commit adds a copy of this value, so that the worker gets
proper data.

[Validation] Tested in Chrome DevTools. remoteAddress in JS is
a valid MAC address.

Change-Id: Id571296861775fee70d93ca94c4da0b496f9d3c8
Signed-off-by: Pawel Wasowski <p.wasowski2@samsung.com>
src/bluetooth/bluetooth_gatt_server_service.cc

index 8d0bc02..bf07067 100644 (file)
@@ -449,13 +449,17 @@ PlatformResult BluetoothGATTServerService::SetReadValueRequestCallback(
                 remote_address, request_id, offset);
     auto rw_callback_data = static_cast<ReadWriteRequestCallbackData*>(user_data);
 
+    // We create a copy of this value, because remote_address pointer will be invalid
+    // when the job will be executed in the worker
+    auto remote_address_copy = std::string{remote_address};
+
     rw_callback_data->instance_.GetWorker().add_job(
-        [remote_address, request_id, server, gatt_handle, offset, rw_callback_data] {
+        [remote_address_copy, request_id, server, gatt_handle, offset, rw_callback_data] {
           ScopeLogger("Async call: SetReadValueRequestCallback");
 
           auto read_value_request = picojson::value{picojson::object{}};
           auto& read_value_request_obj = read_value_request.get<picojson::object>();
-          read_value_request_obj[kClientAddress] = picojson::value{remote_address};
+          read_value_request_obj[kClientAddress] = picojson::value{remote_address_copy};
           read_value_request_obj[kRequestId] = picojson::value{static_cast<double>(request_id)};
           read_value_request_obj[kRequestType] = picojson::value{kReadRequestType};
           read_value_request_obj[kOffset] = picojson::value{static_cast<double>(offset)};