From f59fa67050cd5407789824626826837e3cb6b107 Mon Sep 17 00:00:00 2001 From: Nikita Vasilev Date: Thu, 15 Aug 2019 21:46:16 +0300 Subject: [PATCH 1/2] added type checks for set index functions --- dbms/src/Functions/bitBoolMaskAnd.cpp | 6 ++++++ dbms/src/Functions/bitBoolMaskOr.cpp | 6 ++++++ dbms/src/Functions/bitSwapLastTwo.cpp | 6 ++++++ dbms/src/Functions/bitWrapperFunc.cpp | 8 +++++++- 4 files changed, 25 insertions(+), 1 deletion(-) diff --git a/dbms/src/Functions/bitBoolMaskAnd.cpp b/dbms/src/Functions/bitBoolMaskAnd.cpp index f7cf7efa25f..02e681b59b8 100644 --- a/dbms/src/Functions/bitBoolMaskAnd.cpp +++ b/dbms/src/Functions/bitBoolMaskAnd.cpp @@ -4,6 +4,10 @@ namespace DB { + namespace ErrorCodes + { + extern const int BAD_CAST; + } /// Working with UInt8: last bit = can be true, previous = can be false (Like dbms/src/Storages/MergeTree/BoolMask.h). /// This function provides "AND" operation for BoolMasks. @@ -17,6 +21,8 @@ namespace DB template static inline Result apply(A left, B right) { + if constexpr (!std::is_same_v || !std::is_same_v) + throw DB::Exception("Only UInt8 type is supported by __bitBoolMaskAnd.", ErrorCodes::BAD_CAST); return static_cast( ((static_cast(left) & static_cast(right)) & 1) | ((((static_cast(left) >> 1) | (static_cast(right) >> 1)) & 1) << 1)); diff --git a/dbms/src/Functions/bitBoolMaskOr.cpp b/dbms/src/Functions/bitBoolMaskOr.cpp index 0c34b8e5bdb..d1261f4fe14 100644 --- a/dbms/src/Functions/bitBoolMaskOr.cpp +++ b/dbms/src/Functions/bitBoolMaskOr.cpp @@ -4,6 +4,10 @@ namespace DB { + namespace ErrorCodes + { + extern const int BAD_CAST; + } /// Working with UInt8: last bit = can be true, previous = can be false (Like dbms/src/Storages/MergeTree/BoolMask.h). /// This function provides "OR" operation for BoolMasks. @@ -17,6 +21,8 @@ namespace DB template static inline Result apply(A left, B right) { + if constexpr (!std::is_same_v || !std::is_same_v) + throw DB::Exception("Only UInt8 type is supported by __bitBoolMaskOr.", ErrorCodes::BAD_CAST); return static_cast( ((static_cast(left) | static_cast(right)) & 1) | ((((static_cast(left) >> 1) & (static_cast(right) >> 1)) & 1) << 1)); diff --git a/dbms/src/Functions/bitSwapLastTwo.cpp b/dbms/src/Functions/bitSwapLastTwo.cpp index 4a3b0cd304a..7b3f92a0724 100644 --- a/dbms/src/Functions/bitSwapLastTwo.cpp +++ b/dbms/src/Functions/bitSwapLastTwo.cpp @@ -4,6 +4,10 @@ namespace DB { + namespace ErrorCodes + { + extern const int BAD_CAST; + } /// Working with UInt8: last bit = can be true, previous = can be false (Like dbms/src/Storages/MergeTree/BoolMask.h). /// This function provides "NOT" operation for BoolMasks by swapping last two bits ("can be true" <-> "can be false"). @@ -14,6 +18,8 @@ namespace DB static inline ResultType NO_SANITIZE_UNDEFINED apply(A a) { + if constexpr (!std::is_same_v) + throw DB::Exception("Only UInt8 type is supported by __bitSwapLastTwo.", ErrorCodes::BAD_CAST); return static_cast( ((static_cast(a) & 1) << 1) | ((static_cast(a) >> 1) & 1)); } diff --git a/dbms/src/Functions/bitWrapperFunc.cpp b/dbms/src/Functions/bitWrapperFunc.cpp index b9e37838875..c8951de66d1 100644 --- a/dbms/src/Functions/bitWrapperFunc.cpp +++ b/dbms/src/Functions/bitWrapperFunc.cpp @@ -4,6 +4,10 @@ namespace DB { + namespace ErrorCodes + { + extern const int BAD_CAST; + } /// Working with UInt8: last bit = can be true, previous = can be false (Like dbms/src/Storages/MergeTree/BoolMask.h). /// This function wraps bool atomic functions @@ -15,7 +19,9 @@ namespace DB static inline ResultType NO_SANITIZE_UNDEFINED apply(A a) { - return a == static_cast(0) ? static_cast(0b10) : static_cast(0b1); + if constexpr (!std::is_integral_v) + throw DB::Exception("It's a bug! Only integer types are supported by __bitWrapperFunc.", ErrorCodes::BAD_CAST); + return a == 0 ? static_cast(0b10) : static_cast(0b1); } #if USE_EMBEDDED_COMPILER From a051009d2896780942e10014021822faf0859107 Mon Sep 17 00:00:00 2001 From: Nikita Vasilev Date: Thu, 15 Aug 2019 21:48:48 +0300 Subject: [PATCH 2/2] add it's a bug mes --- dbms/src/Functions/bitBoolMaskAnd.cpp | 2 +- dbms/src/Functions/bitBoolMaskOr.cpp | 2 +- dbms/src/Functions/bitSwapLastTwo.cpp | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/dbms/src/Functions/bitBoolMaskAnd.cpp b/dbms/src/Functions/bitBoolMaskAnd.cpp index 02e681b59b8..eaa1a2a3343 100644 --- a/dbms/src/Functions/bitBoolMaskAnd.cpp +++ b/dbms/src/Functions/bitBoolMaskAnd.cpp @@ -22,7 +22,7 @@ namespace DB static inline Result apply(A left, B right) { if constexpr (!std::is_same_v || !std::is_same_v) - throw DB::Exception("Only UInt8 type is supported by __bitBoolMaskAnd.", ErrorCodes::BAD_CAST); + throw DB::Exception("It's a bug! Only UInt8 type is supported by __bitBoolMaskAnd.", ErrorCodes::BAD_CAST); return static_cast( ((static_cast(left) & static_cast(right)) & 1) | ((((static_cast(left) >> 1) | (static_cast(right) >> 1)) & 1) << 1)); diff --git a/dbms/src/Functions/bitBoolMaskOr.cpp b/dbms/src/Functions/bitBoolMaskOr.cpp index d1261f4fe14..903c3582375 100644 --- a/dbms/src/Functions/bitBoolMaskOr.cpp +++ b/dbms/src/Functions/bitBoolMaskOr.cpp @@ -22,7 +22,7 @@ namespace DB static inline Result apply(A left, B right) { if constexpr (!std::is_same_v || !std::is_same_v) - throw DB::Exception("Only UInt8 type is supported by __bitBoolMaskOr.", ErrorCodes::BAD_CAST); + throw DB::Exception("It's a bug! Only UInt8 type is supported by __bitBoolMaskOr.", ErrorCodes::BAD_CAST); return static_cast( ((static_cast(left) | static_cast(right)) & 1) | ((((static_cast(left) >> 1) & (static_cast(right) >> 1)) & 1) << 1)); diff --git a/dbms/src/Functions/bitSwapLastTwo.cpp b/dbms/src/Functions/bitSwapLastTwo.cpp index 7b3f92a0724..22b7b889d8b 100644 --- a/dbms/src/Functions/bitSwapLastTwo.cpp +++ b/dbms/src/Functions/bitSwapLastTwo.cpp @@ -19,7 +19,7 @@ namespace DB static inline ResultType NO_SANITIZE_UNDEFINED apply(A a) { if constexpr (!std::is_same_v) - throw DB::Exception("Only UInt8 type is supported by __bitSwapLastTwo.", ErrorCodes::BAD_CAST); + throw DB::Exception("It's a bug! Only UInt8 type is supported by __bitSwapLastTwo.", ErrorCodes::BAD_CAST); return static_cast( ((static_cast(a) & 1) << 1) | ((static_cast(a) >> 1) & 1)); }