jdk/test/java/io/CharArrayReader/OverflowInSkip.java
author igerasim
Thu, 11 Aug 2016 17:03:40 +0300
changeset 40263 6d435de7abbc
permissions -rw-r--r--
8163518: Integer overflow in StringBufferInputStream.read() and CharArrayReader.read/skip() Reviewed-by: rriggs, shade
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
40263
6d435de7abbc 8163518: Integer overflow in StringBufferInputStream.read() and CharArrayReader.read/skip()
igerasim
parents:
diff changeset
     1
/*
6d435de7abbc 8163518: Integer overflow in StringBufferInputStream.read() and CharArrayReader.read/skip()
igerasim
parents:
diff changeset
     2
 * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
6d435de7abbc 8163518: Integer overflow in StringBufferInputStream.read() and CharArrayReader.read/skip()
igerasim
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6d435de7abbc 8163518: Integer overflow in StringBufferInputStream.read() and CharArrayReader.read/skip()
igerasim
parents:
diff changeset
     4
 *
6d435de7abbc 8163518: Integer overflow in StringBufferInputStream.read() and CharArrayReader.read/skip()
igerasim
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
6d435de7abbc 8163518: Integer overflow in StringBufferInputStream.read() and CharArrayReader.read/skip()
igerasim
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
6d435de7abbc 8163518: Integer overflow in StringBufferInputStream.read() and CharArrayReader.read/skip()
igerasim
parents:
diff changeset
     7
 * published by the Free Software Foundation.
6d435de7abbc 8163518: Integer overflow in StringBufferInputStream.read() and CharArrayReader.read/skip()
igerasim
parents:
diff changeset
     8
 *
6d435de7abbc 8163518: Integer overflow in StringBufferInputStream.read() and CharArrayReader.read/skip()
igerasim
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
6d435de7abbc 8163518: Integer overflow in StringBufferInputStream.read() and CharArrayReader.read/skip()
igerasim
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
6d435de7abbc 8163518: Integer overflow in StringBufferInputStream.read() and CharArrayReader.read/skip()
igerasim
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
6d435de7abbc 8163518: Integer overflow in StringBufferInputStream.read() and CharArrayReader.read/skip()
igerasim
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
6d435de7abbc 8163518: Integer overflow in StringBufferInputStream.read() and CharArrayReader.read/skip()
igerasim
parents:
diff changeset
    13
 * accompanied this code).
6d435de7abbc 8163518: Integer overflow in StringBufferInputStream.read() and CharArrayReader.read/skip()
igerasim
parents:
diff changeset
    14
 *
6d435de7abbc 8163518: Integer overflow in StringBufferInputStream.read() and CharArrayReader.read/skip()
igerasim
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
6d435de7abbc 8163518: Integer overflow in StringBufferInputStream.read() and CharArrayReader.read/skip()
igerasim
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
6d435de7abbc 8163518: Integer overflow in StringBufferInputStream.read() and CharArrayReader.read/skip()
igerasim
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
6d435de7abbc 8163518: Integer overflow in StringBufferInputStream.read() and CharArrayReader.read/skip()
igerasim
parents:
diff changeset
    18
 *
6d435de7abbc 8163518: Integer overflow in StringBufferInputStream.read() and CharArrayReader.read/skip()
igerasim
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
6d435de7abbc 8163518: Integer overflow in StringBufferInputStream.read() and CharArrayReader.read/skip()
igerasim
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
6d435de7abbc 8163518: Integer overflow in StringBufferInputStream.read() and CharArrayReader.read/skip()
igerasim
parents:
diff changeset
    21
 * questions.
6d435de7abbc 8163518: Integer overflow in StringBufferInputStream.read() and CharArrayReader.read/skip()
igerasim
parents:
diff changeset
    22
 */
6d435de7abbc 8163518: Integer overflow in StringBufferInputStream.read() and CharArrayReader.read/skip()
igerasim
parents:
diff changeset
    23
6d435de7abbc 8163518: Integer overflow in StringBufferInputStream.read() and CharArrayReader.read/skip()
igerasim
parents:
diff changeset
    24
/* @test
6d435de7abbc 8163518: Integer overflow in StringBufferInputStream.read() and CharArrayReader.read/skip()
igerasim
parents:
diff changeset
    25
 * @bug 8163518
6d435de7abbc 8163518: Integer overflow in StringBufferInputStream.read() and CharArrayReader.read/skip()
igerasim
parents:
diff changeset
    26
 * @summary Integer overflow when skipping a lot
6d435de7abbc 8163518: Integer overflow in StringBufferInputStream.read() and CharArrayReader.read/skip()
igerasim
parents:
diff changeset
    27
 */
6d435de7abbc 8163518: Integer overflow in StringBufferInputStream.read() and CharArrayReader.read/skip()
igerasim
parents:
diff changeset
    28
6d435de7abbc 8163518: Integer overflow in StringBufferInputStream.read() and CharArrayReader.read/skip()
igerasim
parents:
diff changeset
    29
import java.io.CharArrayReader;
6d435de7abbc 8163518: Integer overflow in StringBufferInputStream.read() and CharArrayReader.read/skip()
igerasim
parents:
diff changeset
    30
6d435de7abbc 8163518: Integer overflow in StringBufferInputStream.read() and CharArrayReader.read/skip()
igerasim
parents:
diff changeset
    31
public class OverflowInSkip {
6d435de7abbc 8163518: Integer overflow in StringBufferInputStream.read() and CharArrayReader.read/skip()
igerasim
parents:
diff changeset
    32
    public static void main(String[] args) throws Exception {
6d435de7abbc 8163518: Integer overflow in StringBufferInputStream.read() and CharArrayReader.read/skip()
igerasim
parents:
diff changeset
    33
        char[] a = "_123456789_123456789_123456789_123456789"
6d435de7abbc 8163518: Integer overflow in StringBufferInputStream.read() and CharArrayReader.read/skip()
igerasim
parents:
diff changeset
    34
                .toCharArray(); // a.length > 33
6d435de7abbc 8163518: Integer overflow in StringBufferInputStream.read() and CharArrayReader.read/skip()
igerasim
parents:
diff changeset
    35
        try (CharArrayReader car = new CharArrayReader(a)) {
6d435de7abbc 8163518: Integer overflow in StringBufferInputStream.read() and CharArrayReader.read/skip()
igerasim
parents:
diff changeset
    36
            long small = 33;
6d435de7abbc 8163518: Integer overflow in StringBufferInputStream.read() and CharArrayReader.read/skip()
igerasim
parents:
diff changeset
    37
            long big = Long.MAX_VALUE;
6d435de7abbc 8163518: Integer overflow in StringBufferInputStream.read() and CharArrayReader.read/skip()
igerasim
parents:
diff changeset
    38
6d435de7abbc 8163518: Integer overflow in StringBufferInputStream.read() and CharArrayReader.read/skip()
igerasim
parents:
diff changeset
    39
            long smallSkip = car.skip(small);
6d435de7abbc 8163518: Integer overflow in StringBufferInputStream.read() and CharArrayReader.read/skip()
igerasim
parents:
diff changeset
    40
            if (smallSkip != small)
6d435de7abbc 8163518: Integer overflow in StringBufferInputStream.read() and CharArrayReader.read/skip()
igerasim
parents:
diff changeset
    41
                throw new Exception("Expected to skip " + small
6d435de7abbc 8163518: Integer overflow in StringBufferInputStream.read() and CharArrayReader.read/skip()
igerasim
parents:
diff changeset
    42
                        + " chars, but skipped " + smallSkip);
6d435de7abbc 8163518: Integer overflow in StringBufferInputStream.read() and CharArrayReader.read/skip()
igerasim
parents:
diff changeset
    43
6d435de7abbc 8163518: Integer overflow in StringBufferInputStream.read() and CharArrayReader.read/skip()
igerasim
parents:
diff changeset
    44
            long expSkip = a.length - small;
6d435de7abbc 8163518: Integer overflow in StringBufferInputStream.read() and CharArrayReader.read/skip()
igerasim
parents:
diff changeset
    45
            long bigSkip = car.skip(big);
6d435de7abbc 8163518: Integer overflow in StringBufferInputStream.read() and CharArrayReader.read/skip()
igerasim
parents:
diff changeset
    46
            if (bigSkip != expSkip)
6d435de7abbc 8163518: Integer overflow in StringBufferInputStream.read() and CharArrayReader.read/skip()
igerasim
parents:
diff changeset
    47
                throw new Exception("Expected to skip " + expSkip
6d435de7abbc 8163518: Integer overflow in StringBufferInputStream.read() and CharArrayReader.read/skip()
igerasim
parents:
diff changeset
    48
                        + " chars, but skipped " + bigSkip);
6d435de7abbc 8163518: Integer overflow in StringBufferInputStream.read() and CharArrayReader.read/skip()
igerasim
parents:
diff changeset
    49
        }
6d435de7abbc 8163518: Integer overflow in StringBufferInputStream.read() and CharArrayReader.read/skip()
igerasim
parents:
diff changeset
    50
    }
6d435de7abbc 8163518: Integer overflow in StringBufferInputStream.read() and CharArrayReader.read/skip()
igerasim
parents:
diff changeset
    51
}