Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 78ed4c63 authored by Myles Watson's avatar Myles Watson
Browse files

Storage: Abort if the callback is invalidated

Bug: 260006855
Test: atest bluetooth_test_gd_unit
Change-Id: I704b13adb260669ff46948bc9254fea5b6fae1f0
parent 60dc72c5
Loading
Loading
Loading
Loading
+8 −5
Original line number Diff line number Diff line
@@ -70,13 +70,14 @@ void ConfigCache::SetPersistentConfigChangedCallback(std::function<void()> persi
}

ConfigCache::ConfigCache(ConfigCache&& other) noexcept
    : persistent_config_changed_callback_(std::move(other.persistent_config_changed_callback_)),
    : persistent_config_changed_callback_(nullptr),
      persistent_property_names_(std::move(other.persistent_property_names_)),
      information_sections_(std::move(other.information_sections_)),
      persistent_devices_(std::move(other.persistent_devices_)),
      temporary_devices_(std::move(other.temporary_devices_)) {
  // std::function will be in a valid but unspecified state after std::move(), hence resetting it
  other.persistent_config_changed_callback_ = {};
  ASSERT_LOG(
      other.persistent_config_changed_callback_ == nullptr,
      "Can't assign after setting the callback");
}

ConfigCache& ConfigCache::operator=(ConfigCache&& other) noexcept {
@@ -85,8 +86,10 @@ ConfigCache& ConfigCache::operator=(ConfigCache&& other) noexcept {
  }
  std::lock_guard<std::recursive_mutex> my_lock(mutex_);
  std::lock_guard<std::recursive_mutex> others_lock(other.mutex_);
  persistent_config_changed_callback_.swap(other.persistent_config_changed_callback_);
  other.persistent_config_changed_callback_ = {};
  ASSERT_LOG(
      other.persistent_config_changed_callback_ == nullptr,
      "Can't assign after setting the callback");
  persistent_config_changed_callback_ = {};
  persistent_property_names_ = std::move(other.persistent_property_names_);
  information_sections_ = std::move(other.information_sections_);
  persistent_devices_ = std::move(other.persistent_devices_);
+2 −1
Original line number Diff line number Diff line
@@ -193,13 +193,14 @@ void StorageModule::Start() {
    config->SetProperty(kInfoSection, kTimeCreatedProperty, ss.str());
  }
  config->FixDeviceTypeInconsistencies();
  config->SetPersistentConfigChangedCallback([this] { this->CallOn(this, &StorageModule::SaveDelayed); });
  // TODO (b/158035889) Migrate metrics module to GD
  pimpl_ = std::make_unique<impl>(GetHandler(), std::move(config.value()), temp_devices_capacity_);
  if (save_needed) {
    // Set a timer and write the new config file to disk.
    SaveDelayed();
  }
  pimpl_->cache_.SetPersistentConfigChangedCallback(
      [this] { this->CallOn(this, &StorageModule::SaveDelayed); });
  if (bluetooth::os::ParameterProvider::GetBtKeystoreInterface() != nullptr) {
    bluetooth::os::ParameterProvider::GetBtKeystoreInterface()->ConvertEncryptOrDecryptKeyIfNeeded();
  }