Use consistent error message for sparsevec index out of bounds [skip ci]

This commit is contained in:
Andrew Kane
2024-04-26 17:27:09 -07:00
parent 0959e69529
commit 6f2afb16ff
2 changed files with 13 additions and 30 deletions

View File

@@ -94,32 +94,15 @@ CheckNnz(int nnz, int dim)
* Ensure valid index
*/
static inline void
CheckIndex(int32 *indices, int i, int dim, bool text)
CheckIndex(int32 *indices, int i, int dim)
{
int32 index = indices[i];
if (index < 0)
if (index < 0 || index >= dim)
{
if (text)
ereport(ERROR,
(errcode(ERRCODE_DATA_EXCEPTION),
errmsg("sparsevec index out of bounds (< 1)")));
else
ereport(ERROR,
(errcode(ERRCODE_DATA_EXCEPTION),
errmsg("sparsevec index out of bounds for binary representation (< 0)")));
}
if (index >= dim)
{
if (text)
ereport(ERROR,
(errcode(ERRCODE_DATA_EXCEPTION),
errmsg("sparsevec index out of bounds (> dimensions)")));
else
ereport(ERROR,
(errcode(ERRCODE_DATA_EXCEPTION),
errmsg("sparsevec index out of bounds for binary representation (>= dimensions)")));
ereport(ERROR,
(errcode(ERRCODE_DATA_EXCEPTION),
errmsg("sparsevec index out of bounds")));
}
if (i > 0)
@@ -404,7 +387,7 @@ sparsevec_in(PG_FUNCTION_ARGS)
result->indices[i] = elements[i].index;
rvalues[i] = elements[i].value;
CheckIndex(result->indices, i, dim, true);
CheckIndex(result->indices, i, dim);
}
PG_RETURN_POINTER(result);
@@ -543,7 +526,7 @@ sparsevec_recv(PG_FUNCTION_ARGS)
for (int i = 0; i < nnz; i++)
{
result->indices[i] = pq_getmsgint(buf, sizeof(int32));
CheckIndex(result->indices, i, dim, false);
CheckIndex(result->indices, i, dim);
}
for (int i = 0; i < nnz; i++)

View File

@@ -208,27 +208,27 @@ ERROR: sparsevec must have at least 1 dimension
LINE 1: SELECT '{}/-9223372036854775809'::sparsevec;
^
SELECT '{2147483647:1}/1'::sparsevec;
ERROR: sparsevec index out of bounds (> dimensions)
ERROR: sparsevec index out of bounds
LINE 1: SELECT '{2147483647:1}/1'::sparsevec;
^
SELECT '{2147483648:1}/1'::sparsevec;
ERROR: sparsevec index out of bounds (> dimensions)
ERROR: sparsevec index out of bounds
LINE 1: SELECT '{2147483648:1}/1'::sparsevec;
^
SELECT '{-2147483648:1}/1'::sparsevec;
ERROR: sparsevec index out of bounds (< 1)
ERROR: sparsevec index out of bounds
LINE 1: SELECT '{-2147483648:1}/1'::sparsevec;
^
SELECT '{-2147483649:1}/1'::sparsevec;
ERROR: sparsevec index out of bounds (< 1)
ERROR: sparsevec index out of bounds
LINE 1: SELECT '{-2147483649:1}/1'::sparsevec;
^
SELECT '{0:1}/1'::sparsevec;
ERROR: sparsevec index out of bounds (< 1)
ERROR: sparsevec index out of bounds
LINE 1: SELECT '{0:1}/1'::sparsevec;
^
SELECT '{2:1}/1'::sparsevec;
ERROR: sparsevec index out of bounds (> dimensions)
ERROR: sparsevec index out of bounds
LINE 1: SELECT '{2:1}/1'::sparsevec;
^
SELECT '{}/3'::sparsevec(3);