Skip to content

Commit

Permalink
Refactored usage of std::string_view for c-interfaces
Browse files Browse the repository at this point in the history
As std::string_view::data() is not guaranteed to be null-terminated, it is
not safe
to user it in old c-functions accepting 'const char *'.
Some constants converted to arrays of char 'const char buffer[]{"value"}'.
  • Loading branch information
percona-ysorokin authored and oleksandr-kachan committed Apr 25, 2024
1 parent a873799 commit d2def5c
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,9 @@ namespace {
using global_command_services = masking_functions::primitive_singleton<
masking_functions::command_service_tuple>;

constexpr std::string_view psi_category_name{"masking_functions"};
constexpr std::string_view flusher_thd_psi_name{
"masking_functions_dict_flusher"};
constexpr std::string_view flusher_thd_psi_os_name{"mf_flusher"};
constexpr const char psi_category_name[]{"masking_functions"};
constexpr const char flusher_thd_psi_name[]{"masking_functions_dict_flusher"};
constexpr const char flusher_thd_psi_os_name[]{"mf_flusher"};

} // anonymous namespace

Expand All @@ -56,12 +55,12 @@ query_cache::query_cache(query_builder_ptr query_builder,

if (m_flusher_interval_seconds > 0) {
PSI_thread_info thread_info{&m_psi_flusher_thread_key,
flusher_thd_psi_name.data(),
flusher_thd_psi_os_name.data(),
flusher_thd_psi_name,
flusher_thd_psi_os_name,
PSI_FLAG_SINGLETON,
0,
PSI_DOCUMENT_ME};
mysql_thread_register(psi_category_name.data(), &thread_info, 1);
mysql_thread_register(psi_category_name, &thread_info, 1);

const auto res =
mysql_thread_create(m_psi_flusher_thread_key, &m_flusher_thread,
Expand Down
25 changes: 11 additions & 14 deletions components/masking_functions/src/masking_functions/sys_vars.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,12 @@ using global_component_sys_variable_services =
masking_functions::primitive_singleton<
masking_functions::component_sys_variable_service_tuple>;

using str_arg_check_type = STR_CHECK_ARG(str);
using ulonglong_arg_check_type = INTEGRAL_CHECK_ARG(ulonglong);

constexpr std::string_view component_name{"masking_functions"};
constexpr std::string_view masking_database_var_name{"masking_database"};
constexpr std::string_view flush_interval_var_name{
constexpr const char component_name[]{"masking_functions"};
constexpr const char masking_database_var_name[]{"masking_database"};
constexpr const char flush_interval_var_name[]{
"dictionaries_flush_interval_seconds"};

std::string default_database_name{"mysql"};
char default_database_name[]{"mysql"};
const ulonglong default_flush_interval_seconds = 0;

bool is_database_name_initialised = false;
Expand All @@ -62,11 +59,11 @@ std::uint64_t get_flush_interval_seconds() noexcept {
}

bool register_sys_vars() {
str_arg_check_type check_db_name{default_database_name.data()};
STR_CHECK_ARG(str) check_db_name{default_database_name};

const auto &services{global_component_sys_variable_services::instance()};
if (services.registrator->register_variable(
component_name.data(), masking_database_var_name.data(),
component_name, masking_database_var_name,
PLUGIN_VAR_STR | PLUGIN_VAR_MEMALLOC | PLUGIN_VAR_RQCMDARG |
PLUGIN_VAR_READONLY,
"Specifies the database to use for data masking dictionaries "
Expand All @@ -77,11 +74,11 @@ bool register_sys_vars() {
}
is_database_name_initialised = true;

ulonglong_arg_check_type check_flush_interval{default_flush_interval_seconds,
0, ULLONG_MAX, 1};
INTEGRAL_CHECK_ARG(ulonglong)
check_flush_interval{default_flush_interval_seconds, 0, ULLONG_MAX, 1};

if (services.registrator->register_variable(
component_name.data(), flush_interval_var_name.data(),
component_name, flush_interval_var_name,
PLUGIN_VAR_LONGLONG | PLUGIN_VAR_UNSIGNED | PLUGIN_VAR_RQCMDARG |
PLUGIN_VAR_READONLY,
"Sets the interval, in seconds, to wait before attempting to "
Expand All @@ -103,13 +100,13 @@ bool unregister_sys_vars() {
const auto &services{global_component_sys_variable_services::instance()};
if (is_database_name_initialised &&
services.unregistrator->unregister_variable(
component_name.data(), masking_database_var_name.data()) != 0) {
component_name, masking_database_var_name) != 0) {
is_success = false;
}

if (is_flush_interval_initialised &&
services.unregistrator->unregister_variable(
component_name.data(), flush_interval_var_name.data()) != 0) {
component_name, flush_interval_var_name) != 0) {
is_success = false;
}

Expand Down

0 comments on commit d2def5c

Please sign in to comment.