From 44a1070930bd4f10e5ef7c564d94a280f5a667a1 Mon Sep 17 00:00:00 2001 From: Henry Schreiner Date: Thu, 21 Aug 2025 11:41:29 -0400 Subject: [PATCH] fix: limit warning issue Signed-off-by: Henry Schreiner --- include/pybind11/detail/internals.h | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/include/pybind11/detail/internals.h b/include/pybind11/detail/internals.h index 46f44d9b10..4ea374861e 100644 --- a/include/pybind11/detail/internals.h +++ b/include/pybind11/detail/internals.h @@ -18,7 +18,9 @@ #include "struct_smart_holder.h" #include +#include #include +#include #include #include @@ -274,8 +276,10 @@ struct internals { registered_exception_translators.push_front(&translate_exception); #ifdef Py_GIL_DISABLED // Scale proportional to the number of cores. 2x is a heuristic to reduce contention. - auto num_shards - = static_cast(round_up_to_next_pow2(2 * std::thread::hardware_concurrency())); + // Make sure the number isn't unreasonable by limiting it to 16 bits (65K) + auto num_shards = static_cast( + std::min(round_up_to_next_pow2(2 * std::thread::hardware_concurrency()), + std::numeric_limits::max())); if (num_shards == 0) { num_shards = 1; }