From 6b6789e74cfec37c49994d82037a8c654c1cd6cb Mon Sep 17 00:00:00 2001 From: Meng-Hsiu Chiang Date: Wed, 13 May 2026 18:17:05 +0000 Subject: [PATCH] Derive command_lengths from command_array at compile time Replace the manually-maintained command_lengths array with a constexpr lambda that computes string lengths from command_array using std::char_traits::length(). This eliminates the error-prone need to keep the two arrays in sync manually. All new code of the whole pull request, including one or several files that are either new files or modified ones, are contributed under the BSD-new license. I am contributing on behalf of my employer Amazon Web Services, Inc. --- sql/sql_acl.cc | 24 ++++++++++-------------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/sql/sql_acl.cc b/sql/sql_acl.cc index 0a271dab40efb..28a4c97651a12 100644 --- a/sql/sql_acl.cc +++ b/sql/sql_acl.cc @@ -56,6 +56,7 @@ #include "sql_audit.h" #include "password.h" #include "scope.h" +#include #include "sql_plugin_compat.h" #include "wsrep_mysqld.h" @@ -9419,7 +9420,7 @@ static void add_user_parameters(THD *thd, String *result, ACL_USER* acl_user, } } -static const char *command_array[]= +static constexpr const char *command_array[]= { "SELECT", "INSERT", "UPDATE", "DELETE", "CREATE", "DROP", "RELOAD", "SHUTDOWN", "PROCESS","FILE", "GRANT", "REFERENCES", "INDEX", @@ -9432,23 +9433,18 @@ static const char *command_array[]= "BINLOG REPLAY", "SLAVE MONITOR", "SHOW CREATE ROUTINE" }; -static uint command_lengths[]= -{ - 6, 6, 6, 6, 6, 4, 6, - 8, 7, 4, 5, 10, 5, - 5, 14, 5, 23, - 11, 7, 17, 14, - 11, 9, 14, 13, - 11, 5, 7, 17, 14, - 8, 15, 16, 15, - 23, 24, 12, - 13, 13, 19 -}; +static constexpr auto command_lengths= []() constexpr { + std::array lengths{}; + for (size_t i= 0; i < lengths.size(); i++) + lengths[i]= static_cast( + std::char_traits::length(command_array[i])); + return lengths; +}(); static_assert(array_elements(command_array) == PRIVILEGE_T_MAX_BIT + 1, "The definition of command_array does not match privilege_t"); -static_assert(array_elements(command_lengths) == PRIVILEGE_T_MAX_BIT + 1, +static_assert(command_lengths.size() == PRIVILEGE_T_MAX_BIT + 1, "The definition of command_lengths does not match privilege_t");