From cbb24d9aa5978c1a2d008f9ebf208acd9ee9eaa9 Mon Sep 17 00:00:00 2001 From: ParadoxV5 Date: Thu, 16 Jan 2025 20:57:01 -0700 Subject: [PATCH] MDEV-35646: Limit `pseudo_thread_id` to `UINT32_MAX` MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Although the `my_thread_id` type is 64 bits, binlog format specs limits it to 32 bits in practice. (See also: MDEV-35706) The writable SQL variable `pseudo_thread_id` didn’t realize this though and had a range of `ULONGLONG_MAX` (at least `UINT64_MAX` in C/C++). It consequentially accepted larger values silently, but only the lower 32 bits of whom gets binlogged; this could lead to inconsistency. Reviewed-by: Brandon Nesterenko --- include/my_pthread.h | 7 +++++++ mysql-test/suite/sys_vars/r/sysvars_server_embedded.result | 2 +- .../suite/sys_vars/r/sysvars_server_notembedded.result | 2 +- sql/mysqld.cc | 2 +- sql/sys_vars.cc | 2 +- 5 files changed, 11 insertions(+), 4 deletions(-) diff --git a/include/my_pthread.h b/include/my_pthread.h index 10cc2301ea6..519d1be1cb5 100644 --- a/include/my_pthread.h +++ b/include/my_pthread.h @@ -641,6 +641,13 @@ extern pthread_mutexattr_t my_errorcheck_mutexattr; #endif typedef uint64 my_thread_id; +/** + Long-standing formats (such as the client-server protocol and the binary log) + hard-coded `my_thread_id` to 32 bits in practice. (Though not all + `thread_id`s are typed as such, @ref my_thread_id itself among those.) + @see MDEV-35706 +*/ +#define MY_THREAD_ID_MAX UINT32_MAX extern void my_threadattr_global_init(void); extern my_bool my_thread_global_init(void); diff --git a/mysql-test/suite/sys_vars/r/sysvars_server_embedded.result b/mysql-test/suite/sys_vars/r/sysvars_server_embedded.result index a5a02bfa1bc..cdf6d210933 100644 --- a/mysql-test/suite/sys_vars/r/sysvars_server_embedded.result +++ b/mysql-test/suite/sys_vars/r/sysvars_server_embedded.result @@ -2877,7 +2877,7 @@ VARIABLE_SCOPE SESSION ONLY VARIABLE_TYPE BIGINT UNSIGNED VARIABLE_COMMENT This variable is for internal server use NUMERIC_MIN_VALUE 0 -NUMERIC_MAX_VALUE 18446744073709551615 +NUMERIC_MAX_VALUE 4294967295 NUMERIC_BLOCK_SIZE 1 ENUM_VALUE_LIST NULL READ_ONLY NO diff --git a/mysql-test/suite/sys_vars/r/sysvars_server_notembedded.result b/mysql-test/suite/sys_vars/r/sysvars_server_notembedded.result index 50dd4ae23a3..0af08d00c89 100644 --- a/mysql-test/suite/sys_vars/r/sysvars_server_notembedded.result +++ b/mysql-test/suite/sys_vars/r/sysvars_server_notembedded.result @@ -3037,7 +3037,7 @@ VARIABLE_SCOPE SESSION ONLY VARIABLE_TYPE BIGINT UNSIGNED VARIABLE_COMMENT This variable is for internal server use NUMERIC_MIN_VALUE 0 -NUMERIC_MAX_VALUE 18446744073709551615 +NUMERIC_MAX_VALUE 4294967295 NUMERIC_BLOCK_SIZE 1 ENUM_VALUE_LIST NULL READ_ONLY NO diff --git a/sql/mysqld.cc b/sql/mysqld.cc index d6dd8a566af..7a7c1f0491a 100644 --- a/sql/mysqld.cc +++ b/sql/mysqld.cc @@ -9770,7 +9770,7 @@ void init_server_psi_keys(void) */ -static my_thread_id thread_id_max= UINT_MAX32; +static my_thread_id thread_id_max= MY_THREAD_ID_MAX; #include #include diff --git a/sql/sys_vars.cc b/sql/sys_vars.cc index 93298ff7b04..a2058dee224 100644 --- a/sql/sys_vars.cc +++ b/sql/sys_vars.cc @@ -1767,7 +1767,7 @@ Sys_pseudo_thread_id( "pseudo_thread_id", "This variable is for internal server use", SESSION_ONLY(pseudo_thread_id), - NO_CMD_LINE, VALID_RANGE(0, ULONGLONG_MAX), DEFAULT(0), + NO_CMD_LINE, VALID_RANGE(0, MY_THREAD_ID_MAX), DEFAULT(0), BLOCK_SIZE(1), NO_MUTEX_GUARD, IN_BINLOG); static bool