8173072: zipfs fails to handle incorrect info-zip "extended timestamp extra field"
authorsherman
Thu, 19 Jan 2017 16:38:24 -0800
changeset 43224 355457152ea8
parent 43223 9dbe335dad72
child 43225 548d92cace80
8173072: zipfs fails to handle incorrect info-zip "extended timestamp extra field" Reviewed-by: redestad
jdk/src/jdk.zipfs/share/classes/jdk/nio/zipfs/ZipCoder.java
jdk/src/jdk.zipfs/share/classes/jdk/nio/zipfs/ZipFileSystem.java
--- a/jdk/src/jdk.zipfs/share/classes/jdk/nio/zipfs/ZipCoder.java	Thu Jan 19 15:45:36 2017 -0800
+++ b/jdk/src/jdk.zipfs/share/classes/jdk/nio/zipfs/ZipCoder.java	Thu Jan 19 16:38:24 2017 -0800
@@ -67,7 +67,7 @@
         }
     }
 
-    private static ZipCoder utf8 = new UTF8();
+    private static final ZipCoder utf8 = new UTF8();
 
     public static ZipCoder get(String csn) {
         Charset cs = Charset.forName(csn);
@@ -82,15 +82,15 @@
         int clen = (int)(ba.length * cd.maxCharsPerByte());
         char[] ca = new char[clen];
         if (clen == 0)
-        return new String(ca);
+            return new String(ca);
         ByteBuffer bb = ByteBuffer.wrap(ba, 0, ba.length);
         CharBuffer cb = CharBuffer.wrap(ca);
         CoderResult cr = cd.decode(bb, cb, true);
         if (!cr.isUnderflow())
-        throw new IllegalArgumentException(cr.toString());
+            throw new IllegalArgumentException(cr.toString());
         cr = cd.flush(cb);
         if (!cr.isUnderflow())
-        throw new IllegalArgumentException(cr.toString());
+            throw new IllegalArgumentException(cr.toString());
         return new String(ca, 0, cb.position());
     }
 
@@ -100,19 +100,19 @@
         int len = (int)(ca.length * ce.maxBytesPerChar());
         byte[] ba = new byte[len];
         if (len == 0)
-        return ba;
+            return ba;
         ByteBuffer bb = ByteBuffer.wrap(ba);
         CharBuffer cb = CharBuffer.wrap(ca);
         CoderResult cr = ce.encode(cb, bb, true);
         if (!cr.isUnderflow())
-        throw new IllegalArgumentException(cr.toString());
+            throw new IllegalArgumentException(cr.toString());
         cr = ce.flush(bb);
         if (!cr.isUnderflow())
-        throw new IllegalArgumentException(cr.toString());
+            throw new IllegalArgumentException(cr.toString());
         if (bb.position() == ba.length)  // defensive copy?
-        return ba;
+            return ba;
         else
-        return Arrays.copyOf(ba, bb.position());
+            return Arrays.copyOf(ba, bb.position());
     }
 
     boolean isUTF8() {
--- a/jdk/src/jdk.zipfs/share/classes/jdk/nio/zipfs/ZipFileSystem.java	Thu Jan 19 15:45:36 2017 -0800
+++ b/jdk/src/jdk.zipfs/share/classes/jdk/nio/zipfs/ZipFileSystem.java	Thu Jan 19 16:38:24 2017 -0800
@@ -2294,16 +2294,17 @@
                             locPos += locSZ;
                              continue;
                         }
+                        int end = locPos + locSZ - 4;
                         int flag = CH(buf, locPos++);
-                        if ((flag & 0x1) != 0) {
+                        if ((flag & 0x1) != 0 && locPos <= end) {
                             mtime = unixToJavaTime(LG(buf, locPos));
                             locPos += 4;
                         }
-                        if ((flag & 0x2) != 0) {
+                        if ((flag & 0x2) != 0 && locPos <= end) {
                             atime = unixToJavaTime(LG(buf, locPos));
                             locPos += 4;
                         }
-                        if ((flag & 0x4) != 0) {
+                        if ((flag & 0x4) != 0 && locPos <= end) {
                             ctime = unixToJavaTime(LG(buf, locPos));
                             locPos += 4;
                         }