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:
russell@unturf.com 2026-03-26 17:11:57 -04:00
commit 0a580b313d
70422 changed files with 17213626 additions and 0 deletions

View file

@ -0,0 +1,282 @@
/*
* Copyright (c) 2006, 2010, 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 4181483
* @summary Test if DataInputStream methods will check if the stream
* has been closed.
*/
import java.io.*;
public enum OpsAfterClose {
READ { boolean check(DataInputStream is) {
try {
int read = is.read();
System.out.println("read returns: " + read);
} catch (IOException io) {
System.out.print("Excep Msg: "+ io.getMessage() + ", ");
return true;
}
return false;
} },
READ_BUF { boolean check(DataInputStream is) {
try {
byte buf[] = new byte[2];
int read = is.read(buf);
System.out.println("read(buf) returns: " + read);
} catch (IOException io) {
System.out.print("Excep Msg: "+ io.getMessage() + ", ");
return true;
}
return false;
} },
READ_BUF_OFF { boolean check(DataInputStream is) {
try {
byte buf[] = new byte[2];
int len = 1;
int read = is.read(buf, 0, len);
System.out.println("read(buf, 0, len) returns: " + read);
} catch (IOException io) {
System.out.print("Excep Msg: "+ io.getMessage() + ", ");
return true;
}
return false;
} },
AVAILABLE { boolean check(DataInputStream is) {
try {
int avail = is.available();
System.out.println("available() returns: " + avail);
return false;
} catch (IOException io) {
System.out.print("Excep Msg: "+ io.getMessage() + ", ");
return true;
}
} },
SKIP { boolean check(DataInputStream is) {
try {
long skipped = is.skip(1);
System.out.println("skip() returns: " + skipped);
} catch (IOException io) {
System.out.print("Excep Msg: "+ io.getMessage() + ", ");
return true;
}
return false;
} },
MARK { boolean check(DataInputStream is) {
is.mark(20);
return true;
} },
RESET { boolean check(DataInputStream is) {
try {
is.reset();
} catch (IOException io) {
System.out.print("Excep Msg: "+ io.getMessage() + ", ");
return true;
}
return false;
} },
MARK_SUPPORTED { boolean check(DataInputStream is) {
is.markSupported();
return true;
} },
CLOSE { boolean check(DataInputStream is) {
try {
is.close();
return true; // No Exception thrown on windows
} catch (IOException io) {
System.out.print("Excep Msg: "+ io.getMessage() + ", ");
return true; // Exception thrown on solaris and linux
}
}},
READ_BYTE { boolean check(DataInputStream is) {
try {
is.readByte();
} catch (IOException io) {
System.out.print("Excep Msg: "+ io.getMessage() + ", ");
return true;
}
return false;
} },
READ_CHAR { boolean check(DataInputStream is) {
try {
is.readChar();
} catch (IOException io) {
System.out.print("Excep Msg: "+ io.getMessage() + ", ");
return true;
}
return false;
} },
READ_DOUBLE { boolean check(DataInputStream is) {
try {
is.readDouble();
} catch (IOException io) {
System.out.print("Excep Msg: "+ io.getMessage() + ", ");
return true;
}
return false;
} },
READ_FLOAT { boolean check(DataInputStream is) {
try {
is.readFloat();
} catch (IOException io) {
System.out.print("Excep Msg: "+ io.getMessage() + ", ");
return true;
}
return false;
} },
READ_INT { boolean check(DataInputStream is) {
try {
is.readInt();
} catch (IOException io) {
System.out.print("Excep Msg: "+ io.getMessage() + ", ");
return true;
}
return false;
} },
READ_LONG { boolean check(DataInputStream is) {
try {
is.readLong();
} catch (IOException io) {
System.out.print("Excep Msg: "+ io.getMessage() + ", ");
return true;
}
return false;
} },
READ_SHORT { boolean check(DataInputStream is) {
try {
is.readShort();
} catch (IOException io) {
System.out.print("Excep Msg: "+ io.getMessage() + ", ");
return true;
}
return false;
} },
READ_UnsignedByte { boolean check(DataInputStream is) {
try {
is.readUnsignedByte();
} catch (IOException io) {
System.out.print("Excep Msg: "+ io.getMessage() + ", ");
return true;
}
return false;
} },
READ_UnsignedShort { boolean check(DataInputStream is) {
try {
is.readUnsignedShort();
} catch (IOException io) {
System.out.print("Excep Msg: "+ io.getMessage() + ", ");
return true;
}
return false;
} },
READ_UTF { boolean check(DataInputStream is) {
try {
is.readUTF();
} catch (IOException io) {
System.out.print("Excep Msg: "+ io.getMessage() + ", ");
return true;
}
return false;
} },
SKIP_BYTES { boolean check(DataInputStream is) {
try {
is.skipBytes(1);
} catch (IOException io) {
System.out.print("Excep Msg: "+ io.getMessage() + ", ");
return true;
}
return false;
} },
READ_FULLY { boolean check(DataInputStream is) {
try {
is.readFully(new byte[1]);
} catch (IOException io) {
System.out.print("Excep Msg: "+ io.getMessage() + ", ");
return true;
}
return false;
} },
READ_FULLY_BUF { boolean check(DataInputStream is) {
try {
is.readFully(new byte[1], 0, 1);
} catch (IOException io) {
System.out.print("Excep Msg: "+ io.getMessage() + ", ");
return true;
}
return false;
} };
abstract boolean check(DataInputStream is);
public static void main(String args[]) throws Exception {
boolean failed = false;
File f = new File(System.getProperty("test.dir", "."),
"f.txt");
f.createNewFile();
f.deleteOnExit();
FileInputStream fis = new FileInputStream(f);
try {
DataInputStream dis = new DataInputStream(
new FileInputStream(f));
try {
if (testDataInputStream(dis)) {
failed = true;
}
} finally {
dis.close();
}
} finally {
fis.close();
}
}
private static boolean testDataInputStream(DataInputStream is)
throws Exception {
is.close();
boolean failed = false;
boolean result;
System.out.println("Testing :" + is);
for (OpsAfterClose op : OpsAfterClose.values()) {
result = op.check(is);
if (!result) {
failed = true;
}
System.out.println(op + ":" + result);
}
if (failed) {
System.out.println("Test failed for the failed operation{s}" +
" above for :" + is);
}
return failed;
}
}

View file

@ -0,0 +1,129 @@
/*
* Copyright (c) 1999, 2020, 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 4214513 8245036
* @summary Passing a negative offset or length,
* or passing a combination of offset and length too big
* for readFully must throw IndexOutOfBoundsException.
*/
import java.io.*;
public class ReadFully {
private static final void testNegativeOffset() throws Exception {
File file = new File(System.getProperty("test.src"),
"ReadFully.java");
try (FileInputStream in = new FileInputStream(file);
DataInputStream dis = new DataInputStream(in);) {
byte[] buffer = new byte[100];
dis.readFully(buffer, -1, buffer.length);
throw new RuntimeException("Test testNegativeOffset() failed");
} catch (IndexOutOfBoundsException ignore) {
}
}
private static final void testNegativeLength() throws Exception {
File file = new File(System.getProperty("test.src"),
"ReadFully.java");
try (FileInputStream in = new FileInputStream(file);
DataInputStream dis = new DataInputStream(in);) {
byte[] buffer = new byte[100];
dis.readFully(buffer, 0, -1);
throw new RuntimeException("Test testNegativeLength() failed");
} catch (IndexOutOfBoundsException ignore) {
}
}
private static final void testNegativeOffsetZeroLength() throws Exception {
File file = new File(System.getProperty("test.src"),
"ReadFully.java");
try (FileInputStream in = new FileInputStream(file);
DataInputStream dis = new DataInputStream(in);) {
byte[] buffer = new byte[100];
dis.readFully(buffer, -1, 0);
throw new RuntimeException("Test testNegativeOffsetZeroLength() failed");
} catch (IndexOutOfBoundsException ignore) {
}
}
private static final void testBigOffsetLength1() throws Exception {
File file = new File(System.getProperty("test.src"),
"ReadFully.java");
try (FileInputStream in = new FileInputStream(file);
DataInputStream dis = new DataInputStream(in);) {
byte[] buffer = new byte[100];
dis.readFully(buffer, 0, buffer.length + 1);
throw new RuntimeException("Test testBigOffsetLength1() failed");
} catch (IndexOutOfBoundsException ignore) {
}
}
private static final void testBigOffsetLength2() throws Exception {
File file = new File(System.getProperty("test.src"),
"ReadFully.java");
try (FileInputStream in = new FileInputStream(file);
DataInputStream dis = new DataInputStream(in);) {
byte[] buffer = new byte[100];
dis.readFully(buffer, 1, buffer.length);
throw new RuntimeException("Test testBigOffsetLength2() failed");
} catch (IndexOutOfBoundsException ignore) {
}
}
private static final void testBigOffsetLength3() throws Exception {
File file = new File(System.getProperty("test.src"),
"ReadFully.java");
try (FileInputStream in = new FileInputStream(file);
DataInputStream dis = new DataInputStream(in);) {
byte[] buffer = new byte[100];
dis.readFully(buffer, buffer.length, 1);
throw new RuntimeException("Test testBigOffsetLength3() failed");
} catch (IndexOutOfBoundsException ignore) {
}
}
private static final void testBigOffsetLength4() throws Exception {
File file = new File(System.getProperty("test.src"),
"ReadFully.java");
try (FileInputStream in = new FileInputStream(file);
DataInputStream dis = new DataInputStream(in);) {
byte[] buffer = new byte[100];
dis.readFully(buffer, buffer.length + 1, 0);
throw new RuntimeException("Test testBigOffsetLength4() failed");
} catch (IndexOutOfBoundsException ignore) {
}
}
public static final void main(String[] args) throws Exception {
testNegativeOffset();
testNegativeLength();
testNegativeOffsetZeroLength();
testBigOffsetLength1();
testBigOffsetLength2();
testBigOffsetLength3();
testBigOffsetLength4();
}
}

View file

@ -0,0 +1,50 @@
/*
* 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 4093646
@summary Make sure readLine would not push back -1.
*/
import java.io.*;
public class ReadLinePushback {
public static void main(String args[]) throws Exception {
PushbackInputStream pis = new PushbackInputStream
((new StringBufferInputStream("\r")));
DataInputStream dis = new DataInputStream(pis);
String line = dis.readLine();
if (line == null) {
throw new Exception ("Got null, should return empty line");
}
int count = pis.available();
if (count != 0) {
throw new Exception ("Test failed: available() returns "
+ count + "when the file is empty");
}
}
}

View file

@ -0,0 +1,87 @@
/*
* Copyright (c) 2003, 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 4806007
* @summary Checks for vague exceptions from writeUTF/readUTF
* @key randomness
*/
import java.io.*;
import java.util.*;
public class ReadUTF {
private static Random generator = new Random();
private static final int TEST_ITERATIONS = 1000;
private static final int A_NUMBER_NEAR_65535 = 60000;
private static final int MAX_CORRUPTIONS_PER_CYCLE = 3;
public static final void main(String[] args) throws Exception {
for (int i=0; i<TEST_ITERATIONS; i++) {
try {
writeAndReadAString();
} catch (UTFDataFormatException utfdfe) {
if (utfdfe.getMessage() == null)
throw new RuntimeException("vague exception thrown");
} catch (EOFException eofe) {
// These are rare and beyond the scope of the test
}
}
}
private static void writeAndReadAString() throws Exception {
// Write out a string whose UTF-8 encoding is quite possibly
// longer than 65535 bytes
int length = generator.nextInt(A_NUMBER_NEAR_65535) + 1;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
StringBuffer testBuffer = new StringBuffer();
for (int i=0; i<length; i++)
testBuffer.append((char)generator.nextInt());
String testString = testBuffer.toString();
DataOutputStream dos = new DataOutputStream(baos);
dos.writeUTF(testString);
// Corrupt the data to produce malformed characters
byte[] testBytes = baos.toByteArray();
int dataLength = testBytes.length;
int corruptions = generator.nextInt(MAX_CORRUPTIONS_PER_CYCLE);
for (int i=0; i<corruptions; i++) {
int index = generator.nextInt(dataLength);
testBytes[index] = (byte)generator.nextInt();
}
// Pay special attention to mangling the end to produce
// partial characters at end
testBytes[dataLength-1] = (byte)generator.nextInt();
testBytes[dataLength-2] = (byte)generator.nextInt();
// Attempt to decode the bytes back into a String
ByteArrayInputStream bais = new ByteArrayInputStream(testBytes);
DataInputStream dis = new DataInputStream(bais);
dis.readUTF();
}
}

View file

@ -0,0 +1,87 @@
/*
* Copyright (c) 1997, 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 4085938
@summary Check for the correct behaviour of DataInputStream.skipBytes
*/
import java.io.*;
public class SkipBytes{
private static void dotest(DataInputStream dis, int pos, int total,
int toskip, int expected) {
try {
System.err.println("\n\nTotal bytes in the stream = " + total);
System.err.println("Currently at position = " + pos);
System.err.println("Bytes to skip = " + toskip);
System.err.println("Expected result = " + expected);
int skipped = dis.skipBytes(toskip);
System.err.println("Actual skipped = " + skipped);
if (skipped != expected) {
throw new RuntimeException
("DataInputStream.skipBytes does not return expected value");
}
} catch(EOFException e){
throw new RuntimeException
("DataInputStream.skipBytes throws unexpected EOFException");
} catch (IOException e) {
System.err.println("IOException is thrown - possible result");
}
}
public static void main(String args[]) throws Exception {
DataInputStream dis = new DataInputStream(new MyInputStream());
dotest(dis, 0, 11, -1, 0);
dotest(dis, 0, 11, 5, 5);
System.err.println("\n***CAUTION**** - may go into an infinite loop");
dotest(dis, 5, 11, 20, 6);
}
}
class MyInputStream extends InputStream {
private int readctr = 0;
public int read() {
if (readctr > 10){
return -1;
}
else{
readctr++;
return 0;
}
}
public int available() { return 0; }
}