From 0e25aaf1d92c08a4a7b94de214e46213a05b8478 Mon Sep 17 00:00:00 2001 From: Senichenkov Date: Thu, 28 Nov 2024 12:17:04 +0300 Subject: [PATCH] Fix UB found by clang UB sanitizer - Check that column is numeric *before* casting it to INumericType in DataStats::GetMedianAD - Don't process empty vector in GetPartition in gfd_validation.cpp - Disable tests causing float-to-long cast when result is infinity --- src/core/algorithms/gfd/gfd_validation.cpp | 4 ++++ src/core/algorithms/statistics/data_stats.cpp | 2 +- src/tests/test_types.cpp | 14 ++++++++++---- 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/src/core/algorithms/gfd/gfd_validation.cpp b/src/core/algorithms/gfd/gfd_validation.cpp index 1f9bf64dec..39a3e4b0c9 100644 --- a/src/core/algorithms/gfd/gfd_validation.cpp +++ b/src/core/algorithms/gfd/gfd_validation.cpp @@ -25,6 +25,10 @@ std::vector> GetPartition(std::vector const& can config::ThreadNumType const& threads_num) { std::vector> result = {}; + if (candidates.empty()) { + return {}; + } + int musthave = candidates.size() / threads_num; int oversized_num = candidates.size() % threads_num; diff --git a/src/core/algorithms/statistics/data_stats.cpp b/src/core/algorithms/statistics/data_stats.cpp index 809cf42863..121b044c8a 100644 --- a/src/core/algorithms/statistics/data_stats.cpp +++ b/src/core/algorithms/statistics/data_stats.cpp @@ -464,8 +464,8 @@ Statistic DataStats::GetMedianAD(size_t index) const { return all_stats_[index].median_ad; } mo::TypedColumnData const& col = col_data_[index]; - auto const& type = static_cast(col.GetType()); if (!col.IsNumeric()) return {}; + auto const& type = static_cast(col.GetType()); std::vector data = DeleteNullAndEmpties(index); std::byte* median = MedianOfNumericVector(data, type); diff --git a/src/tests/test_types.cpp b/src/tests/test_types.cpp index 7e11b6b55f..38fcb95e18 100644 --- a/src/tests/test_types.cpp +++ b/src/tests/test_types.cpp @@ -182,10 +182,16 @@ TYPED_TEST(TestNumeric, Pow) { test(0, 100); test(22, 12); - test(123, 321); - test(Type(2.72), Type(1.3123141)); - test(-102, 11); - test(-123, 123); + // 123^321 won't fit into long (i. e. IntType) -- it's UB + if constexpr (!std::is_same_v) { + test(123, 321); + } + test(Type(2.72), 1.3123141); + // -102^11 and -123^123 won't fit into long (i. e. IntType) -- it's UB + if constexpr (!std::is_same_v) { + test(-102, 11); + test(-123, 123); + } test(-21, -7); }