Repository navigation
Fix surrogate pairs mis-decoded when split across a read chunk boundary #4478
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
xuorig
wants to merge
1
commit into
graphql-java:master
Choose a base branch
from
xuorig:fix/parser-surrogate-pair-chunk-boundary
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
src/test/groovy/graphql/parser/SurrogatePairChunkBoundaryTest.groovy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| package graphql.parser | ||
|
|
||
| import graphql.language.Argument | ||
| import graphql.language.Document | ||
| import graphql.language.Field | ||
| import graphql.language.ObjectTypeDefinition | ||
| import graphql.language.OperationDefinition | ||
| import graphql.language.SelectionSet | ||
| import graphql.language.StringValue | ||
| import graphql.schema.idl.SchemaParser | ||
| import spock.lang.Specification | ||
| import spock.lang.Unroll | ||
|
|
||
| /** | ||
| * ANTLR's {@link org.antlr.v4.runtime.CharStreams#fromReader(java.io.Reader)} reads a document in fixed | ||
| * size chunks and hands each one to {@link org.antlr.v4.runtime.CodePointBuffer.Builder#append}. When a | ||
| * surrogate pair (e.g. an emoji outside the Basic Multilingual Plane) is split across two of those chunks, | ||
| * ANTLR mis-decodes it into two separate code points instead of one - see | ||
| * <a href="https://github.com/antlr/antlr4/pull/4943">antlr/antlr4#4943</a>. | ||
| * <p> | ||
| * Parser.setupCharStream() avoids this by holding back a trailing unpaired high surrogate so it is carried | ||
| * into the next chunk. This test proves it by placing an astral character at every offset around the chunk | ||
| * boundary. | ||
| */ | ||
| class SurrogatePairChunkBoundaryTest extends Specification { | ||
|
|
||
| // the emoji U+1F600 (😀), expressed as a Java surrogate pair | ||
| static final String EMOJI = "😀" | ||
|
|
||
| static String queryWithEmojiAt(int highSurrogateIndex) { | ||
| def prefix = 'query { f(arg: "' | ||
| def pad = highSurrogateIndex - prefix.length() | ||
| assert pad >= 0 | ||
| return prefix + ('a' * pad) + EMOJI + '") }' | ||
| } | ||
|
|
||
| @Unroll | ||
| def "an astral character surviving a document of length #docLength with the high surrogate at index #highSurrogateIndex"() { | ||
| given: | ||
| def query = queryWithEmojiAt(highSurrogateIndex) | ||
| assert query.indexOf(0xD83D as int) == highSurrogateIndex | ||
|
|
||
| when: | ||
| Document viaString = Parser.parse(query) | ||
| Document viaReader = Parser.parse(ParserEnvironment.newParserEnvironment().document(query).build()) | ||
|
|
||
| then: | ||
| [viaString, viaReader].each { Document document -> | ||
| def field = ((document.definitions[0] as OperationDefinition).selectionSet as SelectionSet).selections[0] as Field | ||
| def argument = field.arguments[0] as Argument | ||
| def value = argument.value as StringValue | ||
| assert value.value == ('a' * (highSurrogateIndex - 'query { f(arg: "'.length())) + "😀" | ||
| } | ||
|
|
||
| where: | ||
| highSurrogateIndex << (4085..4100) | ||
| docLength = queryWithEmojiAt(highSurrogateIndex).length() | ||
| } | ||
|
|
||
| def "a document ending in an unpaired high surrogate is not silently dropped"() { | ||
| given: | ||
| // deliberately malformed: a lone high surrogate with nothing after it, inside a string that never closes. | ||
| // this asserts today's (pre-existing) behaviour is preserved, not that new semantics are introduced. | ||
| def query = 'query { f(arg: "' + ('a' * 4080) + "\uD83D" | ||
|
|
||
| when: | ||
| Parser.parse(query) | ||
|
|
||
| then: | ||
| thrown(InvalidSyntaxException) | ||
| } | ||
|
|
||
| def "an astral character in an SDL description survives a chunk boundary"() { | ||
| given: | ||
| def pad = 'a' * (4096 - '"""'.length()) | ||
| def sdl = '"""' + pad + EMOJI + '"""' + '\ntype Query { f: String }' | ||
|
|
||
| when: | ||
| def registry = new SchemaParser().parse(sdl) | ||
| def objectType = registry.getType("Query", ObjectTypeDefinition).get() | ||
|
|
||
| then: | ||
| objectType.description.content == pad + "😀" | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you'd like to compare, here's the original: https://github.com/rootvector2/antlr4/blob/master/runtime/Java/src/org/antlr/v4/runtime/CharStreams.java#L189-L206