Skip to content

Commit

Permalink
Fix step overflow in range()
Browse files Browse the repository at this point in the history
  • Loading branch information
azat committed Aug 15, 2020
1 parent 79bde24 commit e9d373b
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/Functions/array/range.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,14 @@ class FunctionRange : public IFunction
for (size_t row_idx = 0; row_idx < input_rows_count; ++row_idx)
{
for (size_t st = start, ed = end_data[row_idx]; st < ed; st += step)
{
out_data[offset++] = st;

if (st > st + step)
throw Exception{"A call to function " + getName() + " overflows, investigate the values of arguments you are passing",
ErrorCodes::ARGUMENT_OUT_OF_BOUND};
}

out_offsets[row_idx] = offset;
}

Expand Down Expand Up @@ -200,8 +206,14 @@ class FunctionRange : public IFunction
for (size_t row_idx = 0; row_idx < input_rows_count; ++row_idx)
{
for (size_t st = start_data[row_idx], ed = end_data[row_idx]; st < ed; st += step)
{
out_data[offset++] = st;

if (st > st + step)
throw Exception{"A call to function " + getName() + " overflows, investigate the values of arguments you are passing",
ErrorCodes::ARGUMENT_OUT_OF_BOUND};
}

out_offsets[row_idx] = offset;
}

Expand Down Expand Up @@ -255,8 +267,14 @@ class FunctionRange : public IFunction
for (size_t row_idx = 0; row_idx < input_rows_count; ++row_idx)
{
for (size_t st = start, ed = end_data[row_idx]; st < ed; st += step_data[row_idx])
{
out_data[offset++] = st;

if (st > st + step_data[row_idx])
throw Exception{"A call to function " + getName() + " overflows, investigate the values of arguments you are passing",
ErrorCodes::ARGUMENT_OUT_OF_BOUND};
}

out_offsets[row_idx] = offset;
}

Expand Down Expand Up @@ -313,8 +331,14 @@ class FunctionRange : public IFunction
for (size_t row_idx = 0; row_idx < input_rows_count; ++row_idx)
{
for (size_t st = start_data[row_idx], ed = end_start[row_idx]; st < ed; st += step_data[row_idx])
{
out_data[offset++] = st;

if (st > st + step_data[row_idx])
throw Exception{"A call to function " + getName() + " overflows, investigate the values of arguments you are passing",
ErrorCodes::ARGUMENT_OUT_OF_BOUND};
}

out_offsets[row_idx] = offset;
}

Expand Down
Empty file.
12 changes: 12 additions & 0 deletions tests/queries/0_stateless/01408_range_overflow.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
-- executeGeneric()
SELECT range(1025, 1048576 + 9223372036854775807, 9223372036854775807); -- { serverError 69; }
SELECT range(1025, 1048576 + (9223372036854775807 AS i), i); -- { serverError 69; }

-- executeConstStep()
SELECT range(number, 1048576 + 9223372036854775807, 9223372036854775807) FROM system.numbers LIMIT 1 OFFSET 1025; -- { serverError 69; }

-- executeConstStartStep()
SELECT range(1025, number + 9223372036854775807, 9223372036854775807) FROM system.numbers LIMIT 1 OFFSET 1048576; -- { serverError 69; }

-- executeConstStart()
SELECT range(1025, 1048576 + 9223372036854775807, number + 9223372036854775807) FROM system.numbers LIMIT 1; -- { serverError 69; }

0 comments on commit e9d373b

Please sign in to comment.