< prev index next >

src/java.base/share/classes/java/io/CharArrayReader.java

Print this page
rev 15329 : imported patch 8163518-Integer-overflow-in-StringBufferInputStream-read-byte-int-int

@@ -129,12 +129,14 @@
             }
 
             if (pos >= count) {
                 return -1;
             }
-            if (pos + len > count) {
-                len = count - pos;
+
+            int avail = count - pos;
+            if (len > avail) {
+                len = avail;
             }
             if (len <= 0) {
                 return 0;
             }
             System.arraycopy(buf, pos, b, off, len);

@@ -156,18 +158,17 @@
      * @exception  IOException If the stream is closed, or an I/O error occurs
      */
     public long skip(long n) throws IOException {
         synchronized (lock) {
             ensureOpen();
-            if (pos + n > count) {
-                n = count - pos;
-            }
-            if (n < 0) {
-                return 0;
+
+            long k = count - pos;
+            if (n < k) {
+                k = (n <= 0) ? 0 : n;
             }
-            pos += n;
-            return n;
+            pos += k;
+            return k;
         }
     }
 
     /**
      * Tells whether this stream is ready to be read.  Character-array readers
< prev index next >