postgres.git / summary / log / commit / refs
commit e2a44c77112c76113dad8dd6e99bddd91250f6c4
Author: Jeff Davis <jdavis@postgresql.org>
Date: Thu Aug 20 20:54:26 2026 +0000
First released in: 16.16, 17.12, 18.7, 19.0
hashtext: fix fragile code.
Previously, in the path for non-deterministic collations, the code
assumed that bsize==rsize. That assumption seems to be true for ICU,
and all non-deterministic collations are ICU, so it's not known to be
an actual bug.
The only known place where bsize may not equal rsize is in the libc
provider, where strxfrm() can return an upper bound of the size needed
to store the result. That means the initial call to determine the
buffer size (with dest==NULL, n==0) could return a larger number than
the actual call with an adequate dest buffer. That's OK, because libc
locales are always deterministic.
Commit 679c5084cf2 partially fixed the assumption, but missed this
part. Fix it, and add a more prominent documentation note.
Reviewed-by: Haibo Yan <tristan.yim@gmail.com>
Discussion: https://postgr.es/m/CABXr29Hb31nkj1g2Jmk+1BhAm=3ecGs_pWy4tU++j8CQBnbMxQ@mail.gmail.com
Backpatch-through: 16
src/backend/access/hash/hashfunc.c | 4 ++--
src/backend/utils/adt/pg_locale.c | 24 +++++++++++++++++++++---
src/backend/utils/adt/varchar.c | 4 ++--
3 files changed, 25 insertions(+), 7 deletions(-)
diff --git a/src/backend/access/hash/hashfunc.c b/src/backend/access/hash/hashfunc.c
index e08656caa4f..33b9ebb78ed 100644
--- a/src/backend/access/hash/hashfunc.c
+++ b/src/backend/access/hash/hashfunc.c
@@ -311,7 +311,7 @@ hashtext(PG_FUNCTION_ARGS)
* character in the hash, but it was done before and the behavior must
* be preserved.
*/
- result = hash_any((uint8_t *) buf, bsize + 1);
+ result = hash_any((uint8_t *) buf, rsize + 1);
pfree(buf);
}
@@ -367,7 +367,7 @@ hashtextextended(PG_FUNCTION_ARGS)
* character in the hash, but it was done before and the behavior must
* be preserved.
*/
- result = hash_any_extended((uint8_t *) buf, bsize + 1,
+ result = hash_any_extended((uint8_t *) buf, rsize + 1,
PG_GETARG_INT64(1));
pfree(buf);
diff --git a/src/backend/utils/adt/pg_locale.c b/src/backend/utils/adt/pg_locale.c
index 31c35c48e38..6907de9951a 100644
--- a/src/backend/utils/adt/pg_locale.c
+++ b/src/backend/utils/adt/pg_locale.c
@@ -2197,6 +2197,14 @@ pg_strncoll(const char *arg1, size_t len1, const char *arg2, size_t len2,
}
+/*
+ * pg_strxfrm_libc()
+ *
+ * NB: it's possible for this function to return a different size needed for
+ * two calls with the same input string. If destsize is too small to hold the
+ * result, strxfrm() may return the upper bound of the size needed rather than
+ * the exact size needed.
+ */
static size_t
pg_strxfrm_libc(char *dest, const char *src, size_t destsize,
pg_locale_t locale)
@@ -2215,6 +2223,14 @@ pg_strxfrm_libc(char *dest, const char *src, size_t destsize,
#endif
}
+/*
+ * pg_strnxfrm_libc()
+ *
+ * NB: it's possible for this function to return a different size needed for
+ * two calls with the same input string. If destsize is too small to hold the
+ * result, strxfrm() may return the upper bound of the size needed rather than
+ * the exact size needed.
+ */
static size_t
pg_strnxfrm_libc(char *dest, const char *src, size_t srclen, size_t destsize,
pg_locale_t locale)
@@ -2444,9 +2460,11 @@ 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 (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.
+ * Returns the number of bytes needed (NB: or more; see comments above
+ * pg_strnxfrm_libc()) 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, and the caller should use the
+ * return value to resize the buffer.
*
* 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
diff --git a/src/backend/utils/adt/varchar.c b/src/backend/utils/adt/varchar.c
index 1ba5f9d9f6c..5f76c805da8 100644
--- a/src/backend/utils/adt/varchar.c
+++ b/src/backend/utils/adt/varchar.c
@@ -1038,7 +1038,7 @@ hashbpchar(PG_FUNCTION_ARGS)
* character in the hash, but it was done before and the behavior must
* be preserved.
*/
- result = hash_any((uint8_t *) buf, bsize + 1);
+ result = hash_any((uint8_t *) buf, rsize + 1);
pfree(buf);
}
@@ -1096,7 +1096,7 @@ hashbpcharextended(PG_FUNCTION_ARGS)
* character in the hash, but it was done before and the behavior must
* be preserved.
*/
- result = hash_any_extended((uint8_t *) buf, bsize + 1,
+ result = hash_any_extended((uint8_t *) buf, rsize + 1,
PG_GETARG_INT64(1));
pfree(buf);
[parent: 25251bb27fad]