From 6f2afb16ff4822e4b2f40a960cd8c3c71a2bf5cd Mon Sep 17 00:00:00 2001 From: Andrew Kane Date: Fri, 26 Apr 2024 17:27:09 -0700 Subject: [PATCH] Use consistent error message for sparsevec index out of bounds [skip ci] --- src/sparsevec.c | 31 +++++++------------------------ test/expected/sparsevec.out | 12 ++++++------ 2 files changed, 13 insertions(+), 30 deletions(-) diff --git a/src/sparsevec.c b/src/sparsevec.c index 8cd3773..e7b5141 100644 --- a/src/sparsevec.c +++ b/src/sparsevec.c @@ -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++) diff --git a/test/expected/sparsevec.out b/test/expected/sparsevec.out index 1f653f8..989ec82 100644 --- a/test/expected/sparsevec.out +++ b/test/expected/sparsevec.out @@ -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);