8211765: JarFile constructor throws undocumented exception
authorlancea
Sun, 07 Oct 2018 14:35:00 -0400
changeset 52034 c83bc5def0d4
parent 52033 d6aa9ea2405d
child 52035 f697ba5b18d2
8211765: JarFile constructor throws undocumented exception Reviewed-by: lancea, sherman, alanb, chegar Contributed-by: Jaikiran Pai <jai.forums2013@gmail.com>
src/java.base/share/classes/java/util/zip/ZipFile.java
test/jdk/java/util/jar/JarFile/Constructor.java
--- a/src/java.base/share/classes/java/util/zip/ZipFile.java	Fri Oct 05 20:03:14 2018 +0200
+++ b/src/java.base/share/classes/java/util/zip/ZipFile.java	Sun Oct 07 14:35:00 2018 -0400
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1995, 2017, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1995, 2018, 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
@@ -35,6 +35,7 @@
 import java.lang.ref.Cleaner.Cleanable;
 import java.nio.charset.Charset;
 import java.nio.charset.StandardCharsets;
+import java.nio.file.InvalidPathException;
 import java.nio.file.attribute.BasicFileAttributes;
 import java.nio.file.Files;
 import java.util.ArrayDeque;
@@ -1218,8 +1219,13 @@
 
 
         static Source get(File file, boolean toDelete) throws IOException {
-            Key key = new Key(file,
-                              Files.readAttributes(file.toPath(), BasicFileAttributes.class));
+            final Key key;
+            try {
+                key = new Key(file,
+                        Files.readAttributes(file.toPath(), BasicFileAttributes.class));
+            } catch (InvalidPathException ipe) {
+                throw new IOException(ipe);
+            }
             Source src;
             synchronized (files) {
                 src = files.get(key);
--- a/test/jdk/java/util/jar/JarFile/Constructor.java	Fri Oct 05 20:03:14 2018 +0200
+++ b/test/jdk/java/util/jar/JarFile/Constructor.java	Sun Oct 07 14:35:00 2018 -0400
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2003, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2003, 2018 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
@@ -23,7 +23,7 @@
 
 /**
  * @test
- * @bug 4842702
+ * @bug 4842702 8211765
  * @summary Check that constructors throw specified exceptions
  * @author Martin Buchholz
  */
@@ -63,5 +63,13 @@
 
         try { Unreached (new JarFile (new File ("NoSuchJar.jar"))); }
         catch (IOException e) {}
+
+        // Test that an IOExcception is thrown when an invalid charater
+        // is part of the path on Windows and Unix
+        final String invalidOSPath = System.getProperty("os.name")
+                .startsWith("Windows") ? "C:\\*" : "foo\u0000bar";
+
+        try { Unreached (new JarFile (invalidOSPath)); }
+        catch (IOException e) {}
     }
 }