diff options
| author | Tom Lane | 2022-12-27 16:40:01 +0000 |
|---|---|---|
| committer | Tom Lane | 2022-12-27 16:40:01 +0000 |
| commit | eb8312a22a84c33fc405ae9b497113973f552f90 (patch) | |
| tree | 3d7a135bafd3fd06750a6586e69225447743caae /src/test | |
| parent | 63c844a0a5d70cdbd6ae0470d582d39e75ad8d66 (diff) | |
Detect bad input for types xid, xid8, and cid.
Historically these input functions just called strtoul or strtoull
and returned the result, with no error detection whatever. Upgrade
them to reject garbage input and out-of-range values, similarly to
our other numeric input routines.
To share the code for this with type oid, adjust the existing
"oidin_subr" to be agnostic about the SQL name of the type it is
handling, and move it to numutils.c; then clone it for 64-bit types.
Because the xid types previously accepted hex and octal input by
reason of calling strtoul[l] with third argument zero, I made the
common subroutine do that too, with the consequence that type oid
now also accepts hex and octal input. In view of 6fcda9aba, that
seems like a good thing.
While at it, simplify the existing over-complicated handling of
syntax errors from strtoul: we only need one ereturn not three.
Discussion: https://postgr.es/m/3526121.1672000729@sss.pgh.pa.us
Diffstat (limited to 'src/test')
| -rw-r--r-- | src/test/regress/expected/xid.out | 61 | ||||
| -rw-r--r-- | src/test/regress/sql/xid.sql | 10 |
2 files changed, 54 insertions, 17 deletions
diff --git a/src/test/regress/expected/xid.out b/src/test/regress/expected/xid.out index c7b8d299c8..e62f701943 100644 --- a/src/test/regress/expected/xid.out +++ b/src/test/regress/expected/xid.out @@ -13,29 +13,58 @@ select '010'::xid, 8 | 42 | 4294967295 | 4294967295 | 8 | 42 | 18446744073709551615 | 18446744073709551615 (1 row) --- garbage values are not yet rejected (perhaps they should be) +-- garbage values select ''::xid; - xid ------ - 0 +ERROR: invalid input syntax for type xid: "" +LINE 1: select ''::xid; + ^ +select 'asdf'::xid; +ERROR: invalid input syntax for type xid: "asdf" +LINE 1: select 'asdf'::xid; + ^ +select ''::xid8; +ERROR: invalid input syntax for type xid8: "" +LINE 1: select ''::xid8; + ^ +select 'asdf'::xid8; +ERROR: invalid input syntax for type xid8: "asdf" +LINE 1: select 'asdf'::xid8; + ^ +-- Also try it with non-error-throwing API +SELECT pg_input_is_valid('42', 'xid'); + pg_input_is_valid +------------------- + t (1 row) -select 'asdf'::xid; - xid ------ - 0 +SELECT pg_input_is_valid('asdf', 'xid'); + pg_input_is_valid +------------------- + f (1 row) -select ''::xid8; - xid8 ------- - 0 +SELECT pg_input_error_message('0xffffffffff', 'xid'); + pg_input_error_message +--------------------------------------------------- + value "0xffffffffff" is out of range for type xid (1 row) -select 'asdf'::xid8; - xid8 ------- - 0 +SELECT pg_input_is_valid('42', 'xid8'); + pg_input_is_valid +------------------- + t +(1 row) + +SELECT pg_input_is_valid('asdf', 'xid8'); + pg_input_is_valid +------------------- + f +(1 row) + +SELECT pg_input_error_message('0xffffffffffffffffffff', 'xid8'); + pg_input_error_message +-------------------------------------------------------------- + value "0xffffffffffffffffffff" is out of range for type xid8 (1 row) -- equality diff --git a/src/test/regress/sql/xid.sql b/src/test/regress/sql/xid.sql index 2289803681..b6996588ef 100644 --- a/src/test/regress/sql/xid.sql +++ b/src/test/regress/sql/xid.sql @@ -10,12 +10,20 @@ select '010'::xid, '0xffffffffffffffff'::xid8, '-1'::xid8; --- garbage values are not yet rejected (perhaps they should be) +-- garbage values select ''::xid; select 'asdf'::xid; select ''::xid8; select 'asdf'::xid8; +-- Also try it with non-error-throwing API +SELECT pg_input_is_valid('42', 'xid'); +SELECT pg_input_is_valid('asdf', 'xid'); +SELECT pg_input_error_message('0xffffffffff', 'xid'); +SELECT pg_input_is_valid('42', 'xid8'); +SELECT pg_input_is_valid('asdf', 'xid8'); +SELECT pg_input_error_message('0xffffffffffffffffffff', 'xid8'); + -- equality select '1'::xid = '1'::xid; select '1'::xid != '1'::xid; |
