Skip to content

8356165: System.in in jshell replace supplementary characters with ?? #25079

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
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2015, 2024, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2015, 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -977,7 +977,20 @@ public void perform(LineReaderImpl in) throws IOException {
public synchronized int readUserInput() throws IOException {
if (pendingBytes == null || pendingBytes.length <= pendingBytesPointer) {
char userChar = readUserInputChar();
pendingBytes = String.valueOf(userChar).getBytes();
StringBuilder dataToConvert = new StringBuilder();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps, add here the comment from the PR description for readers from the future:

[...] when the current character is a high surrogate, peek at the next character, and if it is a low surrogate, convert both the high and low surrogates to bytes together.

The (internal) API used in the implementation doesn't express that on first sight.

dataToConvert.append(userChar);
if (Character.isHighSurrogate(userChar)) {
//surrogates cannot be converted independently,
//read the low surrogate and append it to dataToConvert:
char lowSurrogate = readUserInputChar();
if (Character.isLowSurrogate(lowSurrogate)) {
dataToConvert.append(lowSurrogate);
} else {
//if not the low surrogate, rollback the reading of the character:
pendingLinePointer--;
}
}
pendingBytes = dataToConvert.toString().getBytes();
pendingBytesPointer = 0;
}
return pendingBytes[pendingBytesPointer++];
Expand Down
70 changes: 70 additions & 0 deletions test/langtools/jdk/jshell/InputUITest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* Copyright (c) 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/

/*
* @test
* @bug 8356165
* @summary Check user input works properly
* @modules
* jdk.compiler/com.sun.tools.javac.api
* jdk.compiler/com.sun.tools.javac.main
* jdk.jshell/jdk.internal.jshell.tool:open
* jdk.jshell/jdk.internal.jshell.tool.resources:open
* jdk.jshell/jdk.jshell:open
* @library /tools/lib
* @build toolbox.ToolBox toolbox.JarTask toolbox.JavacTask
* @build Compiler UITesting
* @compile InputUITest.java
* @run testng InputUITest
*/

import java.util.function.Function;
import org.testng.annotations.Test;

@Test
public class InputUITest extends UITesting {

static final String LINE_SEPARATOR = System.getProperty("line.separator");
static final String LINE_SEPARATOR_ESCAPED = LINE_SEPARATOR.replace("\n", "\\n")
.replace("\r", "\\r");

public InputUITest() {
super(true);
}

public void testUserInputWithSurrogates() throws Exception {
Function<Integer, String> genSnippet =
realCharsToRead -> "new String(System.in.readNBytes(" +
(realCharsToRead + LINE_SEPARATOR.length()) +
"))\n";
doRunTest((inputSink, out) -> {
inputSink.write(genSnippet.apply(4) + "\uD83D\uDE03\n");
waitOutput(out, patternQuote("\"\uD83D\uDE03" + LINE_SEPARATOR_ESCAPED + "\""));
inputSink.write(genSnippet.apply(1) + "\uD83D\n");
waitOutput(out, patternQuote("\"?" + LINE_SEPARATOR_ESCAPED + "\""));
inputSink.write(genSnippet.apply(1) + "\uDE03\n");
waitOutput(out, patternQuote("\"?" + LINE_SEPARATOR_ESCAPED + "\""));
}, false);
}

}
9 changes: 7 additions & 2 deletions test/langtools/jdk/jshell/UITesting.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2017, 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2017, 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand All @@ -21,6 +21,7 @@
* questions.
*/

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
Expand Down Expand Up @@ -62,6 +63,10 @@ public UITesting(boolean laxLineEndings) {
}

protected void doRunTest(Test test) throws Exception {
doRunTest(test, true);
}

protected void doRunTest(Test test, boolean setUserInput) throws Exception {
// turn on logging of launch failures
Logger.getLogger("jdk.jshell.execution").setLevel(Level.ALL);

Expand All @@ -87,7 +92,7 @@ protected void doRunTest(Test test) throws Exception {
Thread runner = new Thread(() -> {
try {
JavaShellToolBuilder.builder()
.in(input, input)
.in(input, setUserInput ? input : null)
.out(outS)
.err(outS)
.promptCapture(true)
Expand Down