Fix error in serialization of UInt256

This commit is contained in:
Alexey Milovidov 2021-05-04 18:18:51 +03:00
parent 86169cc36e
commit ebe0d849b5

View File

@ -4,8 +4,24 @@
#include <IO/WriteBuffer.h> #include <IO/WriteBuffer.h>
#include <common/itoa.h> #include <common/itoa.h>
/// 40 digits or 39 digits and a sign
#define WRITE_HELPERS_MAX_INT_WIDTH 40U namespace
{
template <typename T> constexpr size_t max_int_width = 0;
template <> constexpr size_t max_int_width<UInt8> = 3; /// 255
template <> constexpr size_t max_int_width<Int8> = 4; /// -128
template <> constexpr size_t max_int_width<UInt16> = 5; /// 65535
template <> constexpr size_t max_int_width<Int16> = 6; /// -32768
template <> constexpr size_t max_int_width<UInt32> = 10; /// 4294967295
template <> constexpr size_t max_int_width<Int32> = 11; /// -2147483648
template <> constexpr size_t max_int_width<UInt64> = 20; /// 18446744073709551615
template <> constexpr size_t max_int_width<Int64> = 20; /// -9223372036854775808
template <> constexpr size_t max_int_width<UInt128> = 39; /// 340282366920938463463374607431768211455
template <> constexpr size_t max_int_width<Int128> = 40; /// -170141183460469231731687303715884105728
template <> constexpr size_t max_int_width<UInt256> = 78; /// 115792089237316195423570985008687907853269984665640564039457584007913129639935
template <> constexpr size_t max_int_width<Int256> = 78; /// -57896044618658097711785492504343953926634992332820282019728792003956564819968
}
namespace DB namespace DB
{ {
@ -15,7 +31,7 @@ namespace detail
template <typename T> template <typename T>
void NO_INLINE writeUIntTextFallback(T x, WriteBuffer & buf) void NO_INLINE writeUIntTextFallback(T x, WriteBuffer & buf)
{ {
char tmp[WRITE_HELPERS_MAX_INT_WIDTH]; char tmp[max_int_width<T>];
int len = itoa(x, tmp) - tmp; int len = itoa(x, tmp) - tmp;
buf.write(tmp, len); buf.write(tmp, len);
} }
@ -24,7 +40,7 @@ namespace detail
template <typename T> template <typename T>
void writeIntText(T x, WriteBuffer & buf) void writeIntText(T x, WriteBuffer & buf)
{ {
if (likely(reinterpret_cast<intptr_t>(buf.position()) + WRITE_HELPERS_MAX_INT_WIDTH < reinterpret_cast<intptr_t>(buf.buffer().end()))) if (likely(reinterpret_cast<uintptr_t>(buf.position()) + max_int_width<T> < reinterpret_cast<uintptr_t>(buf.buffer().end())))
buf.position() = itoa(x, buf.position()); buf.position() = itoa(x, buf.position());
else else
detail::writeUIntTextFallback(x, buf); detail::writeUIntTextFallback(x, buf);