Skip to content

Commit

Permalink
fix thread-safety issues
Browse files Browse the repository at this point in the history
`custom_name_to_id` was being used in thread-unsafe way by
`type::get_by_name`. Fixed that.
Original commit: rttrorg@a0cee4f

(cherry picked from commit 618a5f0)
  • Loading branch information
cgnx-zpal committed Nov 24, 2022
1 parent 2a96dfe commit 986140f
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 9 deletions.
8 changes: 6 additions & 2 deletions src/rttr/detail/type/type_register.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1122,9 +1122,13 @@ flat_map<string_view, type>& type_register_private::get_orig_name_to_id()

/////////////////////////////////////////////////////////////////////////////////////

flat_map<std::string, type, hash>& 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<std::mutex> 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();
}

/////////////////////////////////////////////////////////////////////////////////////
Expand Down
2 changes: 1 addition & 1 deletion src/rttr/detail/type/type_register_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ class RTTR_LOCAL type_register_private
std::vector<type_data*>& get_type_data_storage();
std::vector<type>& get_type_storage();
flat_map<string_view, type>& get_orig_name_to_id();
flat_map<std::string, type, hash>& get_custom_name_to_id();
type get_type_by_custom_name(string_view name);

/////////////////////////////////////////////////////////////////////////////////////

Expand Down
7 changes: 1 addition & 6 deletions src/rttr/type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -486,12 +486,7 @@ variant type::invoke(string_view name, std::vector<argument> 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);
}

/////////////////////////////////////////////////////////////////////////////////////////
Expand Down

0 comments on commit 986140f

Please sign in to comment.