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/regress/regress.c | |
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/regress/regress.c')
-rw-r--r-- | src/test/regress/regress.c | 51 |
1 files changed, 51 insertions, 0 deletions
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(); +} |