undefect. CWE-407 — 63 sites patched across 27 ecosystems
Authors: russell@unturf.com · brackishbert@gmail.com · foxhop.net · TimeHexOn.com Patches, unit tests, benchmarks, whitepaper, and outreach briefs. Public domain — no copyright claimed. Use freely.
This commit is contained in:
commit
0a580b313d
70422 changed files with 17213626 additions and 0 deletions
79
test/jdk/java/io/CharArrayReader/Constructor.java
Normal file
79
test/jdk/java/io/CharArrayReader/Constructor.java
Normal file
|
|
@ -0,0 +1,79 @@
|
|||
/*
|
||||
* Copyright (c) 1998, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
/* @test
|
||||
@bug 4140445 4140451
|
||||
@summary Test if constructor will check for illegal
|
||||
arguments.
|
||||
*/
|
||||
|
||||
|
||||
|
||||
import java.io.*;
|
||||
|
||||
public class Constructor {
|
||||
public static void main(String argv[]) throws Exception {
|
||||
int values[] = {Integer.MIN_VALUE, -1, 0, 1, 4, 16, 31,
|
||||
32, 33, Integer.MAX_VALUE};
|
||||
char b[][] = {null, new char[32]};
|
||||
|
||||
int i = 0, j = 0, k = 0;
|
||||
boolean nullPtr = false, indexOutBnd = false;
|
||||
|
||||
for (i = 0; i < b.length; i++) {
|
||||
for ( j = 0; j < values.length; j++) {
|
||||
for ( k = 0; k < values.length; k++) {
|
||||
|
||||
nullPtr = (b[i] == null);
|
||||
|
||||
int bufLen = nullPtr ? 0 : b[i].length;
|
||||
indexOutBnd = (values[j] < 0)
|
||||
|| (values[j] > bufLen)
|
||||
|| (values[k] < 0)
|
||||
|| ((values[j] + values[k]) < 0);
|
||||
|
||||
try {
|
||||
CharArrayReader rdr = new CharArrayReader
|
||||
(b[i], values[j], values[k]);
|
||||
} catch (NullPointerException e) {
|
||||
if (!nullPtr) {
|
||||
throw new Exception
|
||||
("should not throw NullPointerException");
|
||||
}
|
||||
continue;
|
||||
} catch (IllegalArgumentException e) {
|
||||
if (!indexOutBnd) {
|
||||
throw new Exception
|
||||
("should not throw IllegalArgumentException");
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
if (nullPtr || indexOutBnd) {
|
||||
throw new Exception("Failed to detect illegal argument");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
50
test/jdk/java/io/CharArrayReader/OverflowInRead.java
Normal file
50
test/jdk/java/io/CharArrayReader/OverflowInRead.java
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
/*
|
||||
* Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
/* @test
|
||||
* @bug 8163518
|
||||
* @summary Integer overflow when reading in large buffer
|
||||
* @requires (sun.arch.data.model == "64" & os.maxMemory > 8g)
|
||||
* @run main/othervm -Xmx8g OverflowInRead
|
||||
*/
|
||||
|
||||
import java.io.CharArrayReader;
|
||||
|
||||
public class OverflowInRead {
|
||||
public static void main(String[] args) throws Exception {
|
||||
char[] a = "_123456789_123456789_123456789_123456789"
|
||||
.toCharArray(); // a.length > 33
|
||||
try (CharArrayReader car = new CharArrayReader(a)) {
|
||||
int len1 = 33;
|
||||
char[] buf1 = new char[len1];
|
||||
if (car.read(buf1, 0, len1) != len1)
|
||||
throw new Exception("Expected to read " + len1 + " chars");
|
||||
|
||||
int len2 = Integer.MAX_VALUE - 32;
|
||||
char[] buf2 = new char[len2];
|
||||
int expLen2 = a.length - len1;
|
||||
if (car.read(buf2, 0, len2) != expLen2)
|
||||
throw new Exception("Expected to read " + expLen2 + " chars");
|
||||
}
|
||||
}
|
||||
}
|
||||
51
test/jdk/java/io/CharArrayReader/OverflowInSkip.java
Normal file
51
test/jdk/java/io/CharArrayReader/OverflowInSkip.java
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
/*
|
||||
* Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
/* @test
|
||||
* @bug 8163518
|
||||
* @summary Integer overflow when skipping a lot
|
||||
*/
|
||||
|
||||
import java.io.CharArrayReader;
|
||||
|
||||
public class OverflowInSkip {
|
||||
public static void main(String[] args) throws Exception {
|
||||
char[] a = "_123456789_123456789_123456789_123456789"
|
||||
.toCharArray(); // a.length > 33
|
||||
try (CharArrayReader car = new CharArrayReader(a)) {
|
||||
long small = 33;
|
||||
long big = Long.MAX_VALUE;
|
||||
|
||||
long smallSkip = car.skip(small);
|
||||
if (smallSkip != small)
|
||||
throw new Exception("Expected to skip " + small
|
||||
+ " chars, but skipped " + smallSkip);
|
||||
|
||||
long expSkip = a.length - small;
|
||||
long bigSkip = car.skip(big);
|
||||
if (bigSkip != expSkip)
|
||||
throw new Exception("Expected to skip " + expSkip
|
||||
+ " chars, but skipped " + bigSkip);
|
||||
}
|
||||
}
|
||||
}
|
||||
87
test/jdk/java/io/CharArrayReader/ReadCharBuffer.java
Normal file
87
test/jdk/java/io/CharArrayReader/ReadCharBuffer.java
Normal file
|
|
@ -0,0 +1,87 @@
|
|||
/*
|
||||
* Copyright (c) 2021, 2026, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
/*
|
||||
* @test
|
||||
* @bug 4926314
|
||||
* @summary Test for CharArrayReader#read(CharBuffer).
|
||||
* @run junit ReadCharBuffer
|
||||
*/
|
||||
|
||||
import java.io.CharArrayReader;
|
||||
import java.io.IOException;
|
||||
import java.io.Reader;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.CharBuffer;
|
||||
import java.util.Arrays;
|
||||
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.MethodSource;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
public class ReadCharBuffer {
|
||||
|
||||
private static final int BUFFER_SIZE = 7;
|
||||
|
||||
public static CharBuffer[] buffers() {
|
||||
// test both on-heap and off-heap buffers as they may use different code paths
|
||||
return new CharBuffer[] {
|
||||
CharBuffer.allocate(BUFFER_SIZE),
|
||||
ByteBuffer.allocateDirect(BUFFER_SIZE * 2).asCharBuffer()
|
||||
};
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@MethodSource("buffers")
|
||||
public void read(CharBuffer buffer) throws IOException {
|
||||
fillBuffer(buffer);
|
||||
|
||||
try (Reader reader = new CharArrayReader("ABCD".toCharArray())) {
|
||||
buffer.limit(3);
|
||||
buffer.position(1);
|
||||
assertEquals(2, reader.read(buffer));
|
||||
assertEquals(3, buffer.position());
|
||||
assertEquals(3, buffer.limit());
|
||||
|
||||
buffer.limit(7);
|
||||
buffer.position(4);
|
||||
assertEquals(2, reader.read(buffer));
|
||||
assertEquals(6, buffer.position());
|
||||
assertEquals(7, buffer.limit());
|
||||
|
||||
assertEquals(-1, reader.read(buffer));
|
||||
}
|
||||
|
||||
buffer.clear();
|
||||
assertEquals("xABxCDx", buffer.toString());
|
||||
}
|
||||
|
||||
private void fillBuffer(CharBuffer buffer) {
|
||||
char[] filler = new char[BUFFER_SIZE];
|
||||
Arrays.fill(filler, 'x');
|
||||
buffer.put(filler);
|
||||
buffer.clear();
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue