8191636: [Windows] jshell tool: Wrong character in /env class-path command crashes jshell
authorjlahoda
Wed, 13 Dec 2017 11:27:28 +0100
changeset 48259 c0bf7d8af037
parent 48258 1925dbd47e28
child 48260 8ca86cfb126f
8191636: [Windows] jshell tool: Wrong character in /env class-path command crashes jshell Summary: Fixing handling of invalid paths. Reviewed-by: rfield
src/jdk.jshell/share/classes/jdk/internal/jshell/tool/JShellTool.java
test/langtools/jdk/jshell/ToolSimpleTest.java
--- 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.")
         );
     }