Skip to content

Commit 251c8e3

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 b43f7c1 commit 251c8e3

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
@@ -2020,16 +2020,37 @@ compareStrings(const char *mbstr1, int mblen1,
20202020
}
20212021
else
20222022
{
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;
20272028

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

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

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

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy