From a0cee4f7fe252a318286042cc290e9a0f7178652 Mon Sep 17 00:00:00 2001 From: Utkarsh Ayachit Date: Tue, 9 Nov 2021 19:32:50 -0500 Subject: [PATCH] fix thread-safety issues `custom_name_to_id` was being used in thread-unsafe way by `type::get_by_name`. Fixed that. --- src/rttr/detail/type/type_register.cpp | 8 ++++++-- src/rttr/detail/type/type_register_p.h | 2 +- src/rttr/type.cpp | 7 +------ 3 files changed, 8 insertions(+), 9 deletions(-) diff --git a/src/rttr/detail/type/type_register.cpp b/src/rttr/detail/type/type_register.cpp index 25181117..4d348a48 100644 --- a/src/rttr/detail/type/type_register.cpp +++ b/src/rttr/detail/type/type_register.cpp @@ -1115,9 +1115,13 @@ flat_map& type_register_private::get_orig_name_to_id() ///////////////////////////////////////////////////////////////////////////////////// -flat_map& type_register_private::get_custom_name_to_id() +type type_register_private::get_type_by_custom_name(string_view name) { - return m_custom_name_to_id; + std::lock_guard lock(m_mutex); + auto ret = m_custom_name_to_id.find(name); + if (ret != m_custom_name_to_id.end()) + return (*ret); + return detail::get_invalid_type(); } ///////////////////////////////////////////////////////////////////////////////////// diff --git a/src/rttr/detail/type/type_register_p.h b/src/rttr/detail/type/type_register_p.h index 93188251..37eac0bc 100644 --- a/src/rttr/detail/type/type_register_p.h +++ b/src/rttr/detail/type/type_register_p.h @@ -103,7 +103,7 @@ class RTTR_LOCAL type_register_private std::vector& get_type_data_storage(); std::vector& get_type_storage(); flat_map& get_orig_name_to_id(); - flat_map& get_custom_name_to_id(); + type get_type_by_custom_name(string_view name); ///////////////////////////////////////////////////////////////////////////////////// diff --git a/src/rttr/type.cpp b/src/rttr/type.cpp index 77aa4514..9cad791e 100644 --- a/src/rttr/type.cpp +++ b/src/rttr/type.cpp @@ -477,12 +477,7 @@ variant type::invoke(string_view name, std::vector args) type type::get_by_name(string_view name) RTTR_NOEXCEPT { - auto& custom_name_to_id = detail::type_register_private::get_instance().get_custom_name_to_id(); - auto ret = custom_name_to_id.find(name); - if (ret != custom_name_to_id.end()) - return (*ret); - - return detail::get_invalid_type(); + return detail::type_register_private::get_instance().get_type_by_custom_name(name); } /////////////////////////////////////////////////////////////////////////////////////////