Repository navigation
Write an escaped GSP expression in long text without its backslash - #17181
jdaugherty wants to merge 3 commits into
Conversation
The scanner hands the text of a page on to the parser in pieces of
64,000 characters, and the parser takes the backslash out of an escaped
expression only in a piece that has the whole of \${. A piece could end
between the backslash and the expression, so a \${ that came 63,998 or
63,999 characters after the last expression, scriptlet or tag in front
of it, or that many and a multiple of 64,000 more, was rendered as \${.
The text is no longer cut inside one.
The escape handling came with apache#16144 (8.0.0-M6).
jamesfredley
left a comment
There was a problem hiding this comment.
Requested changes. A long page that ends in an escaped expression fails to compile.
A piece of text the scanner would have cut inside \${ was extended to
the end of it, so that the escape stayed whole. A compiled page stores
each piece in at most 65,535 bytes, so a piece of 64,000 characters
that already took 65,535 bytes, as multibyte text can, no longer fit
and the page failed to compile. The piece now ends before the
backslash, so no piece is longer than before.
codeconsole
left a comment
There was a problem hiding this comment.
I found no defects. I traced the new cut against found() and the parser's per-piece ${ replacement. A cut on the $ or the { of ${ now lands before the backslash, so a piece is max-1 or max-2 characters and never longer than before. That handles the writeUTF limit raised in review, and the compiled two-byte case checks it. Unescaped ${ is still caught before the length check. The -1 path only happens when a piece starts at the backslash and reaches the limit within three characters, which needs a limit below 3. The first spec and both ScanTests cases fail on the base commit. Optional: make htmlCutPosition's { branch reuse the same escape check as the $ branch, so the two can't disagree about what counts as an escape.
-
grails-gsp/core/src/test/groovy/org/grails/gsp/GspLongTextEscapeSpec.groovy:121: This case passes without the fix and never runs the new code:${is found before the length check, and the trailing$ {\$ \{comes after the expression, in a fresh piece far from any cut. To check that the new branches don't match too much, put\$/\{at the cut point (e.g.'x' * 63_998 + '\\$ {'), or drop the case.
jamesfredley
left a comment
There was a problem hiding this comment.
Requested changes. A long page that ends in an escaped expression still fails to compile.
| int cut = htmlCutPosition(c, c1); | ||
| if (cut != -1) { | ||
| end1 = cut + 1; | ||
| return found(HTML, 0); |
There was a problem hiding this comment.
A page of accented letters and plain letters ends in an escaped expression. It should compile and write the expression as text. It fails, because the piece before the escape is still one byte too long. Cut that piece shorter.
There was a problem hiding this comment.
This one is not from the escape. A piece is cut at 64,000 characters, and a compiled page stores a piece in at most 65,535 bytes, so any text with more than 1,535 two-byte characters in one piece fails to compile, with or without \${.
I compiled pages of 63,996 to 64,002 characters with 1,534 to 1,538 é on this branch and on the commit before it. The page with 1,537 é in 63,999 characters and \${a} at the end fails on both: the piece is 65,536 bytes here and one byte more before. The same text with plain text in place of the escape fails the same way, and 7.0.x has the same cut and the same writeUTF call. No page that compiled before fails now; this PR only makes the piece in front of an escape one or two characters shorter.
Cutting by encoded length is a fix of its own, and it belongs on 7.0.x, so I am leaving it out of this PR.
The cut position and the scanner now share one check for `\${`, so they
cannot disagree about what an escape is. The javadoc says that a piece
starting with an escaped expression is longer than a limit below 3. The
tests put text that only looks like an escaped expression at the cut.
✅ All tests passed ✅🏷️ Commit: 71c7df0 Learn more about TestLens at testlens.app/docs. |
jamesfredley
left a comment
There was a problem hiding this comment.
The items this review found have been addressed. A long page that ends in an escaped expression compiles, and the backslash is not written.
matrei
left a comment
There was a problem hiding this comment.
@jdaugherty, the new cut is right, and cutting before the backslash is better than keeping the escape whole. What's left is what #17061 asks of a fix that changes a result, plus an issue for the two-byte limit.
What I ran
On 8.0.x, at 71c7df0 and on the base, aa4de16:
:grails-gsp-core:test(319) and:grails-gsp:test(749, 7 skipped) pass with the PR.- On the base,
GspLongTextEscapeSpecfails 8 of its 49 cases: the four lengths, rendered and compiled. Two of the three newScanTestsfail there. The third, with text that only looks like an escape, passes on both, as a check that the new code matches nothing more should. htmlCutPosition()only looks at the two characters in front of the cut, the only places where a\${can cross it. A cut that it moves goes to the backslash, so the piece is one or two characters shorter, never longer. An unescaped${is still found before the length check.\\${a}renders\${a}, also at the cut, as it does anywhere else in a page.
1. What #17061 asks of this PR
This changes what a page renders on a maintenance branch, so it's a fix that changes a result. #17061 is merged on 7.0.x but not yet merged up to 8.0.x. The old output contradicts expressions.adoc, which says that \${ renders as ${, so it's a wrong result: 8.0.x is the right branch, and no setting is needed. Three things are missing:
- The description should say that no setting is needed, and why: the old output depends on where in the text the escape falls, so no page can rely on it. It should also say how to get the old output:
\\${renders\${. - The upgrade skill needs the same entry as the note, under
# Grails 8.0.1ingrails-skills/upgrade/skills/grails-upgrade/SKILL.md. - Two approving reviews. Your last push dismissed codeconsole's.
A detail in the note: "63,998 or 63,999 characters after the last expression, scriptlet or tag in front of it" leaves out an escape that far from the start of the page, with nothing in front of it. "or after the start of the page" would cover it.
2. The two-byte limit
Your analysis at GroovyPageScanner.java:184 holds. It's the same on 7.0.x and 8.0.x, with or without this PR. Text of a piece goes to the compiled page through writeUTF, which takes at most 65,535 bytes. Text in three-byte characters hits that limit much sooner:
| Text, with no GSP tag or expression in it | Bytes | Not precompiled | Precompiled |
|---|---|---|---|
64,000 characters, 1,535 of them é |
65,535 | renders | renders |
64,000 characters, 1,536 of them é |
65,536 | renders | UTFDataFormatException |
| 21,845 Chinese or Japanese characters | 65,535 | renders | renders |
| 21,846 Chinese or Japanese characters | 65,538 | renders | UTFDataFormatException |
30 paragraphs of 1,000 such characters, in <p> tags |
90,239 | renders | UTFDataFormatException |
HTML tags don't split the text, so a long page of Chinese or Japanese text is enough. Such a page renders in development. compileGroovyPages precompiles the pages with the same GroovyPageCompiler before the jar or the war is built. Since the fix stays out of this PR, it should get an issue for 7.0.x so it isn't lost. I opened #17205 for it.
3. With the merge-up of #17180
The two changes to GroovyPageScanner meet when #17180 is merged up. I cherry-picked #17180, as it was at 63cbbe7, onto this head. The code applies apart from one line of GSPWriter, where 8.0.x checks !GrailsStringUtils.hasLength(s) and 7.0.x isEmpty(s). :grails-gsp-core:test (414) and :grails-gsp:test (749, 7 skipped) pass with both.
Split out of #17114, as the review there asked.
\${in the text of a page renders as${. The scanner hands long text to the parser in pieces of 64,000 characters, and a piece could end between the backslash and the${. A\${that came 63,998 or 63,999 characters after the last expression, scriptlet or tag in front of it (or that many plus a multiple of 64,000) was then rendered with its backslash. The text is no longer cut inside an escaped expression.The bug came with #16144 (8.0.0-M6), so 7.0.x does not have it. The GSP line-number fixes of #17114 go to 7.0.x in their own PR and reach 8.0.x with the merge-up.
The upgrade note is under Grails 8.0.1 in
upgrading80x.adoc.Tested locally:
:grails-gsp-core:test(with the newGspLongTextEscapeSpec), the compiler and page tests of:grails-gsp, andcodeStyle.