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,68 @@
/*
* Copyright (c) 1998, 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 4101707
@summary Test if fill() will behave correctly at EOF to
allow file to grow.
*/
import java.io.*;
public class GrowAfterEOF {
public static void main(String[] args) throws Exception {
File input = new File(".", "TestEOFInput.txt");
RandomAccessFile rf = new RandomAccessFile(input, "rw");
try {
BufferedReader r = new BufferedReader
(new InputStreamReader(new FileInputStream(input)));
try {
// write something
rf.writeBytes("a line");
// read till the end of file
while (r.readLine() != null);
// append to the end of the file
rf.seek(rf.length());
rf.writeBytes("new line");
// now try to read again
boolean readMore = false;
while (r.readLine() != null) {
readMore = true;
}
if (!readMore) {
input.delete();
throw new Exception("Failed test: unable to read!");
} else {
input.delete();
}
} finally {
r.close();
}
} finally {
rf.close();
}
}
}

View file

@ -0,0 +1,45 @@
/*
* 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 4149935
* @summary Create with a null stream should throw an exception
*/
import java.io.*;
public class NullCreate {
public static void main(String args[])
{
try{
InputStreamReader osw = new InputStreamReader(null);
} catch (NullPointerException e){
// No problem - null create argument caught
return;
}
throw new RuntimeException("Create with null did not throw an error");
}
}

View file

@ -0,0 +1,106 @@
/*
* Copyright (c) 2001, 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 4401798
@summary Check that single-character reads work properly
*/
import java.io.*;
public class One {
private abstract static class Test {
InputStreamReader isr;
StringBuffer sb;
String expect;
Test(byte[] in, String expect) throws Exception {
isr = new InputStreamReader(new ByteArrayInputStream(in), "UTF-8");
sb = new StringBuffer(expect.length());
this.expect = expect;
go();
}
void go() throws Exception {
read();
if (!expect.equals(sb.toString()))
throw new Exception("Expected " + expect
+ ", got " + sb.toString());
}
abstract void read() throws IOException;
}
private static void test(String expect) throws Exception {
byte[] in = expect.getBytes("UTF-8");
new Test(in, expect) {
public void read() throws IOException {
for (;;) {
int c;
if ((c = isr.read()) == -1)
break;
sb.append((char)c);
}
}};
new Test(in, expect) {
public void read() throws IOException {
for (;;) {
char[] cb = new char[1];
if (isr.read(cb) == -1)
break;
sb.append(cb[0]);
}
}};
new Test(in, expect) {
public void read() throws IOException {
for (;;) {
char[] cb = new char[2];
int n;
if ((n = isr.read(cb)) == -1)
break;
sb.append(cb[0]);
if (n == 2)
sb.append(cb[1]);
}
}};
}
public static void main(String[] args) throws Exception {
test("x");
test("xy");
test("xyz");
test("\ud800\udc00");
test("x\ud800\udc00");
}
}

View file

@ -0,0 +1,105 @@
/*
* 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 8287003
* @summary Test for InputStreamReader#read(CharBuffer).
* @run junit ReadCharBuffer
*/
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.CodingErrorAction;
import java.util.Arrays;
import java.util.stream.Stream;
import static java.nio.charset.StandardCharsets.US_ASCII;
import static java.nio.charset.StandardCharsets.UTF_8;
import org.junit.jupiter.api.Test;
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 = 24;
public static Stream<CharBuffer> buffers() {
// test both on-heap and off-heap buffers as they make use different code paths
return Stream.of(CharBuffer.allocate(BUFFER_SIZE),
ByteBuffer.allocateDirect(BUFFER_SIZE * 2).asCharBuffer());
}
private void fillBuffer(CharBuffer buffer) {
char[] filler = new char[BUFFER_SIZE];
Arrays.fill(filler, 'x');
buffer.put(filler);
buffer.clear();
}
@ParameterizedTest
@MethodSource("buffers")
public void read(CharBuffer buffer) throws IOException {
fillBuffer(buffer);
try (Reader reader = new InputStreamReader(new ByteArrayInputStream("ABCDEFGHIJKLMNOPQRTUVWXYZ".getBytes(US_ASCII)), US_ASCII)) {
buffer.limit(7);
buffer.position(1);
assertEquals(reader.read(buffer), 6);
assertEquals(buffer.position(), 7);
assertEquals(buffer.limit(), 7);
buffer.limit(16);
buffer.position(8);
assertEquals(reader.read(buffer), 8);
assertEquals(buffer.position(), 16);
assertEquals(buffer.limit(), 16);
}
buffer.clear();
assertEquals(buffer.toString(), "xABCDEFxGHIJKLMNxxxxxxxx");
}
@Test
public void readLeftover() throws IOException {
byte[] b = new byte[] {'a', 'b', (byte) 0xC2};
ByteArrayInputStream bais = new ByteArrayInputStream(b);
InputStreamReader r = new InputStreamReader(bais,
UTF_8.newDecoder().onMalformedInput(CodingErrorAction.IGNORE));
int n = r.read();
assertEquals((char)n, 'a');
char[] c = new char[3];
n = r.read(c, 0, 3);
assertEquals(n, 1);
assertEquals((char)c[0], 'b');
n = r.read();
assertEquals(n, -1);
}
}

View file

@ -0,0 +1,43 @@
/*
* Copyright (c) 2000, 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 4399447
* @summary Ensure that read offsets work properly
*/
import java.io.*;
public class ReadOffset {
public static void main(String[] args) throws Exception {
InputStream is
= new ByteArrayInputStream("foo bar".getBytes("US-ASCII"));
InputStreamReader isr = new InputStreamReader(is, "US-ASCII");
char[] cbuf = new char[100];
int n;
System.out.println(n = isr.read(cbuf, 0, 3));
System.out.println(isr.read(cbuf, n, cbuf.length - n));
}
}

View file

@ -0,0 +1,80 @@
/*
* Copyright (c) 2022, 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 8292043
* @run junit StatefulDecoderNearEOF
* @summary Check MalformedInputException is thrown with stateful decoders
* with malformed input before EOF
*/
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UncheckedIOException;
import java.nio.charset.CodingErrorAction;
import java.nio.charset.MalformedInputException;
import java.nio.charset.StandardCharsets;
import java.util.stream.IntStream;
import java.util.stream.Stream;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
public class StatefulDecoderNearEOF {
public static Stream<Arguments> inputs() {
return Stream.of(
// BOM, followed by High surrogate (in UTF-16LE).
// First read() should throw an exception.
Arguments.of(new byte[] {(byte)0xff, (byte)0xfe, 0, (byte)0xd8}, 0),
// BOM, followed by 'A', 'B', 'C', then by High surrogate (in UTF-16LE).
// Fourth read() should throw an exception.
Arguments.of(new byte[] {(byte)0xff, (byte)0xfe, (byte)0x41, 0, (byte)0x42, 0, (byte)0x43, 0, 0, (byte)0xd8}, 3));
}
@ParameterizedTest
@MethodSource("inputs")
public void testStatefulDecoderNearEOF(byte[] ba, int numSucessReads) throws IOException {
try (var r = new InputStreamReader(
new ByteArrayInputStream(ba),
StandardCharsets.UTF_16.newDecoder().onMalformedInput(CodingErrorAction.REPORT))) {
// Issue read() as many as numSucessReads which should not fail
IntStream.rangeClosed(1, numSucessReads).forEach(i -> {
try {
assertEquals(r.read(), (int)ba[i * 2]);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
});
// Final dangling high surrogate should throw an exception
assertThrows(MalformedInputException.class, () -> r.read());
}
}
}