Skip to content

Commit

Permalink
Fix UB found by clang UB sanitizer
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
p-senichenkov committed Nov 29, 2024
1 parent ce4ea48 commit 1464239
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 5 deletions.
6 changes: 6 additions & 0 deletions src/core/algorithms/gfd/gfd_validation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#include <set>
#include <thread>

#include <easylogging++.h>

#include <boost/graph/eccentricity.hpp>
#include <boost/graph/exterior_property.hpp>
#include <boost/graph/floyd_warshall_shortest.hpp>
Expand All @@ -25,6 +27,10 @@ std::vector<std::vector<vertex_t>> GetPartition(std::vector<vertex_t> const& can
config::ThreadNumType const& threads_num) {
std::vector<std::vector<vertex_t>> result = {};

if (candidates.empty()) {
return {};
}

int musthave = candidates.size() / threads_num;
int oversized_num = candidates.size() % threads_num;

Expand Down
2 changes: 1 addition & 1 deletion src/core/algorithms/statistics/data_stats.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<mo::INumericType const&>(col.GetType());
if (!col.IsNumeric()) return {};
auto const& type = static_cast<mo::INumericType const&>(col.GetType());

std::vector<std::byte const*> data = DeleteNullAndEmpties(index);
std::byte* median = MedianOfNumericVector(data, type);
Expand Down
14 changes: 10 additions & 4 deletions src/tests/test_types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<Type, long>) {
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<Type, long>) {
test(-102, 11);
test(-123, 123);
}
test(-21, -7);
}

Expand Down

0 comments on commit 1464239

Please sign in to comment.