Skip to content

Commit 679c508

Browse files
committed
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
1 parent f822be3 commit 679c508

File tree

3 files changed

+18
-10
lines changed

3 files changed

+18
-10
lines changed

src/backend/access/hash/hashfunc.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,9 @@ hashtext(PG_FUNCTION_ARGS)
298298
buf = palloc(bsize + 1);
299299

300300
rsize = pg_strnxfrm(buf, bsize + 1, keydata, keylen, mylocale);
301-
if (rsize != bsize)
301+
302+
/* the second call may return a smaller value than the first */
303+
if (rsize > bsize)
302304
elog(ERROR, "pg_strnxfrm() returned unexpected result");
303305

304306
/*
@@ -352,7 +354,9 @@ hashtextextended(PG_FUNCTION_ARGS)
352354
buf = palloc(bsize + 1);
353355

354356
rsize = pg_strnxfrm(buf, bsize + 1, keydata, keylen, mylocale);
355-
if (rsize != bsize)
357+
358+
/* the second call may return a smaller value than the first */
359+
if (rsize > bsize)
356360
elog(ERROR, "pg_strnxfrm() returned unexpected result");
357361

358362
/*

src/backend/utils/adt/pg_locale.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2353,9 +2353,9 @@ pg_strxfrm_enabled(pg_locale_t locale)
23532353
* The provided 'src' must be nul-terminated. If 'destsize' is zero, 'dest'
23542354
* may be NULL.
23552355
*
2356-
* Returns the number of bytes needed to store the transformed string,
2357-
* excluding the terminating nul byte. If the value returned is 'destsize' or
2358-
* greater, the resulting contents of 'dest' are undefined.
2356+
* Returns the number of bytes needed (or more) to store the transformed
2357+
* string, excluding the terminating nul byte. If the value returned is
2358+
* 'destsize' or greater, the resulting contents of 'dest' are undefined.
23592359
*/
23602360
size_t
23612361
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)
23852385
* 'src' does not need to be nul-terminated. If 'destsize' is zero, 'dest' may
23862386
* be NULL.
23872387
*
2388-
* Returns the number of bytes needed to store the transformed string,
2389-
* excluding the terminating nul byte. If the value returned is 'destsize' or
2390-
* greater, the resulting contents of 'dest' are undefined.
2388+
* Returns the number of bytes needed (or more) to store the transformed
2389+
* string, excluding the terminating nul byte. If the value returned is
2390+
* 'destsize' or greater, the resulting contents of 'dest' are undefined.
23912391
*
23922392
* This function may need to nul-terminate the argument for libc functions;
23932393
* so if the caller already has a nul-terminated string, it should call

src/backend/utils/adt/varchar.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1028,7 +1028,9 @@ hashbpchar(PG_FUNCTION_ARGS)
10281028
buf = palloc(bsize + 1);
10291029

10301030
rsize = pg_strnxfrm(buf, bsize + 1, keydata, keylen, mylocale);
1031-
if (rsize != bsize)
1031+
1032+
/* the second call may return a smaller value than the first */
1033+
if (rsize > bsize)
10321034
elog(ERROR, "pg_strnxfrm() returned unexpected result");
10331035

10341036
/*
@@ -1084,7 +1086,9 @@ hashbpcharextended(PG_FUNCTION_ARGS)
10841086
buf = palloc(bsize + 1);
10851087

10861088
rsize = pg_strnxfrm(buf, bsize + 1, keydata, keylen, mylocale);
1087-
if (rsize != bsize)
1089+
1090+
/* the second call may return a smaller value than the first */
1091+
if (rsize > bsize)
10881092
elog(ERROR, "pg_strnxfrm() returned unexpected result");
10891093

10901094
/*

0 commit comments

Comments
 (0)