changeset 48344:c0bf7d8af037

8191636: [Windows] jshell tool: Wrong character in /env class-path command crashes jshell Summary: Fixing handling of invalid paths. Reviewed-by: rfield
author jlahoda
date Wed, 13 Dec 2017 11:27:28 +0100
parents 1925dbd47e28
children 8ca86cfb126f
files src/jdk.jshell/share/classes/jdk/internal/jshell/tool/JShellTool.java test/langtools/jdk/jshell/ToolSimpleTest.java
diffstat 2 files changed, 15 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- 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.")
         );
     }