diff options
author | Andres Freund <andres@anarazel.de> | 2025-02-10 10:03:40 -0500 |
---|---|---|
committer | Andres Freund <andres@anarazel.de> | 2025-02-10 10:03:40 -0500 |
commit | db3eb0e8256a7089d16cb6ed1ea7a65654c0e105 (patch) | |
tree | 0ec4af31c90820568052d53b49ed0e31d7f7f5d9 /src/test | |
parent | 00f1a1f665f078f5abadbf8baddc5c187fba80f8 (diff) |
Add pg_encoding_set_invalid()
There are cases where we cannot / do not want to error out for invalidly
encoded input. In such cases it can be useful to replace e.g. an incomplete
multi-byte characters with bytes that will trigger an error when getting
validated as part of a larger string.
Unfortunately, until now, for some encoding no such sequence existed. For
those encodings this commit removes one previously accepted input combination
- we consider that to be ok, as the chosen bytes are outside of the valid
ranges for the encodings, we just previously failed to detect that.
As we cannot add a new field to pg_wchar_table without breaking ABI, this is
implemented "in-line" in the newly added function.
Author: Noah Misch <noah@leadboat.com>
Reviewed-by: Andres Freund <andres@anarazel.de>
Backpatch-through: 13
Security: CVE-2025-1094
Diffstat (limited to 'src/test')
-rw-r--r-- | src/test/regress/expected/conversion.out | 4 | ||||
-rw-r--r-- | src/test/regress/input/create_function_1.source | 4 | ||||
-rw-r--r-- | src/test/regress/output/create_function_1.source | 3 | ||||
-rw-r--r-- | src/test/regress/regress.c | 51 | ||||
-rw-r--r-- | src/test/regress/sql/conversion.sql | 3 |
5 files changed, 65 insertions, 0 deletions
diff --git a/src/test/regress/expected/conversion.out b/src/test/regress/expected/conversion.out index 62c10671685..f052783d84f 100644 --- a/src/test/regress/expected/conversion.out +++ b/src/test/regress/expected/conversion.out @@ -1,6 +1,10 @@ -- -- create user defined conversion -- +SELECT FROM test_enc_setup(); +-- +(1 row) + CREATE USER regress_conversion_user WITH NOCREATEDB NOCREATEROLE; SET SESSION AUTHORIZATION regress_conversion_user; CREATE CONVERSION myconv FOR 'LATIN1' TO 'UTF8' FROM iso8859_1_to_utf8; diff --git a/src/test/regress/input/create_function_1.source b/src/test/regress/input/create_function_1.source index 412e339fcf2..b230a8a6a81 100644 --- a/src/test/regress/input/create_function_1.source +++ b/src/test/regress/input/create_function_1.source @@ -62,6 +62,10 @@ CREATE FUNCTION test_atomic_ops() AS '@libdir@/regress@DLSUFFIX@' LANGUAGE C; +CREATE FUNCTION test_enc_setup() RETURNS void + AS '@libdir@/regress@DLSUFFIX@', 'test_enc_setup' + LANGUAGE C STRICT; + -- Tests creating a FDW handler CREATE FUNCTION test_fdw_handler() RETURNS fdw_handler diff --git a/src/test/regress/output/create_function_1.source b/src/test/regress/output/create_function_1.source index 4d78fa12289..4e4db33612b 100644 --- a/src/test/regress/output/create_function_1.source +++ b/src/test/regress/output/create_function_1.source @@ -55,6 +55,9 @@ CREATE FUNCTION test_atomic_ops() RETURNS bool AS '@libdir@/regress@DLSUFFIX@' LANGUAGE C; +CREATE FUNCTION test_enc_setup() RETURNS void + AS '@libdir@/regress@DLSUFFIX@', 'test_enc_setup' + LANGUAGE C STRICT; -- Tests creating a FDW handler CREATE FUNCTION test_fdw_handler() RETURNS fdw_handler diff --git a/src/test/regress/regress.c b/src/test/regress/regress.c index aa9fef866fa..481588c9a44 100644 --- a/src/test/regress/regress.c +++ b/src/test/regress/regress.c @@ -29,6 +29,7 @@ #include "commands/trigger.h" #include "executor/executor.h" #include "executor/spi.h" +#include "mb/pg_wchar.h" #include "miscadmin.h" #include "nodes/supportnodes.h" #include "optimizer/optimizer.h" @@ -1088,3 +1089,53 @@ test_opclass_options_func(PG_FUNCTION_ARGS) { PG_RETURN_NULL(); } + +/* one-time tests for encoding infrastructure */ +PG_FUNCTION_INFO_V1(test_enc_setup); +Datum +test_enc_setup(PG_FUNCTION_ARGS) +{ + /* Test pg_encoding_set_invalid() */ + for (int i = 0; i < _PG_LAST_ENCODING_; i++) + { + char buf[2], + bigbuf[16]; + int len, + mblen, + valid; + + if (pg_encoding_max_length(i) == 1) + continue; + pg_encoding_set_invalid(i, buf); + len = strnlen(buf, 2); + if (len != 2) + elog(WARNING, + "official invalid string for encoding \"%s\" has length %d", + pg_enc2name_tbl[i].name, len); + mblen = pg_encoding_mblen(i, buf); + if (mblen != 2) + elog(WARNING, + "official invalid string for encoding \"%s\" has mblen %d", + pg_enc2name_tbl[i].name, mblen); + valid = pg_encoding_verifymbstr(i, buf, len); + if (valid != 0) + elog(WARNING, + "official invalid string for encoding \"%s\" has valid prefix of length %d", + pg_enc2name_tbl[i].name, valid); + valid = pg_encoding_verifymbstr(i, buf, 1); + if (valid != 0) + elog(WARNING, + "first byte of official invalid string for encoding \"%s\" has valid prefix of length %d", + pg_enc2name_tbl[i].name, valid); + memset(bigbuf, ' ', sizeof(bigbuf)); + bigbuf[0] = buf[0]; + bigbuf[1] = buf[1]; + valid = pg_encoding_verifymbstr(i, bigbuf, sizeof(bigbuf)); + if (valid != 0) + elog(WARNING, + "trailing data changed official invalid string for encoding \"%s\" to have valid prefix of length %d", + pg_enc2name_tbl[i].name, valid); + } + + PG_RETURN_VOID(); +} diff --git a/src/test/regress/sql/conversion.sql b/src/test/regress/sql/conversion.sql index 02cf39f1ce9..0f01190e9d9 100644 --- a/src/test/regress/sql/conversion.sql +++ b/src/test/regress/sql/conversion.sql @@ -1,6 +1,9 @@ -- -- create user defined conversion -- + +SELECT FROM test_enc_setup(); + CREATE USER regress_conversion_user WITH NOCREATEDB NOCREATEROLE; SET SESSION AUTHORIZATION regress_conversion_user; CREATE CONVERSION myconv FOR 'LATIN1' TO 'UTF8' FROM iso8859_1_to_utf8; |