From ebe0d849b57cc5893acf28b571e42b10aef382c5 Mon Sep 17 00:00:00 2001 From: Alexey Milovidov Date: Tue, 4 May 2021 18:18:51 +0300 Subject: [PATCH] Fix error in serialization of UInt256 --- src/IO/WriteIntText.h | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/src/IO/WriteIntText.h b/src/IO/WriteIntText.h index 93444e7df73..55b384220e4 100644 --- a/src/IO/WriteIntText.h +++ b/src/IO/WriteIntText.h @@ -4,8 +4,24 @@ #include #include -/// 40 digits or 39 digits and a sign -#define WRITE_HELPERS_MAX_INT_WIDTH 40U + +namespace +{ + template constexpr size_t max_int_width = 0; + template <> constexpr size_t max_int_width = 3; /// 255 + template <> constexpr size_t max_int_width = 4; /// -128 + template <> constexpr size_t max_int_width = 5; /// 65535 + template <> constexpr size_t max_int_width = 6; /// -32768 + template <> constexpr size_t max_int_width = 10; /// 4294967295 + template <> constexpr size_t max_int_width = 11; /// -2147483648 + template <> constexpr size_t max_int_width = 20; /// 18446744073709551615 + template <> constexpr size_t max_int_width = 20; /// -9223372036854775808 + template <> constexpr size_t max_int_width = 39; /// 340282366920938463463374607431768211455 + template <> constexpr size_t max_int_width = 40; /// -170141183460469231731687303715884105728 + template <> constexpr size_t max_int_width = 78; /// 115792089237316195423570985008687907853269984665640564039457584007913129639935 + template <> constexpr size_t max_int_width = 78; /// -57896044618658097711785492504343953926634992332820282019728792003956564819968 +} + namespace DB { @@ -15,7 +31,7 @@ namespace detail template void NO_INLINE writeUIntTextFallback(T x, WriteBuffer & buf) { - char tmp[WRITE_HELPERS_MAX_INT_WIDTH]; + char tmp[max_int_width]; int len = itoa(x, tmp) - tmp; buf.write(tmp, len); } @@ -24,7 +40,7 @@ namespace detail template void writeIntText(T x, WriteBuffer & buf) { - if (likely(reinterpret_cast(buf.position()) + WRITE_HELPERS_MAX_INT_WIDTH < reinterpret_cast(buf.buffer().end()))) + if (likely(reinterpret_cast(buf.position()) + max_int_width < reinterpret_cast(buf.buffer().end()))) buf.position() = itoa(x, buf.position()); else detail::writeUIntTextFallback(x, buf);