diff --git a/src/sparsevec.c b/src/sparsevec.c index 3dd1380..70d9ac9 100644 --- a/src/sparsevec.c +++ b/src/sparsevec.c @@ -197,7 +197,7 @@ sparsevec_in(PG_FUNCTION_ARGS) { char *lit = PG_GETARG_CSTRING(0); int32 typmod = PG_GETARG_INT32(2); - int dim; + long dim; char *pt = lit; char *stringEnd; SparseVector *result; @@ -244,7 +244,7 @@ sparsevec_in(PG_FUNCTION_ARGS) { for (;;) { - int index; + long index; float value; /* TODO Better error */ @@ -263,18 +263,17 @@ sparsevec_in(PG_FUNCTION_ARGS) errmsg("invalid input syntax for type sparsevec: \"%s\"", lit))); /* Use similar logic as int2vectorin */ - errno = 0; - index = strtoint(pt, &stringEnd, 10); + index = strtol(pt, &stringEnd, 10); if (stringEnd == pt) ereport(ERROR, (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION), errmsg("invalid input syntax for type sparsevec: \"%s\"", lit))); - if (errno == ERANGE) - ereport(ERROR, - (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE), - errmsg("index \"%s\" is out of range for type sparsevec", pnstrdup(pt, stringEnd - pt)))); + if (index > INT_MAX) + index = INT_MAX; + else if (index < INT_MIN) + index = INT_MIN; pt = stringEnd; @@ -352,18 +351,17 @@ sparsevec_in(PG_FUNCTION_ARGS) pt++; /* Use similar logic as int2vectorin */ - errno = 0; - dim = strtoint(pt, &stringEnd, 10); + dim = strtol(pt, &stringEnd, 10); if (stringEnd == pt) ereport(ERROR, (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION), errmsg("invalid input syntax for type sparsevec: \"%s\"", lit))); - if (errno == ERANGE) - ereport(ERROR, - (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE), - errmsg("dimensions \"%s\" is out of range for type sparsevec", pnstrdup(pt, stringEnd - pt)))); + if (dim > INT_MAX) + dim = INT_MAX; + else if (dim < INT_MIN) + dim = INT_MIN; pt = stringEnd; diff --git a/test/expected/sparsevec_input.out b/test/expected/sparsevec_input.out index 88552ca..dd1bc70 100644 --- a/test/expected/sparsevec_input.out +++ b/test/expected/sparsevec_input.out @@ -192,19 +192,19 @@ ERROR: sparsevec cannot have more than 1000000 dimensions LINE 1: SELECT '{}/1000001'::sparsevec; ^ SELECT '{}/2147483648'::sparsevec; -ERROR: dimensions "2147483648" is out of range for type sparsevec +ERROR: sparsevec cannot have more than 1000000 dimensions LINE 1: SELECT '{}/2147483648'::sparsevec; ^ SELECT '{}/-2147483649'::sparsevec; -ERROR: dimensions "-2147483649" is out of range for type sparsevec +ERROR: sparsevec must have at least 1 dimension LINE 1: SELECT '{}/-2147483649'::sparsevec; ^ SELECT '{}/9223372036854775808'::sparsevec; -ERROR: dimensions "9223372036854775808" is out of range for type sparsevec +ERROR: sparsevec cannot have more than 1000000 dimensions LINE 1: SELECT '{}/9223372036854775808'::sparsevec; ^ SELECT '{}/-9223372036854775809'::sparsevec; -ERROR: dimensions "-9223372036854775809" is out of range for type sparsevec +ERROR: sparsevec must have at least 1 dimension LINE 1: SELECT '{}/-9223372036854775809'::sparsevec; ^ SELECT '{2147483647:1}/1'::sparsevec; @@ -212,7 +212,7 @@ ERROR: index must be less than or equal to dimensions LINE 1: SELECT '{2147483647:1}/1'::sparsevec; ^ SELECT '{2147483648:1}/1'::sparsevec; -ERROR: index "2147483648" is out of range for type sparsevec +ERROR: index must be less than or equal to dimensions LINE 1: SELECT '{2147483648:1}/1'::sparsevec; ^ SELECT '{-2147483648:1}/1'::sparsevec; @@ -220,7 +220,7 @@ ERROR: index must be greater than zero LINE 1: SELECT '{-2147483648:1}/1'::sparsevec; ^ SELECT '{-2147483649:1}/1'::sparsevec; -ERROR: index "-2147483649" is out of range for type sparsevec +ERROR: index must be greater than zero LINE 1: SELECT '{-2147483649:1}/1'::sparsevec; ^ SELECT '{0:1}/1'::sparsevec;