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