8156209: Add argument checks to BasicImageReader calls
authorjlaskey
Fri, 27 May 2016 12:34:08 -0300
changeset 38582 8618f4ccb220
parent 38581 e761c1ccd13e
child 38583 d887ee89eec0
8156209: Add argument checks to BasicImageReader calls Reviewed-by: sundar
jdk/src/java.base/share/classes/jdk/internal/jimage/BasicImageReader.java
jdk/src/java.base/share/classes/jdk/internal/jimage/ImageHeader.java
jdk/src/java.base/share/classes/jdk/internal/jimage/ImageLocation.java
jdk/src/java.base/share/classes/jdk/internal/jimage/ImageReader.java
jdk/src/java.base/share/classes/jdk/internal/jimage/ImageReaderFactory.java
jdk/src/java.base/share/classes/jdk/internal/jimage/ImageStream.java
jdk/src/java.base/share/classes/jdk/internal/jimage/ImageStringsReader.java
--- a/jdk/src/java.base/share/classes/jdk/internal/jimage/BasicImageReader.java	Fri May 27 14:33:48 2016 +0900
+++ b/jdk/src/java.base/share/classes/jdk/internal/jimage/BasicImageReader.java	Fri May 27 12:34:08 2016 -0300
@@ -67,9 +67,9 @@
     static private final boolean MAP_ALL =
             isSystemProperty("jdk.image.map.all", "true", IS_64_BIT ? "true" : "false");
 
-    private final String name;
+    private final Path imagePath;
     private final ByteOrder byteOrder;
-    private final Path imagePath;
+    private final String name;
     private final ByteBuffer memoryMap;
     private final FileChannel channel;
     private final ImageHeader header;
@@ -83,11 +83,9 @@
 
     protected BasicImageReader(Path path, ByteOrder byteOrder)
             throws IOException {
-        Objects.requireNonNull(path);
-        Objects.requireNonNull(byteOrder);
-        this.name = path.toString();
-        this.byteOrder = byteOrder;
-        imagePath = path;
+        this.imagePath = Objects.requireNonNull(path);
+        this.byteOrder = Objects.requireNonNull(byteOrder);
+        this.name = this.imagePath.toString();
 
         ByteBuffer map;
 
@@ -211,6 +209,8 @@
     }
 
     public static void releaseByteBuffer(ByteBuffer buffer) {
+        Objects.requireNonNull(buffer);
+
         if (!MAP_ALL) {
             ImageBufferCache.releaseBuffer(buffer);
         }
@@ -240,10 +240,14 @@
     }
 
     public ImageLocation findLocation(String mn, String rn) {
+        Objects.requireNonNull(mn);
+        Objects.requireNonNull(rn);
+
         return findLocation("/" + mn + "/" + rn);
     }
 
     public synchronized ImageLocation findLocation(String name) {
+        Objects.requireNonNull(name);
         // Details of the algorithm used here can be found in
         // jdk.tools.jlink.internal.PerfectHashBuilder.
         byte[] bytes = ImageStringsReader.mutf8FromString(name);
@@ -287,16 +291,25 @@
     }
 
     public long[] getAttributes(int offset) {
+        if (offset < 0 || offset >= locations.limit()) {
+            throw new IndexOutOfBoundsException("offset");
+        }
+
         ByteBuffer buffer = slice(locations, offset, locations.limit() - offset);
         return ImageLocation.decompress(buffer);
     }
 
     public String getString(int offset) {
+        if (offset < 0 || offset >= strings.limit()) {
+            throw new IndexOutOfBoundsException("offset");
+        }
+
         ByteBuffer buffer = slice(strings, offset, strings.limit() - offset);
         return ImageStringsReader.stringFromByteBuffer(buffer);
     }
 
     private byte[] getBufferBytes(ByteBuffer buffer) {
+        Objects.requireNonNull(buffer);
         byte[] bytes = new byte[buffer.limit()];
         buffer.get(bytes);
 
@@ -343,6 +356,7 @@
     }
 
     public byte[] getResource(String name) {
+        Objects.requireNonNull(name);
         ImageLocation location = findLocation(name);
 
         return location != null ? getResource(location) : null;
@@ -362,6 +376,7 @@
     }
 
     public ByteBuffer getResourceBuffer(ImageLocation loc) {
+        Objects.requireNonNull(loc);
         long offset = loc.getContentOffset() + indexSize;
         long compressedSize = loc.getCompressedSize();
         long uncompressedSize = loc.getUncompressedSize();
@@ -399,6 +414,7 @@
     }
 
     public InputStream getResourceStream(ImageLocation loc) {
+        Objects.requireNonNull(loc);
         byte[] bytes = getResource(loc);
 
         return new ByteArrayInputStream(bytes);
--- a/jdk/src/java.base/share/classes/jdk/internal/jimage/ImageHeader.java	Fri May 27 14:33:48 2016 +0900
+++ b/jdk/src/java.base/share/classes/jdk/internal/jimage/ImageHeader.java	Fri May 27 12:34:08 2016 -0300
@@ -27,6 +27,7 @@
 
 import java.nio.ByteBuffer;
 import java.nio.IntBuffer;
+import java.util.Objects;
 
 /**
  * @implNote This class needs to maintain JDK 8 source compatibility.
@@ -75,6 +76,8 @@
     }
 
     static ImageHeader readFrom(IntBuffer buffer) {
+        Objects.requireNonNull(buffer);
+
         if (buffer.capacity() != HEADER_SLOTS) {
             throw new InternalError("jimage header not the correct size");
         }
@@ -94,11 +97,13 @@
     }
 
     public void writeTo(ImageStream stream) {
+        Objects.requireNonNull(stream);
         stream.ensure(getHeaderSize());
         writeTo(stream.getBuffer());
     }
 
     public void writeTo(ByteBuffer buffer) {
+        Objects.requireNonNull(buffer);
         buffer.putInt(magic);
         buffer.putInt(majorVersion << 16 | minorVersion);
         buffer.putInt(flags);
--- a/jdk/src/java.base/share/classes/jdk/internal/jimage/ImageLocation.java	Fri May 27 14:33:48 2016 +0900
+++ b/jdk/src/java.base/share/classes/jdk/internal/jimage/ImageLocation.java	Fri May 27 12:34:08 2016 -0300
@@ -26,6 +26,7 @@
 package jdk.internal.jimage;
 
 import java.nio.ByteBuffer;
+import java.util.Objects;
 
 /**
  * @implNote This class needs to maintain JDK 8 source compatibility.
@@ -50,8 +51,8 @@
     protected final ImageStrings strings;
 
     public ImageLocation(long[] attributes, ImageStrings strings) {
-        this.attributes = attributes;
-        this.strings = strings;
+        this.attributes = Objects.requireNonNull(attributes);
+        this.strings = Objects.requireNonNull(strings);
     }
 
     ImageStrings getStrings() {
@@ -67,6 +68,7 @@
     }
 
     static long[] decompress(ByteBuffer bytes) {
+        Objects.requireNonNull(bytes);
         long[] attributes = new long[ATTRIBUTE_COUNT];
 
         if (bytes != null) {
@@ -103,6 +105,7 @@
     }
 
     public static byte[] compress(long[] attributes) {
+        Objects.requireNonNull(attributes);
         ImageStream stream = new ImageStream(16);
 
         for (int kind = ATTRIBUTE_END + 1; kind < ATTRIBUTE_COUNT; kind++) {
@@ -124,6 +127,8 @@
      }
 
     public boolean verify(String name) {
+        Objects.requireNonNull(name);
+
         return name.equals(getFullName());
     }
 
@@ -250,6 +255,7 @@
     }
 
     static ImageLocation readFrom(BasicImageReader reader, int offset) {
+        Objects.requireNonNull(reader);
         long[] attributes = reader.getAttributes(offset);
         ImageStringsReader strings = reader.getStrings();
 
--- a/jdk/src/java.base/share/classes/jdk/internal/jimage/ImageReader.java	Fri May 27 14:33:48 2016 +0900
+++ b/jdk/src/java.base/share/classes/jdk/internal/jimage/ImageReader.java	Fri May 27 12:34:08 2016 -0300
@@ -59,6 +59,9 @@
     }
 
     public static ImageReader open(Path imagePath, ByteOrder byteOrder) throws IOException {
+        Objects.requireNonNull(imagePath);
+        Objects.requireNonNull(byteOrder);
+
         return SharedImageReader.open(imagePath, byteOrder);
     }
 
@@ -218,6 +221,9 @@
         }
 
         public static ImageReader open(Path imagePath, ByteOrder byteOrder) throws IOException {
+            Objects.requireNonNull(imagePath);
+            Objects.requireNonNull(byteOrder);
+
             synchronized (OPEN_FILES) {
                 SharedImageReader reader = OPEN_FILES.get(imagePath);
 
@@ -237,6 +243,8 @@
         }
 
         public void close(ImageReader image) throws IOException {
+            Objects.requireNonNull(image);
+
             synchronized (OPEN_FILES) {
                 if (!openers.remove(image)) {
                     throw new IOException("image file already closed");
--- a/jdk/src/java.base/share/classes/jdk/internal/jimage/ImageReaderFactory.java	Fri May 27 14:33:48 2016 +0900
+++ b/jdk/src/java.base/share/classes/jdk/internal/jimage/ImageReaderFactory.java	Fri May 27 12:34:08 2016 -0300
@@ -31,6 +31,7 @@
 import java.nio.file.Paths;
 import java.util.concurrent.ConcurrentHashMap;
 import java.util.Map;
+import java.util.Objects;
 
 /**
  * Factory to get ImageReader
@@ -54,6 +55,7 @@
      * Returns an {@code ImageReader} to read from the given image file
      */
     public static ImageReader get(Path jimage) throws IOException {
+        Objects.requireNonNull(jimage);
         ImageReader reader = readers.get(jimage);
         if (reader != null) {
             return reader;
--- a/jdk/src/java.base/share/classes/jdk/internal/jimage/ImageStream.java	Fri May 27 14:33:48 2016 +0900
+++ b/jdk/src/java.base/share/classes/jdk/internal/jimage/ImageStream.java	Fri May 27 12:34:08 2016 -0300
@@ -28,6 +28,7 @@
 import java.nio.ByteBuffer;
 import java.nio.ByteOrder;
 import java.util.Arrays;
+import java.util.Objects;
 
 /**
  * @implNote This class needs to maintain JDK 8 source compatibility.
@@ -57,16 +58,16 @@
 
     public ImageStream(int size, ByteOrder byteOrder) {
         buffer = ByteBuffer.allocate(size);
-        buffer.order(byteOrder);
+        buffer.order(Objects.requireNonNull(byteOrder));
     }
 
     public ImageStream(byte[] bytes, ByteOrder byteOrder) {
-        buffer = ByteBuffer.wrap(bytes);
-        buffer.order(byteOrder);
+        buffer = ByteBuffer.wrap(Objects.requireNonNull(bytes));
+        buffer.order(Objects.requireNonNull(byteOrder));
     }
 
     public ImageStream(ByteBuffer buffer) {
-        this.buffer = buffer;
+        this.buffer = Objects.requireNonNull(buffer);
     }
 
     public ImageStream align(int alignment) {
--- a/jdk/src/java.base/share/classes/jdk/internal/jimage/ImageStringsReader.java	Fri May 27 14:33:48 2016 +0900
+++ b/jdk/src/java.base/share/classes/jdk/internal/jimage/ImageStringsReader.java	Fri May 27 12:34:08 2016 -0300
@@ -27,6 +27,7 @@
 
 import java.io.UTFDataFormatException;
 import java.nio.ByteBuffer;
+import java.util.Objects;
 
 /**
  * @implNote This class needs to maintain JDK 8 source compatibility.
@@ -40,7 +41,7 @@
     private final BasicImageReader reader;
 
     ImageStringsReader(BasicImageReader reader) {
-        this.reader = reader;
+        this.reader = Objects.requireNonNull(reader);
     }
 
     @Override
@@ -54,7 +55,19 @@
     }
 
     private static int hashCode(byte[] bytes, int offset, int count, int seed) {
-        for (int i = offset, limit = offset + count; i < limit; i++) {
+        Objects.requireNonNull(bytes);
+
+        if (offset < 0 || offset >= bytes.length) {
+            throw new IndexOutOfBoundsException("offset");
+        }
+
+        int limit = offset + count;
+
+        if (limit < 0 || limit > bytes.length) {
+            throw new IndexOutOfBoundsException("limit");
+        }
+
+        for (int i = offset; i < limit; i++) {
             seed = (seed * HASH_MULTIPLIER) ^ (bytes[i] & 0xFF);
         }