Relax check for return value from second call of pg_strnxfrm().
authorJeff Davis <[email protected]>
Tue, 30 Jul 2024 23:23:20 +0000 (16:23 -0700)
committerJeff Davis <[email protected]>
Tue, 30 Jul 2024 23:25:21 +0000 (16:25 -0700)
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[email protected]
Reviewed-by: Heikki Linnakangas
Backpatch-through: 16

src/backend/access/hash/hashfunc.c
src/backend/utils/adt/pg_locale.c
src/backend/utils/adt/varchar.c

index 37646cc9a109a862c4dcb53072c83aff76a3d8f7..ac21884162f93544cee5e2c55813427c7dbcb8e2 100644 (file)
@@ -300,7 +300,9 @@ hashtext(PG_FUNCTION_ARGS)
        buf = palloc(bsize + 1);
 
        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");
 
        /*
@@ -354,7 +356,9 @@ hashtextextended(PG_FUNCTION_ARGS)
        buf = palloc(bsize + 1);
 
        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");
 
        /*
index 9aaaaa960e6e932ba890ea0f858e464cf119b2fb..99a21f20b9f933559b557662231206511b5344a5 100644 (file)
@@ -2288,9 +2288,9 @@ pg_strxfrm_enabled(pg_locale_t locale)
  * The provided 'src' must be nul-terminated. If 'destsize' is zero, 'dest'
  * may be NULL.
  *
- * Returns the number of bytes needed to store the transformed string,
- * excluding the terminating nul byte. If the value returned is 'destsize' or
- * greater, the resulting contents of 'dest' are undefined.
+ * Returns the number of bytes needed (or more) to store the transformed
+ * string, excluding the terminating nul byte. If the value returned is
+ * 'destsize' or greater, the resulting contents of 'dest' are undefined.
  */
 size_t
 pg_strxfrm(char *dest, const char *src, size_t destsize, pg_locale_t locale)
@@ -2320,9 +2320,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
  * be NULL.
  *
- * Returns the number of bytes needed to store the transformed string,
- * excluding the terminating nul byte. If the value returned is 'destsize' or
- * greater, the resulting contents of 'dest' are undefined.
+ * Returns the number of bytes needed (or more) to store the transformed
+ * string, excluding the terminating nul byte. If the value returned is
+ * 'destsize' or greater, the resulting contents of 'dest' are undefined.
  *
  * 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
index b92ff4d266e261b902294a198910cf3415e497cb..10d7ed74f886b7153907869049a683efa9adb708 100644 (file)
@@ -1029,7 +1029,9 @@ hashbpchar(PG_FUNCTION_ARGS)
        buf = palloc(bsize + 1);
 
        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");
 
        /*
@@ -1085,7 +1087,9 @@ hashbpcharextended(PG_FUNCTION_ARGS)
        buf = palloc(bsize + 1);
 
        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");
 
        /*