8155608: String intrinsic range checks are not strict enough
authorthartmann
Fri, 13 May 2016 08:31:23 +0200
changeset 38790 b74a24c96491
parent 38789 178dd1b12732
child 38791 f676b24e8ee3
8155608: String intrinsic range checks are not strict enough Summary: Range checks in inflate, compress and getChars are not strict enough. Reviewed-by: kvn, twisti, jrose
jdk/src/java.base/share/classes/java/lang/StringLatin1.java
jdk/src/java.base/share/classes/java/lang/StringUTF16.java
--- a/jdk/src/java.base/share/classes/java/lang/StringLatin1.java	Wed May 11 09:13:45 2016 +0200
+++ b/jdk/src/java.base/share/classes/java/lang/StringLatin1.java	Fri May 13 08:31:23 2016 +0200
@@ -557,7 +557,7 @@
 
     // inflatedCopy byte[] -> char[]
     @HotSpotIntrinsicCandidate
-    private static void inflate(byte[] src, int srcOff, char[] dst, int dstOff, int len) {
+    public static void inflate(byte[] src, int srcOff, char[] dst, int dstOff, int len) {
         for (int i = 0; i < len; i++) {
             dst[dstOff++] = (char)(src[srcOff++] & 0xff);
         }
@@ -567,7 +567,7 @@
     @HotSpotIntrinsicCandidate
     public static void inflate(byte[] src, int srcOff, byte[] dst, int dstOff, int len) {
         // We need a range check here because 'putChar' has no checks
-        checkBoundsOffCount(dstOff, len, dst.length);
+        checkBoundsOffCount(dstOff << 1, len << 1, dst.length);
         for (int i = 0; i < len; i++) {
             StringUTF16.putChar(dst, dstOff++, src[srcOff++] & 0xff);
         }
--- a/jdk/src/java.base/share/classes/java/lang/StringUTF16.java	Wed May 11 09:13:45 2016 +0200
+++ b/jdk/src/java.base/share/classes/java/lang/StringUTF16.java	Fri May 13 08:31:23 2016 +0200
@@ -144,7 +144,7 @@
 
     // compressedCopy char[] -> byte[]
     @HotSpotIntrinsicCandidate
-    private static int compress(char[] src, int srcOff, byte[] dst, int dstOff, int len) {
+    public static int compress(char[] src, int srcOff, byte[] dst, int dstOff, int len) {
         for (int i = 0; i < len; i++) {
             char c = src[srcOff];
             if (c > 0xFF) {
@@ -162,7 +162,7 @@
     @HotSpotIntrinsicCandidate
     public static int compress(byte[] src, int srcOff, byte[] dst, int dstOff, int len) {
         // We need a range check here because 'getChar' has no checks
-        checkBoundsOffCount(srcOff, len, src.length);
+        checkBoundsOffCount(srcOff << 1, len << 1, src.length);
         for (int i = 0; i < len; i++) {
             char c = getChar(src, srcOff);
             if (c > 0xFF) {
@@ -211,7 +211,9 @@
     @HotSpotIntrinsicCandidate
     public static void getChars(byte[] value, int srcBegin, int srcEnd, char dst[], int dstBegin) {
         // We need a range check here because 'getChar' has no checks
-        checkBoundsOffCount(srcBegin, srcEnd - srcBegin, value.length);
+        if (srcBegin < srcEnd) {
+            checkBoundsOffCount(srcBegin << 1, (srcEnd - srcBegin) << 1, value.length);
+        }
         for (int i = srcBegin; i < srcEnd; i++) {
             dst[dstBegin++] = getChar(value, i);
         }