Skip to content

Commit

Permalink
Fix ubsan in AggregateFunctionMinMaxAny::read with high sizes
Browse files Browse the repository at this point in the history
  • Loading branch information
Algunenano committed Nov 15, 2022
1 parent 7e73b18 commit 143b67d
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 2 deletions.
7 changes: 6 additions & 1 deletion src/AggregateFunctions/AggregateFunctionMinMaxAny.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ namespace ErrorCodes
{
extern const int ILLEGAL_TYPE_OF_ARGUMENT;
extern const int NOT_IMPLEMENTED;
extern const int TOO_LARGE_STRING_SIZE;
}

/** Aggregate functions that store one of passed values.
Expand Down Expand Up @@ -521,7 +522,11 @@ struct SingleValueDataString //-V730
{
if (capacity < rhs_size)
{
capacity = static_cast<UInt32>(roundUpToPowerOfTwoOrZero(rhs_size));
capacity = static_cast<Int32>(roundUpToPowerOfTwoOrZero(rhs_size));
/// It might happen if the size was too big and the rounded value does not fit a size_t
if (unlikely(capacity <= rhs_size))
throw Exception(ErrorCodes::TOO_LARGE_STRING_SIZE, "String size is too big ({})", rhs_size);

/// Don't free large_data here.
large_data = arena->alloc(capacity);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Common/Arena.h
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ class Arena : private boost::noncopyable
/// Get piece of memory, without alignment.
char * alloc(size_t size)
{
if (unlikely(head->pos + size > head->end))
if (unlikely(static_cast<std::ptrdiff_t>(size) > head->end - head->pos))
addMemoryChunk(size);

char * res = head->pos;
Expand Down
Empty file.
3 changes: 3 additions & 0 deletions tests/queries/0_stateless/02481_i43247_ubsan_in_minmaxany.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
-- https://github.com/ClickHouse/ClickHouse/issues/43247
SELECT finalizeAggregation(CAST('AggregateFunction(categoricalInformationValue, Nullable(UInt8), UInt8)AggregateFunction(categoricalInformationValue, Nullable(UInt8), UInt8)',
'AggregateFunction(min, String)')); -- { serverError 131 }

0 comments on commit 143b67d

Please sign in to comment.