@@ -2019,16 +2019,37 @@ compareStrings(const char *mbstr1, int mblen1,
2019
2019
}
2020
2020
else
2021
2021
{
2022
- /* We have to convert other encodings to UTF-8 first, then compare. */
2023
- char * utf8str1 = pg_server_to_any (mbstr1 , mblen1 , PG_UTF8 ),
2024
- * utf8str2 = pg_server_to_any (mbstr2 , mblen2 , PG_UTF8 );
2025
- int cmp ;
2022
+ char * utf8str1 ,
2023
+ * utf8str2 ;
2024
+ int cmp ,
2025
+ utf8len1 ,
2026
+ utf8len2 ;
2026
2027
2027
- cmp = binaryCompareStrings (utf8str1 , strlen (utf8str1 ),
2028
- utf8str2 , strlen (utf8str2 ));
2028
+ /*
2029
+ * We have to convert other encodings to UTF-8 first, then compare.
2030
+ * Input strings may be not null-terminated and pg_server_to_any() may
2031
+ * return them "as is". So, use strlen() only if there is real
2032
+ * conversion.
2033
+ */
2034
+ utf8str1 = pg_server_to_any (mbstr1 , mblen1 , PG_UTF8 );
2035
+ utf8str2 = pg_server_to_any (mbstr2 , mblen2 , PG_UTF8 );
2036
+ utf8len1 = (mbstr1 == utf8str1 ) ? mblen1 : strlen (utf8str1 );
2037
+ utf8len2 = (mbstr2 == utf8str2 ) ? mblen2 : strlen (utf8str2 );
2038
+
2039
+ cmp = binaryCompareStrings (utf8str1 , utf8len1 , utf8str2 , utf8len2 );
2040
+
2041
+ /*
2042
+ * If pg_server_to_any() did no real conversion, then we actually
2043
+ * compared original strings. So, we already done.
2044
+ */
2045
+ if (mbstr1 == utf8str1 && mbstr2 == utf8str2 )
2046
+ return cmp ;
2029
2047
2030
- pfree (utf8str1 );
2031
- pfree (utf8str2 );
2048
+ /* Free memory if needed */
2049
+ if (mbstr1 != utf8str1 )
2050
+ pfree (utf8str1 );
2051
+ if (mbstr2 != utf8str2 )
2052
+ pfree (utf8str2 );
2032
2053
2033
2054
/*
2034
2055
* When all Unicode codepoints are equal, return result of binary
0 commit comments