[
https://issues.apache.org/jira/browse/GROOVY-12269?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=18105451#comment-18105451
]
ASF GitHub Bot commented on GROOVY-12269:
-----------------------------------------
paulk-asert opened a new pull request, #2806:
URL: https://github.com/apache/groovy/pull/2806
…tatement
ScriptEngineFactory.getOutputStatement takes the text to display, which is
data, and Groovy renders it as a double-quoted literal escaping only the quote
and the backslash. Two characters were left to reach the generated code
unaltered:
- a dollar makes the literal an interpolating GString, so display text
containing ${...} is evaluated rather than printed, and a bare $name leaks a
binding;
- a line terminator ends the line, so display text spanning lines emits a
statement that does not compile at all.
Escape both, along with the carriage return. Other control characters such
as tab are legal inside a Groovy string literal and are left alone, so output
is unchanged for every input that already worked.
Nashorn is the precedent here: its factory had the same class of defect,
reported on nashorn-dev in January 2017 as producing a syntax error for display
text containing quotes, and was fixed by quoting the argument rather than by
reinterpreting it as code. Groovy had already chosen the same reading by
quoting and escaping at all; this completes it.
getProgram and getMethodCallSyntax take code by contract and are unchanged.
The factory's code-generating methods had no tests. The new ones assert the
emitted spelling for each escaped character, and separately evaluate the
generated statement and compare what it prints with the original text, since
displaying the text verbatim is the actual contract. Without the fix the first
fails on the dollar and the second fails to compile.
> improve special character escaping in GroovyScriptEngineFactory
> #getOutputStatement
> -----------------------------------------------------------------------------------
>
> Key: GROOVY-12269
> URL: https://issues.apache.org/jira/browse/GROOVY-12269
> Project: Groovy
> Issue Type: Improvement
> Reporter: Paul King
> Assignee: Paul King
> Priority: Major
>
--
This message was sent by Atlassian Jira
(v8.20.10#820010)