From a64443aaeec78ebd53e41e646d65d30232b5b68f Mon Sep 17 00:00:00 2001 From: =?utf8?q?Rados=C5=82aw=20Zarzy=C5=84ski?= Date: Wed, 30 Aug 2023 15:19:07 +0200 Subject: [PATCH] common/weighted_shuffle: don't feed std::discrete_distribution with all-zero weights MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit This flaw results is the assertions like the following one: ``` /usr/include/c++/11/bits/random.tcc:2667: void std::discrete_distribution<_IntType>::param_type::_M_initialize() [with _IntType = int]: Assertion '__sum > 0' failed. Aborted (core dumped) ``` The reason behind is that `std::discrete_distribution` sums the weights and uses the result as a divisor. Fixes: https://tracker.ceph.com/issues/62645 Signed-off-by: Radosław Zarzyński (cherry picked from commit 1b7a7a8df88ffac007dbafdecc131807de66c046) --- src/common/weighted_shuffle.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/common/weighted_shuffle.h b/src/common/weighted_shuffle.h index 10def0a011a41..dd8f22da014de 100644 --- a/src/common/weighted_shuffle.h +++ b/src/common/weighted_shuffle.h @@ -14,6 +14,8 @@ void weighted_shuffle(RandomIt first, RandomIt last, { if (first == last) { return; + } else if (std::accumulate(weight_first, weight_last, 0) == 0) { + return; } else { std::discrete_distribution d{weight_first, weight_last}; if (auto n = d(g); n > 0) { -- 2.39.5