Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit de0dc0b

Browse files
committed
Fix string comparison in jsonpath
Take into account pg_server_to_any() may return input string "as is". Reported-by: Andrew Dunstan, Thomas Munro Discussion: https://postgr.es/m/0ed83a33-d900-466a-880a-70ef456c721f%402ndQuadrant.com Author: Alexander Korotkov, Thomas Munro Backpatch-through: 12
1 parent 3218ff5 commit de0dc0b

File tree

1 file changed

+29
-8
lines changed

1 file changed

+29
-8
lines changed

src/backend/utils/adt/jsonpath_exec.c

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2019,16 +2019,37 @@ compareStrings(const char *mbstr1, int mblen1,
20192019
}
20202020
else
20212021
{
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;
20262027

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;
20292047

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);
20322053

20332054
/*
20342055
* When all Unicode codepoints are equal, return result of binary

0 commit comments

Comments
 (0)