Skip to content

fix: validate Unicode escapes of PostgreSQL escape strings - #2861

Merged
manticore-projects merged 1 commit into
JSQLParser:masterfrom
tanjiroK-coder:escape-string-unicode-escapes
Oct 27, 2026
Merged

manticore-projects merged 1 commit into
JSQLParser:masterfrom
tanjiroK-coder:escape-string-unicode-escapes

Conversation

@tanjiroK-coder

Copy link
Copy Markdown
Contributor

With Dialect.POSTGRESQL, StringValue.getNotExcapedValue resolves the \uXXXX and \UXXXXXXXX escapes of an E'...' string with Integer.parseInt and StringBuilder.appendCodePoint, with no range or surrogate check, while the U&'...' path added in #2820 already validates the same escapes the way the server does. So E'\uD800' comes back as a lone surrogate (an ill-formed string that turns into U+FFFD or '?' once it is encoded), E'\u0000' yields a NUL the server refuses, E'\U00110000' throws from StringBuilder, E'\UFFFFFFFF' throws NumberFormatException, and the parser accepts every one of them. I noticed it while comparing the two decoders after the U&'' change. The E'' Unicode escapes now go through the same code point and surrogate pair rules as the U&'' escapes: a pair must be two consecutive escapes, in either form, and too few digits, code point zero, anything above U+10FFFF and an unpaired surrogate raise the documented IllegalArgumentException. stringValue() in the grammar checks an escape string under the PostgreSQL dialect the way lexUnicodeEscapeString does for U&'' strings, so such a literal is a parse error as on the server. Standard strings and the other dialects are unchanged, and the regression test in StringValueTest covers the accepted and rejected forms.

With Dialect.POSTGRESQL, StringValue.getNotExcapedValue resolved the
\uXXXX and \UXXXXXXXX escapes of an E'...' string with Integer.parseInt
and StringBuilder.appendCodePoint and no range or surrogate check, so
E'\uD800' produced an unpaired surrogate, E'\u0000' a NUL, E'\U00110000'
threw from StringBuilder and E'\UFFFFFFFF' a NumberFormatException,
while the parser accepted them all. PostgreSQL rejects each of them.

Decode the E'' Unicode escapes with the rules the U&'' path already
applies: exactly 4 or 8 hexadecimal digits, no code point zero or value
above U+10FFFF, and a surrogate pair only as two consecutive escapes.
The parser rejects such a literal under the PostgreSQL dialect as it
does for U&'' strings.
@manticore-projects
manticore-projects merged commit 23d3173 into JSQLParser:master Oct 27, 2026
10 checks passed
@manticore-projects

Copy link
Copy Markdown
Contributor

Thank you very much!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants