Fix UBSan report in mapOp

This commit is contained in:
Alexey Milovidov 2021-03-31 04:07:18 +03:00
parent e2b25affe5
commit a59c66c266
3 changed files with 11 additions and 3 deletions

View File

@ -120,6 +120,12 @@ private:
return res; return res;
} }
template <typename T>
static inline auto NO_SANITIZE_UNDEFINED negate(T x)
{
return -x;
}
template <typename KeyType, bool is_str_key, typename ValType> template <typename KeyType, bool is_str_key, typename ValType>
ColumnPtr execute2(size_t row_count, TupleMaps & args, const DataTypeTuple & res_type) const ColumnPtr execute2(size_t row_count, TupleMaps & args, const DataTypeTuple & res_type) const
{ {
@ -159,14 +165,14 @@ private:
if constexpr (is_str_key) if constexpr (is_str_key)
{ {
// have to use Field structs to get strings // have to use Field structs to get strings
key = arg.key_column.operator[](offset + j).get<KeyType>(); key = arg.key_column[offset + j].get<KeyType>();
} }
else else
{ {
key = assert_cast<const ColumnVector<KeyType> &>(arg.key_column).getData()[offset + j]; key = assert_cast<const ColumnVector<KeyType> &>(arg.key_column).getData()[offset + j];
} }
auto value = arg.val_column.operator[](offset + j).get<ValType>(); ValType value = arg.val_column[offset + j].get<ValType>();
if constexpr (op_type == OpTypes::ADD) if constexpr (op_type == OpTypes::ADD)
{ {
@ -177,7 +183,7 @@ private:
else else
{ {
static_assert(op_type == OpTypes::SUBTRACT); static_assert(op_type == OpTypes::SUBTRACT);
const auto [it, inserted] = summing_map.insert({key, first ? value : -value}); const auto [it, inserted] = summing_map.insert({key, first ? value : negate(value)});
if (!inserted) if (!inserted)
it->second -= value; it->second -= value;
} }

View File

@ -0,0 +1 @@
\N (([0,10,255],[-9223372036854775808,1025,0]),[255,NULL]) \N ([0,255],3,[-2]) [NULL]

View File

@ -0,0 +1 @@
SELECT toInt32([toUInt8(NULL)], NULL), (mapSubtract(([toUInt8(256), 10], [toInt32(-9223372036854775808), 1025]), ([toUInt8(65535), 0], [toInt16(0.), -9223372036854775808])), [toUInt8(-1), toInt32(([toUInt8(9223372036854775807), -1], [toInt32(255), 65536]), NULL)]), toUInt8(([2, 9223372036854775807], [toFloat32('0.0000065536'), 2]), 9223372036854775807, NULL), ([toUInt8(1024), 255], toUInt8(3), [toInt16(-2)]), [NULL];