From 384d144d6e163af384d351b59a74f403e87f2ea7 Mon Sep 17 00:00:00 2001 From: Janusz Sobczak Date: Tue, 15 Nov 2022 13:28:59 -0800 Subject: [PATCH] Do not use absl::string_view as keys. absl::string_view usually is not suitable type for a member variable nor a map key. The string referenced by string_view may run out of scope. PiperOrigin-RevId: 488743930 --- .../g3/credential_storage_impl.cc | 35 +++++++++--------- .../g3/credential_storage_impl.h | 36 +++++++++++++------ 2 files changed, 42 insertions(+), 29 deletions(-) diff --git a/internal/platform/implementation/g3/credential_storage_impl.cc b/internal/platform/implementation/g3/credential_storage_impl.cc index d56e024b..9dc72db5 100644 --- a/internal/platform/implementation/g3/credential_storage_impl.cc +++ b/internal/platform/implementation/g3/credential_storage_impl.cc @@ -14,6 +14,7 @@ #include "internal/platform/implementation/g3/credential_storage_impl.h" +#include #include #include #include @@ -32,7 +33,7 @@ void CredentialStorageImpl::SaveCredentials( absl::string_view manager_app_id, absl::string_view account_name, const std::vector& private_credentials, const std::vector& public_credentials, - ::nearby::presence::PublicCredentialType public_credential_type, + PublicCredentialType public_credential_type, ::nearby::presence::GenerateCredentialsCallback callback) { NEARBY_LOGS(INFO) << "G3 Save Private Credentials for account: " << account_name << "], manager app ID:[" << manager_app_id @@ -45,14 +46,14 @@ void CredentialStorageImpl::SaveCredentials( } { absl::MutexLock lock(&private_mutex_); - auto private_key_value = std::make_pair( - std::make_pair(manager_app_id, account_name), private_credentials); - auto private_result = private_credentials_map_.insert(private_key_value); + PrivateCredentialKey key = + CreatePrivateCredentialKey(manager_app_id, account_name); + auto private_result = private_credentials_map_.insert( + std::make_pair(key, private_credentials)); if (!private_result.second) { NEARBY_LOGS(WARNING) << "Credentials already saved in map. Overwriting previous creds!"; - private_credentials_map_[std::make_pair(manager_app_id, account_name)] = - private_credentials; + private_credentials_map_[key] = private_credentials; } } @@ -67,16 +68,14 @@ void CredentialStorageImpl::SaveCredentials( } { absl::MutexLock lock(&public_mutex_); - auto public_key_value = std::make_pair( - std::make_tuple(manager_app_id, account_name, public_credential_type), - public_credentials); - auto public_result = public_credentials_map_.insert(public_key_value); + PublicCredentialKey key = CreatePublicCredentialKey( + manager_app_id, account_name, public_credential_type); + auto public_result = + public_credentials_map_.insert(std::make_pair(key, public_credentials)); if (!public_result.second) { NEARBY_LOGS(WARNING) << "Credentials already saved in map. Overwriting previous creds!"; - public_credentials_map_[std::make_tuple(manager_app_id, account_name, - public_credential_type)] = - public_credentials; + public_credentials_map_[key] = public_credentials; } } @@ -90,8 +89,8 @@ void CredentialStorageImpl::GetPrivateCredentials( << credential_selector.account_name << "], manager app ID:[" << credential_selector.manager_app_id << "]"; absl::MutexLock lock(&private_mutex_); - auto key = std::make_pair(credential_selector.manager_app_id, - credential_selector.account_name); + PrivateCredentialKey key = CreatePrivateCredentialKey( + credential_selector.manager_app_id, credential_selector.account_name); if (private_credentials_map_.find(key) == private_credentials_map_.end()) { NEARBY_LOGS(WARNING) << "There are no Private Credentials stored for key:" << std::get<0>(key) << ", " << std::get<1>(key); @@ -112,9 +111,9 @@ void CredentialStorageImpl::GetPublicCredentials( << credential_selector.account_name << "], manager app ID:[" << credential_selector.manager_app_id << "]"; absl::MutexLock lock(&public_mutex_); - auto key = - std::make_tuple(credential_selector.manager_app_id, - credential_selector.account_name, public_credential_type); + PublicCredentialKey key = CreatePublicCredentialKey( + credential_selector.manager_app_id, credential_selector.account_name, + public_credential_type); if (public_credentials_map_.find(key) == public_credentials_map_.end()) { NEARBY_LOGS(WARNING) << "There are no Public Credentials stored for key:" << std::get<0>(key) << ", " << std::get<1>(key) << ", " diff --git a/internal/platform/implementation/g3/credential_storage_impl.h b/internal/platform/implementation/g3/credential_storage_impl.h index b823deb8..7e4287a7 100644 --- a/internal/platform/implementation/g3/credential_storage_impl.h +++ b/internal/platform/implementation/g3/credential_storage_impl.h @@ -16,6 +16,7 @@ #define THIRD_PARTY_NEARBY_INTERNAL_PLATFORM_IMPLEMENTATION_G3_CREDENTIAL_STORAGE_H_ #include +#include #include #include #include @@ -38,17 +39,22 @@ namespace g3 { */ class CredentialStorageImpl : public api::CredentialStorage { public: + using PrivateCredential = ::nearby::internal::PrivateCredential; + using PublicCredential = ::nearby::internal::PublicCredential; + using PublicCredentialType = ::nearby::presence::PublicCredentialType; + using PrivateCredentialKey = std::pair; + using PublicCredentialKey = + std::tuple; + explicit CredentialStorageImpl() = default; ~CredentialStorageImpl() override = default; // Used to save private and public credentials. void SaveCredentials( absl::string_view manager_app_id, absl::string_view account_name, - const std::vector<::nearby::internal::PrivateCredential>& - private_credentials, - const std::vector<::nearby::internal::PublicCredential>& - public_credentials, - ::nearby::presence::PublicCredentialType public_credential_type, + const std::vector& private_credentials, + const std::vector& public_credentials, + PublicCredentialType public_credential_type, ::nearby::presence::GenerateCredentialsCallback callback) override; // Used to fetch private creds when broadcasting. @@ -60,16 +66,24 @@ class CredentialStorageImpl : public api::CredentialStorage { // Used to fetch remote public creds when scanning. void GetPublicCredentials( const ::nearby::presence::CredentialSelector& credential_selector, - ::nearby::presence::PublicCredentialType public_credential_type, + PublicCredentialType public_credential_type, ::nearby::presence::GetPublicCredentialsResultCallback callback) override; private: - absl::flat_hash_map, - std::vector<::nearby::internal::PrivateCredential>> + PrivateCredentialKey CreatePrivateCredentialKey( + absl::string_view manager_app_id, absl::string_view account_name) { + return std::make_tuple(std::string(manager_app_id), + std::string(account_name)); + } + PublicCredentialKey CreatePublicCredentialKey( + absl::string_view manager_app_id, absl::string_view account_name, + PublicCredentialType credential_type) { + return std::make_tuple(std::string(manager_app_id), + std::string(account_name), credential_type); + } + absl::flat_hash_map> private_credentials_map_; - absl::flat_hash_map, - std::vector<::nearby::internal::PublicCredential>> + absl::flat_hash_map> public_credentials_map_; absl::Mutex private_mutex_; absl::Mutex public_mutex_;