1
0
mirror of https://github.com/postgres/postgres.git synced 2026-10-23 13:29:09 +03:00

Tighten tid input parsing with strtoul() to reject empty fields

strtoul() informs that in the case of no conversion, "endptr" is set
equal to the location of the input pointer, which is something that
tidin() has never checked.

This allowed inputs like "(,)", "(1,)" or "(,1)" to be accepted on some
platforms, like Linux where "(1,)" implied "(1,0)", but rejected on
others like macos where "(1,)" is rejected, due to the fact that errno
may not be set.

This change may cause some issues as some tid values will now be
rejected, but at least we get the consistent strtoul() experience.
If it proves to be a pain for some, it could always be reverted.
Another point of this change is that we get one step closer to a
possible common strtoul() where the error handling could be unified in
core, with all the errors handled the same way for errno and no input
parsed, at least.

Reported-by: Michael Malis <malis@pgrust.com>
Author: Tristan Partin <tristan@partin.io>
Discussion: https://postgr.es/m/19584-e60c446ba6f57c9c@postgresql.org
Discussion: https://postgr.es/m/DKBS2Z9CGARC.2T07O6TJYSE8B@partin.io
This commit is contained in:
Michael Paquier
2026-08-06 15:39:56 +09:00
parent 4790abd112
commit 58ff4a0a08
3 changed files with 12 additions and 3 deletions

View File

@@ -73,7 +73,7 @@ tidin(PG_FUNCTION_ARGS)
errno = 0;
cvt = strtoul(coord[0], &badp, 10);
if (errno || *badp != DELIM)
if (badp == coord[0] || errno || *badp != DELIM)
ereturn(escontext, (Datum) 0,
(errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
errmsg("invalid input syntax for type %s: \"%s\"",
@@ -95,8 +95,7 @@ tidin(PG_FUNCTION_ARGS)
#endif
cvt = strtoul(coord[1], &badp, 10);
if (errno || *badp != RDELIM ||
cvt > USHRT_MAX)
if (badp == coord[1] || errno || *badp != RDELIM || cvt > USHRT_MAX)
ereturn(escontext, (Datum) 0,
(errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
errmsg("invalid input syntax for type %s: \"%s\"",

View File

@@ -17,6 +17,14 @@ SELECT '(1,65536)'::tid; -- error
ERROR: invalid input syntax for type tid: "(1,65536)"
LINE 1: SELECT '(1,65536)'::tid;
^
SELECT '(,1)'::tid; -- error
ERROR: invalid input syntax for type tid: "(,1)"
LINE 1: SELECT '(,1)'::tid;
^
SELECT '(1,)'::tid; -- error
ERROR: invalid input syntax for type tid: "(1,)"
LINE 1: SELECT '(1,)'::tid;
^
-- Also try it with non-error-throwing API
SELECT pg_input_is_valid('(0)', 'tid');
pg_input_is_valid

View File

@@ -8,6 +8,8 @@ SELECT
SELECT '(4294967296,1)'::tid; -- error
SELECT '(1,65536)'::tid; -- error
SELECT '(,1)'::tid; -- error
SELECT '(1,)'::tid; -- error
-- Also try it with non-error-throwing API
SELECT pg_input_is_valid('(0)', 'tid');