Skip to content

Commit cddec2b

Browse files
authored
fix: limit warning issue (#5807)
Signed-off-by: Henry Schreiner <[email protected]>
1 parent e71489c commit cddec2b

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

include/pybind11/detail/internals.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818
#include "struct_smart_holder.h"
1919

2020
#include <atomic>
21+
#include <cstdint>
2122
#include <exception>
23+
#include <limits>
2224
#include <mutex>
2325
#include <thread>
2426

@@ -274,8 +276,10 @@ struct internals {
274276
registered_exception_translators.push_front(&translate_exception);
275277
#ifdef Py_GIL_DISABLED
276278
// Scale proportional to the number of cores. 2x is a heuristic to reduce contention.
277-
auto num_shards
278-
= static_cast<size_t>(round_up_to_next_pow2(2 * std::thread::hardware_concurrency()));
279+
// Make sure the number isn't unreasonable by limiting it to 16 bits (65K)
280+
auto num_shards = static_cast<std::uint16_t>(
281+
std::min<std::size_t>(round_up_to_next_pow2(2 * std::thread::hardware_concurrency()),
282+
std::numeric_limits<std::uint16_t>::max()));
279283
if (num_shards == 0) {
280284
num_shards = 1;
281285
}

0 commit comments

Comments
 (0)