8191636: [Windows] jshell tool: Wrong character in /env class-path command crashes jshell
Summary: Fixing handling of invalid paths.
Reviewed-by: rfield
--- a/src/jdk.jshell/share/classes/jdk/internal/jshell/tool/JShellTool.java Wed Dec 13 12:43:38 2017 +0530
+++ b/src/jdk.jshell/share/classes/jdk/internal/jshell/tool/JShellTool.java Wed Dec 13 11:27:28 2017 +0100
@@ -43,6 +43,7 @@
import java.nio.charset.Charset;
import java.nio.file.FileSystems;
import java.nio.file.Files;
+import java.nio.file.InvalidPathException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.text.MessageFormat;
@@ -381,7 +382,7 @@
private Collection<String> validPaths(Collection<String> vals, String context, boolean isModulePath) {
Stream<String> result = vals.stream()
.map(s -> Arrays.stream(s.split(File.pathSeparator))
- .map(sp -> toPathResolvingUserHome(sp))
+ .flatMap(sp -> toPathImpl(sp, context))
.filter(p -> checkValidPathEntry(p, context, isModulePath))
.map(p -> p.toString())
.collect(Collectors.joining(File.pathSeparator)));
@@ -421,6 +422,16 @@
return false;
}
+ private Stream<Path> toPathImpl(String path, String context) {
+ try {
+ return Stream.of(toPathResolvingUserHome(path));
+ } catch (InvalidPathException ex) {
+ msg("jshell.err.file.not.found", context, path);
+ failed = true;
+ return Stream.empty();
+ }
+ }
+
Options parse(OptionSet options) {
addOptions(OptionKind.CLASS_PATH,
validPaths(options.valuesOf(argClassPath), "--class-path", false));
--- a/test/langtools/jdk/jshell/ToolSimpleTest.java Wed Dec 13 12:43:38 2017 +0530
+++ b/test/langtools/jdk/jshell/ToolSimpleTest.java Wed Dec 13 11:27:28 2017 +0100
@@ -216,7 +216,9 @@
public void testInvalidClassPath() {
test(
a -> assertCommand(a, "/env --class-path snurgefusal",
- "| File 'snurgefusal' for '--class-path' is not found.")
+ "| File 'snurgefusal' for '--class-path' is not found."),
+ a -> assertCommand(a, "/env --class-path ?",
+ "| File '?' for '--class-path' is not found.")
);
}