aboutsummaryrefslogtreecommitdiff
path: root/src/backend/utils/adt/oracle_compat.c
diff options
context:
space:
mode:
authorPeter Eisentraut <peter@eisentraut.org>2021-12-06 13:26:50 +0100
committerPeter Eisentraut <peter@eisentraut.org>2021-12-06 13:37:11 +0100
commite9e63b7022ddd0aaaae7cd439daa234cf9e6a21c (patch)
tree2f2af5e4f4c36e3f7146411116c5f56783db3146 /src/backend/utils/adt/oracle_compat.c
parentd4596a20d046e800644d6027613c6a2cb5a6c35e (diff)
downloadpostgresql-e9e63b7022ddd0aaaae7cd439daa234cf9e6a21c.tar.gz
postgresql-e9e63b7022ddd0aaaae7cd439daa234cf9e6a21c.zip
Fix inappropriate uses of PG_GETARG_UINT32()
The chr() function used PG_GETARG_UINT32() even though the argument is declared as (signed) integer. As a result, you can pass negative arguments to this function and it internally interprets them as positive. Ultimately ends up being harmless, but it seems wrong, so fix this and rearrange the internal error checking a bit to accommodate this. Another case was in the documentation, where example code used PG_GETARG_UINT32() with an argument declared as signed integer. Reviewed-by: Nathan Bossart <bossartn@amazon.com> Discussion: https://www.postgresql.org/message-id/flat/7e43869b-d412-8f81-30a3-809783edc9a3%40enterprisedb.com
Diffstat (limited to 'src/backend/utils/adt/oracle_compat.c')
-rw-r--r--src/backend/utils/adt/oracle_compat.c33
1 files changed, 20 insertions, 13 deletions
diff --git a/src/backend/utils/adt/oracle_compat.c b/src/backend/utils/adt/oracle_compat.c
index f737aa6fbde..ec99f2b738e 100644
--- a/src/backend/utils/adt/oracle_compat.c
+++ b/src/backend/utils/adt/oracle_compat.c
@@ -999,10 +999,26 @@ ascii(PG_FUNCTION_ARGS)
Datum
chr (PG_FUNCTION_ARGS)
{
- uint32 cvalue = PG_GETARG_UINT32(0);
+ int32 arg = PG_GETARG_INT32(0);
+ uint32 cvalue;
text *result;
int encoding = GetDatabaseEncoding();
+ /*
+ * Error out on arguments that make no sense or that we can't validly
+ * represent in the encoding.
+ */
+ if (arg < 0)
+ ereport(ERROR,
+ (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
+ errmsg("character number must be positive")));
+ else if (arg == 0)
+ ereport(ERROR,
+ (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
+ errmsg("null character not permitted")));
+
+ cvalue = arg;
+
if (encoding == PG_UTF8 && cvalue > 127)
{
/* for Unicode we treat the argument as a code point */
@@ -1017,7 +1033,7 @@ chr (PG_FUNCTION_ARGS)
if (cvalue > 0x0010ffff)
ereport(ERROR,
(errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
- errmsg("requested character too large for encoding: %d",
+ errmsg("requested character too large for encoding: %u",
cvalue)));
if (cvalue > 0xffff)
@@ -1058,28 +1074,19 @@ chr (PG_FUNCTION_ARGS)
if (!pg_utf8_islegal(wch, bytes))
ereport(ERROR,
(errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
- errmsg("requested character not valid for encoding: %d",
+ errmsg("requested character not valid for encoding: %u",
cvalue)));
}
else
{
bool is_mb;
- /*
- * Error out on arguments that make no sense or that we can't validly
- * represent in the encoding.
- */
- if (cvalue == 0)
- ereport(ERROR,
- (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
- errmsg("null character not permitted")));
-
is_mb = pg_encoding_max_length(encoding) > 1;
if ((is_mb && (cvalue > 127)) || (!is_mb && (cvalue > 255)))
ereport(ERROR,
(errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
- errmsg("requested character too large for encoding: %d",
+ errmsg("requested character too large for encoding: %u",
cvalue)));
result = (text *) palloc(VARHDRSZ + 1);