2323#include < algorithm>
2424#include < cmath>
2525#include < sstream>
26+ #include < type_traits>
2627
2728#include " common_defs.hpp"
2829#include " memory_operations.hpp"
@@ -43,6 +44,14 @@ inline void check_not_infinite(T value, const char* name) {
4344 }
4445}
4546
47+ template <typename T>
48+ inline void check_non_zero (T value, const char * name) {
49+ static_assert (std::is_arithmetic<T>::value, " T must be an arithmetic type" );
50+ if (value == 0 ) {
51+ throw std::invalid_argument (std::string (name) + " must not be zero" );
52+ }
53+ }
54+
4655template <typename T, typename A>
4756tdigest<T, A>::tdigest(uint16_t k, const A& allocator):
4857tdigest (false , k, std::numeric_limits<T>::infinity(), -std::numeric_limits<T>::infinity(), vector_centroid(allocator), 0, vector_t(allocator))
@@ -437,6 +446,8 @@ tdigest<T, A> tdigest<T, A>::deserialize(std::istream& is, const A& allocator) {
437446 for (const auto & c: centroids) {
438447 check_not_nan (c.get_mean (), " centroid mean" );
439448 check_not_infinite (c.get_mean (), " centroid mean" );
449+ check_non_zero (c.get_weight (), " centroid weight" );
450+
440451 weight += c.get_weight ();
441452 }
442453 for (const auto & value: buffer) {
@@ -508,6 +519,8 @@ tdigest<T, A> tdigest<T, A>::deserialize(const void* bytes, size_t size, const A
508519 for (const auto & c: centroids) {
509520 check_not_nan (c.get_mean (), " centroid mean" );
510521 check_not_infinite (c.get_mean (), " centroid mean" );
522+ check_non_zero (c.get_weight (), " centroid weight" );
523+
511524 weight += c.get_weight ();
512525 }
513526 for (const auto & value: buffer) {
@@ -542,6 +555,8 @@ tdigest<T, A> tdigest<T, A>::deserialize_compat(std::istream& is, const A& alloc
542555 const auto weight_double = read_big_endian<double >(is);
543556 check_not_nan (weight_double, " centroid weight" );
544557 check_not_infinite (weight_double, " centroid weight" );
558+ check_non_zero (weight_double, " centroid weight" );
559+
545560 const auto mean = read_big_endian<double >(is);
546561 check_not_nan (mean, " centroid mean" );
547562 check_not_infinite (mean, " centroid mean" );
0 commit comments