Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 10fdc67

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 0d57dc2 commit 10fdc67

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
@@ -2373,9 +2373,9 @@ pg_strxfrm_enabled(pg_locale_t locale)
23732373
* The provided 'src' must be nul-terminated. If 'destsize' is zero, 'dest'
23742374
* may be NULL.
23752375
*
2376-
* Returns the number of bytes needed to store the transformed string,
2377-
* excluding the terminating nul byte. If the value returned is 'destsize' or
2378-
* greater, the resulting contents of 'dest' are undefined.
2376+
* Returns the number of bytes needed (or more) to store the transformed
2377+
* string, excluding the terminating nul byte. If the value returned is
2378+
* 'destsize' or greater, the resulting contents of 'dest' are undefined.
23792379
*/
23802380
size_t
23812381
pg_strxfrm(char *dest, const char *src, size_t destsize, pg_locale_t locale)
@@ -2405,9 +2405,9 @@ pg_strxfrm(char *dest, const char *src, size_t destsize, pg_locale_t locale)
24052405
* 'src' does not need to be nul-terminated. If 'destsize' is zero, 'dest' may
24062406
* be NULL.
24072407
*
2408-
* Returns the number of bytes needed to store the transformed string,
2409-
* excluding the terminating nul byte. If the value returned is 'destsize' or
2410-
* greater, the resulting contents of 'dest' are undefined.
2408+
* Returns the number of bytes needed (or more) to store the transformed
2409+
* string, excluding the terminating nul byte. If the value returned is
2410+
* 'destsize' or greater, the resulting contents of 'dest' are undefined.
24112411
*
24122412
* This function may need to nul-terminate the argument for libc functions;
24132413
* 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)