Address ubsan on readBinary with bool

This commit is contained in:
Raúl Marín 2022-11-14 15:02:55 +01:00
parent 4988e387f2
commit ee1a1d852d
4 changed files with 15 additions and 3 deletions

View File

@ -153,9 +153,7 @@ public:
void deserialize(AggregateDataPtr __restrict place, ReadBuffer & buf, std::optional<size_t> version, Arena * arena) const override
{
/// When deserializing a bool it might trigger UBSAN if the input is not 0 or 1, so it's better to use a Int8
static_assert(sizeof(bool) == sizeof(Int8));
Int8 flag = true;
bool flag = true;
if constexpr (serialize_flag)
readBinary(flag, buf);
if (flag)

View File

@ -1032,6 +1032,15 @@ template <typename T>
requires is_arithmetic_v<T>
inline void readBinary(T & x, ReadBuffer & buf) { readPODBinary(x, buf); }
inline void readBinary(bool & x, ReadBuffer & buf)
{
/// When deserializing a bool it might trigger UBSAN if the input is not 0 or 1, so it's better to treat it as an Int8
static_assert(sizeof(bool) == sizeof(Int8));
Int8 flag = 0;
readBinary(flag, buf);
x = (flag != 0);
}
inline void readBinary(String & x, ReadBuffer & buf) { readStringBinary(x, buf); }
inline void readBinary(Int128 & x, ReadBuffer & buf) { readPODBinary(x, buf); }
inline void readBinary(Int256 & x, ReadBuffer & buf) { readPODBinary(x, buf); }

View File

@ -0,0 +1 @@
ubsan 30313233343536373839303132333435363738393031323334353637383930313233343536373839303132333435363738393031323334353637383930313233

View File

@ -2,6 +2,10 @@
SELECT finalizeAggregation(CAST(unhex('0F00000030'), 'AggregateFunction(min, String)')); -- { serverError 33 }
SELECT finalizeAggregation(CAST(unhex('FFFF000030'), 'AggregateFunction(min, String)')); -- { serverError 33 }
-- UBSAN
SELECT 'ubsan', hex(finalizeAggregation(CAST(unhex('4000000030313233343536373839303132333435363738393031323334353637383930313233343536373839303132333435363738393031323334353637383930313233010000000000000000'),
'AggregateFunction(argMax, String, UInt64)')));
-- aggThrow should check for errors in its input
SELECT finalizeAggregation(CAST('', 'AggregateFunction(aggThrow(0.), UInt8)')); -- { serverError 32 }