@@ -1998,12 +1998,12 @@ bttext_abbrev_convert(Datum original, SortSupport ssup)
19981998{
19991999 TextSortSupport * tss = (TextSortSupport * ) ssup -> ssup_extra ;
20002000 text * authoritative = DatumGetTextPP (original );
2001+ char * authoritative_data = VARDATA_ANY (authoritative );
20012002
20022003 /* working state */
20032004 Datum res ;
20042005 char * pres ;
20052006 int len ;
2006- Size bsize ;
20072007 uint32 hash ;
20082008
20092009 /*
@@ -2017,14 +2017,16 @@ bttext_abbrev_convert(Datum original, SortSupport ssup)
20172017
20182018 /*
20192019 * If we're using the C collation, use memcmp(), rather than strxfrm(),
2020- * to abbreviated keys. The full comparator for the C locale is always
2020+ * to abbreviate keys. The full comparator for the C locale is always
20212021 * memcmp(), and we can't risk having this give a different answer.
20222022 * Besides, this should be faster, too.
20232023 */
20242024 if (tss -> collate_c )
2025- memcpy (pres , VARDATA_ANY ( authoritative ) , Min (len , sizeof (Datum )));
2025+ memcpy (pres , authoritative_data , Min (len , sizeof (Datum )));
20262026 else
20272027 {
2028+ Size bsize ;
2029+
20282030 /*
20292031 * We're not using the C collation, so fall back on strxfrm.
20302032 */
@@ -2067,6 +2069,14 @@ bttext_abbrev_convert(Datum original, SortSupport ssup)
20672069 Min (tss -> buflen2 * 2 , MaxAllocSize ));
20682070 tss -> buf2 = palloc (tss -> buflen2 );
20692071 }
2072+
2073+ /*
2074+ * Every Datum byte is always compared. This is safe because the
2075+ * strxfrm() blob is itself NUL terminated, leaving no danger of
2076+ * misinterpreting any NUL bytes not intended to be interpreted as
2077+ * logically representing termination.
2078+ */
2079+ memcpy (pres , tss -> buf2 , Min (sizeof (Datum ), bsize ));
20702080 }
20712081
20722082 /*
@@ -2080,15 +2090,14 @@ bttext_abbrev_convert(Datum original, SortSupport ssup)
20802090 * in order to compensate for cases where differences are past
20812091 * CACHE_LINE_SIZE bytes, so as to limit the overhead of hashing.
20822092 */
2083- hash = hash_any ((unsigned char * ) tss -> buf1 , Min (len , PG_CACHE_LINE_SIZE ));
2093+ hash = hash_any ((unsigned char * ) authoritative_data ,
2094+ Min (len , PG_CACHE_LINE_SIZE ));
20842095
20852096 if (len > PG_CACHE_LINE_SIZE )
20862097 hash ^= DatumGetUInt32 (hash_uint32 ((uint32 ) len ));
20872098
20882099 addHyperLogLog (& tss -> full_card , hash );
20892100
2090- memcpy (pres , tss -> buf2 , Min (sizeof (Datum ), bsize ));
2091-
20922101 /* Hash abbreviated key */
20932102#if SIZEOF_DATUM == 8
20942103 {
@@ -2105,12 +2114,6 @@ bttext_abbrev_convert(Datum original, SortSupport ssup)
21052114
21062115 addHyperLogLog (& tss -> abbr_card , hash );
21072116
2108- /*
2109- * Every Datum byte is always compared. This is safe because the strxfrm()
2110- * blob is itself NUL terminated, leaving no danger of misinterpreting any
2111- * NUL bytes not intended to be interpreted as logically representing
2112- * termination.
2113- */
21142117 return res ;
21152118}
21162119
0 commit comments