Relax check for return value from second call of pg_strnxfrm().

strxfrm() is not guaranteed to return the exact number of bytes needed
to store the result; it may return a higher value.

Discussion: https://postgr.es/m/32f85d88d1f64395abfe5a10dd97a62a4d3474ce.camel@j-davis.com
Reviewed-by: Heikki Linnakangas
Backpatch-through: 16
This commit is contained in:
Jeff Davis 2024-07-30 16:23:20 -07:00
parent f822be3962
commit 679c5084cf
3 changed files with 18 additions and 10 deletions

View File

@ -298,7 +298,9 @@ hashtext(PG_FUNCTION_ARGS)
buf = palloc(bsize + 1); buf = palloc(bsize + 1);
rsize = pg_strnxfrm(buf, bsize + 1, keydata, keylen, mylocale); rsize = pg_strnxfrm(buf, bsize + 1, keydata, keylen, mylocale);
if (rsize != bsize)
/* the second call may return a smaller value than the first */
if (rsize > bsize)
elog(ERROR, "pg_strnxfrm() returned unexpected result"); elog(ERROR, "pg_strnxfrm() returned unexpected result");
/* /*
@ -352,7 +354,9 @@ hashtextextended(PG_FUNCTION_ARGS)
buf = palloc(bsize + 1); buf = palloc(bsize + 1);
rsize = pg_strnxfrm(buf, bsize + 1, keydata, keylen, mylocale); rsize = pg_strnxfrm(buf, bsize + 1, keydata, keylen, mylocale);
if (rsize != bsize)
/* the second call may return a smaller value than the first */
if (rsize > bsize)
elog(ERROR, "pg_strnxfrm() returned unexpected result"); elog(ERROR, "pg_strnxfrm() returned unexpected result");
/* /*

View File

@ -2353,9 +2353,9 @@ pg_strxfrm_enabled(pg_locale_t locale)
* The provided 'src' must be nul-terminated. If 'destsize' is zero, 'dest' * The provided 'src' must be nul-terminated. If 'destsize' is zero, 'dest'
* may be NULL. * may be NULL.
* *
* Returns the number of bytes needed to store the transformed string, * Returns the number of bytes needed (or more) to store the transformed
* excluding the terminating nul byte. If the value returned is 'destsize' or * string, excluding the terminating nul byte. If the value returned is
* greater, the resulting contents of 'dest' are undefined. * 'destsize' or greater, the resulting contents of 'dest' are undefined.
*/ */
size_t size_t
pg_strxfrm(char *dest, const char *src, size_t destsize, pg_locale_t locale) pg_strxfrm(char *dest, const char *src, size_t destsize, pg_locale_t locale)
@ -2385,9 +2385,9 @@ pg_strxfrm(char *dest, const char *src, size_t destsize, pg_locale_t locale)
* 'src' does not need to be nul-terminated. If 'destsize' is zero, 'dest' may * 'src' does not need to be nul-terminated. If 'destsize' is zero, 'dest' may
* be NULL. * be NULL.
* *
* Returns the number of bytes needed to store the transformed string, * Returns the number of bytes needed (or more) to store the transformed
* excluding the terminating nul byte. If the value returned is 'destsize' or * string, excluding the terminating nul byte. If the value returned is
* greater, the resulting contents of 'dest' are undefined. * 'destsize' or greater, the resulting contents of 'dest' are undefined.
* *
* This function may need to nul-terminate the argument for libc functions; * This function may need to nul-terminate the argument for libc functions;
* so if the caller already has a nul-terminated string, it should call * so if the caller already has a nul-terminated string, it should call

View File

@ -1028,7 +1028,9 @@ hashbpchar(PG_FUNCTION_ARGS)
buf = palloc(bsize + 1); buf = palloc(bsize + 1);
rsize = pg_strnxfrm(buf, bsize + 1, keydata, keylen, mylocale); rsize = pg_strnxfrm(buf, bsize + 1, keydata, keylen, mylocale);
if (rsize != bsize)
/* the second call may return a smaller value than the first */
if (rsize > bsize)
elog(ERROR, "pg_strnxfrm() returned unexpected result"); elog(ERROR, "pg_strnxfrm() returned unexpected result");
/* /*
@ -1084,7 +1086,9 @@ hashbpcharextended(PG_FUNCTION_ARGS)
buf = palloc(bsize + 1); buf = palloc(bsize + 1);
rsize = pg_strnxfrm(buf, bsize + 1, keydata, keylen, mylocale); rsize = pg_strnxfrm(buf, bsize + 1, keydata, keylen, mylocale);
if (rsize != bsize)
/* the second call may return a smaller value than the first */
if (rsize > bsize)
elog(ERROR, "pg_strnxfrm() returned unexpected result"); elog(ERROR, "pg_strnxfrm() returned unexpected result");
/* /*