Range check for h3KRing

This commit is contained in:
Alexey Milovidov 2020-08-15 11:49:54 +03:00
parent d0eeedd322
commit 1d51d9e8b6
3 changed files with 17 additions and 0 deletions

View File

@ -14,10 +14,13 @@
namespace DB
{
namespace ErrorCodes
{
extern const int ILLEGAL_TYPE_OF_ARGUMENT;
extern const int PARAMETER_OUT_OF_BOUND;
}
class FunctionH3KRing : public IFunction
{
public:
@ -65,6 +68,15 @@ public:
const H3Index origin_hindex = col_hindex->getUInt(row);
const int k = col_k->getInt(row);
/// Overflow is possible. The function maxKringSize does not check for overflow.
/// The calculation is similar to square of k but several times more.
/// Let's use huge underestimation as the safe bound. We should not allow to generate too large arrays nevertheless.
constexpr auto max_k = 10000;
if (k > max_k)
throw Exception(ErrorCodes::PARAMETER_OUT_OF_BOUND, "Too large 'k' argument for {} function, maximum {}", getName(), max_k);
if (k < 0)
throw Exception(ErrorCodes::PARAMETER_OUT_OF_BOUND, "Argument 'k' for {} function must be non negative", getName());
const auto vec_size = maxKringSize(k);
hindex_vec.resize(vec_size);
kRing(origin_hindex, k, hindex_vec.data());

View File

@ -0,0 +1 @@
122

View File

@ -0,0 +1,4 @@
SELECT h3kRing(581276613233082367, 65535); -- { serverError 12 }
SELECT h3kRing(581276613233082367, -1); -- { serverError 12 }
SELECT length(h3kRing(111111111111, 1000));
SELECT h3kRing(581276613233082367, nan); -- { serverError 43 }