summaryrefslogtreecommitdiff
path: root/src/common
diff options
context:
space:
mode:
Diffstat (limited to 'src/common')
-rw-r--r--src/common/jsonapi.c6
-rw-r--r--src/common/wchar.c15
2 files changed, 18 insertions, 3 deletions
diff --git a/src/common/jsonapi.c b/src/common/jsonapi.c
index 1bf38d7b429..d376ab152d4 100644
--- a/src/common/jsonapi.c
+++ b/src/common/jsonapi.c
@@ -740,7 +740,7 @@ json_lex_string(JsonLexContext *lex)
ch = (ch * 16) + (*s - 'A') + 10;
else
{
- lex->token_terminator = s + pg_encoding_mblen(lex->input_encoding, s);
+ lex->token_terminator = s + pg_encoding_mblen_bounded(lex->input_encoding, s);
return JSON_UNICODE_ESCAPE_FORMAT;
}
}
@@ -846,7 +846,7 @@ json_lex_string(JsonLexContext *lex)
default:
/* Not a valid string escape, so signal error. */
lex->token_start = s;
- lex->token_terminator = s + pg_encoding_mblen(lex->input_encoding, s);
+ lex->token_terminator = s + pg_encoding_mblen_bounded(lex->input_encoding, s);
return JSON_ESCAPING_INVALID;
}
}
@@ -860,7 +860,7 @@ json_lex_string(JsonLexContext *lex)
* shown it's not a performance win.
*/
lex->token_start = s;
- lex->token_terminator = s + pg_encoding_mblen(lex->input_encoding, s);
+ lex->token_terminator = s + pg_encoding_mblen_bounded(lex->input_encoding, s);
return JSON_ESCAPING_INVALID;
}
diff --git a/src/common/wchar.c b/src/common/wchar.c
index 6e7d731e020..0636b8765ba 100644
--- a/src/common/wchar.c
+++ b/src/common/wchar.c
@@ -1911,6 +1911,11 @@ const pg_wchar_tbl pg_wchar_table[] = {
/*
* Returns the byte length of a multibyte character.
+ *
+ * Caution: when dealing with text that is not certainly valid in the
+ * specified encoding, the result may exceed the actual remaining
+ * string length. Callers that are not prepared to deal with that
+ * should use pg_encoding_mblen_bounded() instead.
*/
int
pg_encoding_mblen(int encoding, const char *mbstr)
@@ -1921,6 +1926,16 @@ pg_encoding_mblen(int encoding, const char *mbstr)
}
/*
+ * Returns the byte length of a multibyte character; but not more than
+ * the distance to end of string.
+ */
+int
+pg_encoding_mblen_bounded(int encoding, const char *mbstr)
+{
+ return strnlen(mbstr, pg_encoding_mblen(encoding, mbstr));
+}
+
+/*
* Returns the display length of a multibyte character.
*/
int