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,93 @@
/*
* 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.
*/
/**/
import java.io.*;
public class ABCInputStream extends InputStream {
int len;
int chunk;
int count = 0;
char next = firstChar();
ABCInputStream(int len) {
this(len, len);
}
ABCInputStream(int len, int chunk) {
this.len = len;
this.chunk = chunk;
}
static char firstChar() {
return 'a';
}
static char nextChar(char c) {
if (c == 'z')
return '0';
else if (c == '9')
return 'a';
else
return (char)(c + 1);
}
public int read() {
if (count >= len)
return -1;
char c = next;
next = nextChar(c);
count++;
return (byte) c;
}
public int read(byte buf[], int off, int len) {
int n = (len > chunk) ? chunk : len;
for (int i = off; i < off + n; i++) {
int c = read();
if (c == -1) {
if (i > off)
return i - off;
else
return -1;
}
buf[i] = (byte) c;
}
return n;
}
public int available() {
int remaining = len - count;
return (remaining > chunk) ? chunk : remaining;
}
public void close() throws IOException {
if (len == 0)
throw new IOException("Already closed");
len = 0;
}
}

View file

@ -0,0 +1,63 @@
/*
* Copyright (c) 1997, 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.
*/
/**/
import java.io.*;
public class ABCOutputStream extends OutputStream {
int len;
int count = 0;
char next = ABCInputStream.firstChar();
ABCOutputStream(int len) {
this.len = len;
}
public void write(int c) throws IOException {
if (count >= len)
throw new IOException("Wrote too many characters");
if (c != next)
throw new IOException("Wrong character written: "
+ "Expected '" + next + "', "
+ "got '" + (char)c + "'");
next = ABCInputStream.nextChar(next);
count++;
}
public void write(byte buf[], int off, int len) throws IOException {
for (int i = off; i < off + len; i++)
write(buf[i]);
}
public void close() throws IOException {
if (len == 0)
throw new IOException("Already closed");
else if (count < len)
throw new IOException("Wrote too few characters");
len = 0;
}
}

View file

@ -0,0 +1,71 @@
/*
* 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.
*/
/**/
import java.io.Reader;
class ABCReader extends Reader {
int len;
int count = 0;
char next = 'a';
ABCReader(int len) {
this.len = len;
}
public int read() {
if (count >= len)
return -1;
char c = next;
if (next == 'z')
next = '0';
else if (next == '9')
next = 'a';
else
next++;
count++;
return c;
}
public int read(char cbuf[], int off, int len) {
for (int i = off; i < off + len; i++) {
int c = read();
if (c == -1) {
if (i > off)
return i - off;
else
return -1;
}
cbuf[i] = (char) c;
}
return len;
}
public void close() {
len = 0;
}
}

View file

@ -0,0 +1,51 @@
/*
* 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
@summary Simple stream-copy test for BufferedReader and BufferedWriter
*/
import java.io.*;
public class BCat {
public static int count = 10000;
public static int chunk = 512;
public static void main(String[] args) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(new ABCInputStream(count, chunk)),
1025);
Writer out = new BufferedWriter(new OutputStreamWriter(new ABCOutputStream(count)));
char[] buf = new char[119];
int n;
while ((n = in.read(buf)) != -1) {
out.write(buf, 0, n);
System.err.print(" " + n);
}
in.close();
out.close();
System.err.println();
}
}

View file

@ -0,0 +1,76 @@
/*
* 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
@summary Test interaction of buffer sizes in buffered char and byte streams
*/
import java.io.*;
public class BufferSizes {
static int min = 90;
static int max = 110;
static int chunk = 100;
static int count = 1000;
static void runBytes() throws IOException {
for (int sz = min; sz <= max; sz++) {
System.err.println(sz);
InputStream in
= new BufferedInputStream(new ABCInputStream(count, chunk), sz);
OutputStream out
= new BufferedOutputStream(new ABCOutputStream(count), sz);
int n;
byte[] buf = new byte[sz];
while ((n = in.read(buf, 0, sz)) != -1)
out.write(buf, 0, n);
in.close();
out.close();
}
}
static void runChars() throws IOException {
for (int sz = min; sz <= max; sz++) {
System.err.println(sz);
Reader in
= new BufferedReader(new InputStreamReader(new ABCInputStream(count, chunk)), sz);
Writer out
= new BufferedWriter(new OutputStreamWriter(new ABCOutputStream(count)), sz);
int n;
char[] cbuf = new char[sz];
while ((n = in.read(cbuf, 0, sz)) != -1)
out.write(cbuf, 0, n);
in.close();
out.close();
}
}
public static void main(String[] args) throws IOException {
runBytes();
runChars();
}
}

View file

@ -0,0 +1,135 @@
/*
* 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
@summary General tests for BufferedReader.mark
*/
import java.io.*;
public class BufferedReaderMark {
static int bufferSize = -1;
static int inputSize = -1;
static String d2(int n) {
String s = Integer.toString(n);
if (s.length() < 2)
return s + " ";
else
return s;
}
static void fail(String s) {
throw new RuntimeException(s);
}
public static void main(String[] args) throws Exception {
PrintStream log = System.err;
if (bufferSize == -1)
bufferSize = 7;
if (inputSize == -1)
inputSize = bufferSize * 3;
try {
Reader in = new BufferedReader(new ABCReader(inputSize), bufferSize);
char buf[] = new char[bufferSize];
if (in.read(buf) != bufferSize)
fail("Read failed");
in.reset();
fail("reset() didn't throw");
}
catch (IOException x) {
if (x.getMessage().equals("Stream not marked"))
log.println("Unmarked-stream test OK");
else
fail(x.toString());
}
boolean err = false;
try {
Reader in = new BufferedReader(new ABCReader(inputSize), bufferSize);
char buf[] = new char[bufferSize];
if (in.read() == -1)
fail("Read 1 failed");
in.mark(bufferSize);
while (in.read() != -1);
in.reset();
fail("reset() didn't throw");
}
catch (IOException x) {
if (x.getMessage().equals("Mark invalid")) {
err = true;
log.println("Invalid-mark test OK");
}
else {
log.println("Invalid-mark test failed: " + x);
fail(x.toString());
}
}
if (! err)
fail("Invalid-mark test failed: Exception not thrown");
int c;
for (int off = 0; off < bufferSize * 2; off++) {
for (int ra = 0; ra <= inputSize + 2; ra++) {
Reader in = new BufferedReader(new ABCReader(inputSize), bufferSize);
log.print(d2(off) + ", " + d2(ra) + " mark: ");
for (int i = 0; i < off; i++) {
if ((c = in.read()) == -1) {
log.print("<EOF>");
break;
}
log.print((char) c);
}
in.mark(ra);
for (int i = 0; i < ra; i++) {
if ((c = in.read()) == -1) {
log.print("<EOF>");
break;
}
log.print((char) c);
}
log.println();
log.print(d2(off) + ", " + d2(ra) + " reset: ");
in.reset();
for (int i = 0; i < off; i++)
log.print(' ');
for (;;) {
if ((c = in.read()) == -1) {
log.print("<EOF>");
break;
}
log.print((char) c);
}
log.println();
}
}
}
}

View file

@ -0,0 +1,48 @@
/*
* 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
@summary Simple stream-copy test for InputStreamReader & OutputStreamWriter
*/
import java.io.*;
public class Cat {
public static int count = 10000;
public static void main(String[] args) throws IOException {
Reader in = new InputStreamReader(new ABCInputStream(count));
Writer out = new OutputStreamWriter(new ABCOutputStream(count));
int c;
while ((c = in.read()) != -1) {
out.write(c);
System.err.print((char)c);
}
in.close();
out.close();
System.err.println();
}
}

View file

@ -0,0 +1,53 @@
/*
* 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.
*/
/**/
import java.io.*;
public class CharGenerator {
private IntGenerator r;
int min = 0, max = 0xffff;
public CharGenerator(IntGenerator r) {
this.r = r;
}
public CharGenerator(IntGenerator r, int min, int max) {
this.r = r;
this.min = min;
this.max = max;
}
public char next() {
char c;
do
c = (char) (r.next(max - min) + min);
while ((c == '\r') || (c == '\n')
|| ((c >= 0xd800) && (c <= 0xdfff))
|| (c == 0xfffe));
return c;
}
}

View file

@ -0,0 +1,46 @@
/*
* 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.
*/
/**/
import java.util.Random;
public class IntGenerator {
Random r;
public IntGenerator() {
r = new Random(0);
}
public int next(int max) {
if (max <= 0)
return 0;
int x = r.nextInt();
if (x < 0)
x = 0 - x;
return x % (max + 1);
}
}

View file

@ -0,0 +1,84 @@
/*
* 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.
*/
/**/
public class LineGenerator {
IntGenerator ig;
StringGenerator sg;
int limit;
public LineGenerator(IntGenerator ig, StringGenerator sg, int limit) {
this.ig = ig;
this.sg = sg;
this.limit = limit;
}
public LineGenerator(IntGenerator ig) {
this.ig = ig;
this.sg = new StringGenerator(ig);
this.limit = -1;
}
private char prevTerminator = 0;
private int count = 0;
public String lineTerminator;
public String next() {
if ((count >= limit) && (limit >= 0))
return null;
String l = sg.next();
/* Avoid "\r\n" sequences
in which the '\n' terminates a blank line */
int len = l.length();
int t;
do
t = ig.next(2);
while ((prevTerminator == '\r') && (len == 0) && (t == 0));
String ts;
switch (t) {
case 0:
ts = "\n";
prevTerminator = '\n';
break;
case 1:
ts = "\r";
prevTerminator = '\r';
break;
case 2:
default:
ts = "\r\n";
prevTerminator = '\n';
break;
}
count++;
lineTerminator = ts;
return l;
}
}

View file

@ -0,0 +1,62 @@
/*
* 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
@summary Check line lengths using all recognized line-termination sequences
*/
import java.io.*;
public class LineLengths {
static String enc = "UTF8";
static String inEnc = null;
static String outEnc = null;
public static void main(String[] args) throws Exception {
PrintWriter log
= new PrintWriter(new OutputStreamWriter(System.err), true);
if (inEnc == null)
inEnc = enc;
if (outEnc == null)
outEnc = enc;
PipedOutputStream uo = new PipedOutputStream();
PipedInputStream ui = new PipedInputStream(uo);
PipedOutputStream co = new PipedOutputStream();
PipedInputStream ci = new PipedInputStream(co);
BufferedWriter w = new BufferedWriter(new OutputStreamWriter(co, outEnc));
BufferedReader r = new BufferedReader(new InputStreamReader(ci, inEnc));
Thread t1 = new Thread(new LineLengthsSource(uo, w, log));
Thread t2 = new Thread(new LineSink(ui, r, log));
t1.start();
t2.start();
t1.join();
t2.join();
}
}

View file

@ -0,0 +1,98 @@
/*
* 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.
*/
/**/
import java.io.*;
class LineLengthsSource implements Runnable {
DataOutputStream uo;
BufferedWriter to;
PrintWriter log;
public LineLengthsSource(OutputStream us, BufferedWriter ts,
PrintWriter log)
throws IOException
{
uo = new DataOutputStream(us);
to = ts;
this.log = log;
}
private void flush() throws IOException {
uo.flush();
Thread.currentThread().yield();
to.flush();
Thread.currentThread().yield();
}
private String termString(int t) {
switch (t) {
case 0: return "\n";
case 1: return "\r";
case 2: return "\r\n";
default: return "";
}
}
private String termName(int t) {
switch (t) {
case 0: return "\\n";
case 1: return "\\r";
case 2: return "\\r\\n";
default: return "";
}
}
private void go(int t) throws IOException {
for (int ln = 0; ln < 128; ln++) {
String ts = termString(t);
StringBuffer s = new StringBuffer(ln + ts.length());
for (int i = 0; i < ln; i++)
s.append('x');
log.println("[" + ln + "]" + termName(t));
uo.writeUTF(s.toString());
s.append(ts);
to.write(s.toString());
flush();
}
}
public void run() {
try {
go(0);
go(1);
go(2);
uo.close();
Thread.currentThread().yield();
to.close();
Thread.currentThread().yield();
}
catch (IOException x) {
return; /* Probably pipe broken */
}
}
}

View file

@ -0,0 +1,100 @@
/*
* 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
@summary Stochastic test of LineNumberReader
*/
import java.io.*;
public class LineNumbers {
static String enc = "UTF8";
static String inEnc;
static String outEnc;
static int limit = 500;
public static void main(String[] args) throws Exception {
PrintWriter log
= new PrintWriter(new OutputStreamWriter(System.err), true);
if (inEnc == null)
inEnc = enc;
if (outEnc == null)
outEnc = enc;
PipedOutputStream co = new PipedOutputStream();
PipedInputStream ci = new PipedInputStream(co);
BufferedWriter w = new BufferedWriter(new OutputStreamWriter(co, outEnc));
LineNumberReader r = new LineNumberReader(new InputStreamReader(ci, inEnc));
Thread t1 = new Thread(new RandomLineSource(w, limit));
Thread t2 = new Thread(new LineNumberSink(r, limit, log));
t1.start();
t2.start();
t1.join();
t2.join();
}
}
class LineNumberSink implements Runnable {
LineNumberReader r;
int limit;
PrintWriter log;
LineNumberSink(LineNumberReader r, int limit, PrintWriter log) {
this.r = r;
this.limit = limit;
this.log = log;
}
public void run() {
String s;
int n = 0;
try {
while ((s = r.readLine()) != null) {
n++;
int ln = r.getLineNumber();
log.println("[" + ln + "] " + s.length());
log.println(s);
if (log.checkError())
log.println("Conversion errors"); /* #### */
if (n != ln)
throw new RuntimeException("Line number mismatch: Expected " + n + ", got " + ln);
}
if (n != limit)
throw new RuntimeException("Incorrect line count");
}
catch (IOException x) {
throw new RuntimeException(x.toString());
}
log.println(n + " lines read");
}
}

View file

@ -0,0 +1,111 @@
/*
* Copyright (c) 1997, 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.
*/
/**/
import java.io.*;
class LineSink implements Runnable {
DataInputStream ui;
BufferedReader ti;
int count;
PrintWriter log;
public LineSink(InputStream us, BufferedReader ts,
int count, PrintWriter log)
throws IOException
{
this(us, ts, log);
this.count = count;
}
public LineSink(InputStream us, BufferedReader ts, PrintWriter log)
throws IOException
{
ui = new DataInputStream(us);
ti = ts;
this.count = Integer.MAX_VALUE;
this.log = log;
}
private String readUTFLine() throws IOException {
String s;
try {
s = ui.readUTF();
}
catch (EOFException x) {
return null;
}
return s;
}
public void run() {
try {
for (int ln = 0; ln < count; ln++) {
String us = readUTFLine();
if (us == null) {
if (count < Integer.MAX_VALUE)
throw new RuntimeException("Premature EOF on UTF stream");
log.println("EOF on UTF stream");
break;
}
String ts = ti.readLine();
if (ts == null) {
if (count < Integer.MAX_VALUE)
throw new RuntimeException("Premature EOF on char stream");
log.println("EOF on char stream");
break;
}
if (us.length() != ts.length()) {
log.println("Length mismatch: us = \""
+ us + "\", ts = \""
+ ts + "\"");
throw new RuntimeException("Line " + ln +
": Length mismatch: " +
us.length() + " " + ts.length());
}
for (int i = 0; i < us.length(); i++) {
if (us.charAt(i) != ts.charAt(i))
throw new RuntimeException("Line " + ln +
": Char mismatch: [" + i + "] " +
Integer.toHexString(us.charAt(i)) +
" " + Integer.toHexString(ts.charAt(i)));
}
log.println(ln + " " + ts.length());
}
if (readUTFLine() != null)
throw new RuntimeException("Expected EOF on UTF stream");
if (ti.readLine() != null)
throw new RuntimeException("Expected EOF on char stream");
} catch (IOException x) {
throw new RuntimeException("Unexpected IOException: " + x);
}
}
}

View file

@ -0,0 +1,90 @@
/*
* 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.
*/
/**/
import java.io.*;
import java.util.Random;
class RandomLineSource implements Runnable {
DataOutputStream uo;
BufferedWriter to;
LineGenerator lg;
PrintWriter log;
public RandomLineSource(OutputStream us, BufferedWriter ts, int limit,
PrintWriter log)
{
if (us != null)
uo = new DataOutputStream(us);
to = ts;
IntGenerator ig = new IntGenerator();
lg = new LineGenerator(ig, new StringGenerator(ig), limit);
this.log = log;
}
public RandomLineSource(OutputStream us, BufferedWriter ts, int limit) {
this(us, ts, limit, null);
}
public RandomLineSource(BufferedWriter ts, int limit) {
this(null, ts, limit);
}
private void flush() throws IOException {
if (uo != null) {
uo.flush();
Thread.currentThread().yield();
}
to.flush();
for (int i = 0; i < 10; i++)
Thread.currentThread().yield();
}
private int count = 0;
public void run() {
try {
String s;
while ((s = lg.next()) != null) {
if (uo != null)
uo.writeUTF(s);
to.write(s + lg.lineTerminator);
flush();
count++;
}
if (uo != null) {
uo.close();
Thread.currentThread().yield();
}
to.close();
Thread.currentThread().yield();
} catch (IOException x) {
return; /* Probably pipe broken */
}
}
}

View file

@ -0,0 +1,63 @@
/*
* 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
@summary Stochastic tests for InputStreamReader & OutputStreamWriter
*/
import java.io.*;
public class RandomLines {
static String enc = "UTF8";
static String inEnc;
static String outEnc;
static int count = 500;
public static void main(String[] args) throws Exception {
PrintWriter log
= new PrintWriter(new OutputStreamWriter(System.err), true);
if (inEnc == null)
inEnc = enc;
if (outEnc == null)
outEnc = enc;
PipedOutputStream uo = new PipedOutputStream();
PipedInputStream ui = new PipedInputStream(uo);
PipedOutputStream co = new PipedOutputStream();
PipedInputStream ci = new PipedInputStream(co);
BufferedWriter w = new BufferedWriter(new OutputStreamWriter(co, outEnc));
BufferedReader r = new BufferedReader(new InputStreamReader(ci, inEnc));
Thread t1 = new Thread(new RandomLineSource(uo, w, count, log));
Thread t2 = new Thread(new LineSink(ui, r, count, log));
t1.start();
t2.start();
t1.join();
t2.join();
}
}

View file

@ -0,0 +1,76 @@
/*
* 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
@summary Check that BufferedReader/Writer correctly handle small reads
*/
import java.io.*;
public class SmallReads {
static class OneByteInputStream extends FilterInputStream {
OneByteInputStream(InputStream in) {
super(in);
}
public int read(byte[] b, int off, int len) throws IOException {
return in.read(b, off, 1);
}
}
static String enc = "UTF8";
static String inEnc;
static String outEnc;
static int count = 100;
public static void main(String[] args) throws Exception {
PrintWriter log
= new PrintWriter(new OutputStreamWriter(System.err), true);
if (inEnc == null)
inEnc = enc;
if (outEnc == null)
outEnc = enc;
PipedOutputStream uo = new PipedOutputStream();
PipedInputStream ui = new PipedInputStream(uo);
PipedOutputStream co = new PipedOutputStream();
PipedInputStream ci = new PipedInputStream(co);
InputStream ci2 = new OneByteInputStream(ci);
BufferedWriter w = new BufferedWriter(new OutputStreamWriter(co, outEnc));
BufferedReader r = new BufferedReader(new InputStreamReader(ci2, inEnc));
Thread t1 = new Thread(new RandomLineSource(uo, w, count, log));
Thread t2 = new Thread(new LineSink(ui, r, count, log));
t1.start();
t2.start();
t1.join();
t2.join();
}
}

View file

@ -0,0 +1,150 @@
/*
* Copyright (c) 1997, 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
@summary General tests of String constructors and methods that convert
between character encodings. This should really be in
java/lang/String, but it shares code with the I/O charStream
tests.
*/
import java.io.*;
public class StringConvert {
static String enc = "UTF8";
static int limit = 500;
static int max = 0xffff;
static void fail(String s) {
throw new RuntimeException(s);
}
public static void main(String[] args) throws Exception {
PrintStream log = System.err;
IntGenerator ig = new IntGenerator();
CharGenerator cg;
StringGenerator sg;
String s;
int i = 0;
/* String(byte[] bytes, String enc)
getBytes(String enc)
*/
log.println("-- String(byte[], String), getBytes(String)");
i = 0;
cg = new CharGenerator(ig, 0, max);
sg = new StringGenerator(ig, cg, limit);
while ((s = sg.next()) != null) {
byte[] b = s.getBytes(enc);
String t = new String(b, enc);
if (!s.equals(t)) {
int n = Math.min(s.length(), t.length());
for (int j = 0; j < n; j++) {
if (s.charAt(j) != t.charAt(j)) {
log.println("Mismatch: " + j + " "
+ Integer.toHexString(s.charAt(j))
+ " != "
+ Integer.toHexString(t.charAt(j)));
}
}
fail("Conversion failure");
}
log.println("[" + i + "] " + s.length());
i++;
}
/* String(byte[] bytes)
getBytes()
*/
log.println("-- String(byte[]), getBytes()");
i = 0;
cg = new CharGenerator(ig, 0x20, 0x7e);
sg = new StringGenerator(ig, cg, limit);
while ((s = sg.next()) != null) {
log.println("[" + i + "] \"" + s + "\"");
byte[] b = s.getBytes();
String t = new String(b);
if (! s.equals(t))
fail("Conversion failure");
i++;
}
/* String(byte[] bytes, int offset, int length)
getBytes()
*/
log.println("-- String(byte[], int, int), getBytes()");
i = 0;
cg = new CharGenerator(ig, 0x20, 0x7e);
sg = new StringGenerator(ig, cg, limit);
while ((s = sg.next()) != null) {
log.println("[" + i + "] \"" + s + "\"");
byte[] b = s.getBytes();
int o = ig.next(s.length() - 1);
int n = ig.next(s.length() - o);
String t = new String(b, o, n);
if (! s.substring(o, o + n).equals(t))
fail("Conversion failure");
i++;
}
/* String(byte[] bytes, int offset, int length, String enc)
getBytes(String enc)
*/
log.println("-- String(byte[], int, int, String), getBytes(String)");
i = 0;
cg = new CharGenerator(ig);
sg = new StringGenerator(ig, cg, limit);
while ((s = sg.next()) != null) {
log.println("[" + i + "] " + s.length());
byte[] b = s.getBytes(enc);
int o = ig.next(100);
byte[] b2 = new byte[b.length + o];
System.arraycopy(b, 0, b2, o, b.length);
String t = new String(b2, o, b.length, enc);
if (! s.equals(t))
fail("Conversion failure");
i++;
}
/* Substrings */
log.println("-- Substrings");
i = 0;
cg = new CharGenerator(ig, 0x20, 0x7e);
sg = new StringGenerator(ig, cg, limit);
while ((s = sg.next()) != null) {
log.println("[" + i + "] \"" + s + "\"");
int o = ig.next(s.length() - 1);
int n = ig.next(s.length() - o);
String s2 = s.substring(o, o + n);
byte[] b = s2.getBytes();
String t = new String(b);
if (! s2.equals(t))
fail("Conversion failure");
i++;
}
}
}

View file

@ -0,0 +1,73 @@
/*
* 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.
*/
/**/
public class StringGenerator {
private IntGenerator ig;
private CharGenerator cg;
private int limit;
private int count = 0;
public StringGenerator(IntGenerator ig, CharGenerator cg, int limit) {
this.ig = ig;
this.cg = cg;
this.limit = limit;
}
public StringGenerator(IntGenerator ig, CharGenerator cg) {
this(ig, cg, -1);
}
public StringGenerator(IntGenerator ig, int limit) {
this(ig, new CharGenerator(ig), limit);
}
public StringGenerator(IntGenerator ig) {
this(ig, -1);
}
public StringGenerator(int limit) {
this(new IntGenerator(), limit);
}
public StringGenerator() {
this(new IntGenerator());
}
public String next() {
if ((count >= limit) && (limit >= 0))
return null;
int len = ig.next(80);
StringBuffer s = new StringBuffer(len);
for (int i = 0; i < len; i++)
s.append(cg.next());
count++;
return s.toString();
}
}

View file

@ -0,0 +1,49 @@
/*
* Copyright (c) 2002, 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 4657074
* @summary Ensure that stream decoders work properly for UTF-16 charsets
* @run main/timeout=10 UTF16
*/
import java.io.*;
public class UTF16 {
private static byte[] array = new byte[20000];
private static void go(String cs) throws Exception {
InputStream is = new ByteArrayInputStream(array);
Reader r = new InputStreamReader(is, "UTF-16LE");
r.read();
}
public static void main(String[] args) throws Exception {
go("UTF-16");
go("UTF-16LE");
go("UTF-16BE");
}
}

View file

@ -0,0 +1,61 @@
/*
* 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 4059684
@summary Simple heartbeat test of the UTF8 byte->char converter
*/
import java.io.*;
public class UTF8 {
static String test
= "This is a simple\ntest of the UTF8\r\nbyte-to-char and char-to-byte\nconverters.";
public static void main(String[] args) throws IOException {
ByteArrayOutputStream bo = new ByteArrayOutputStream();
Writer out = new OutputStreamWriter(bo, "UTF8");
out.write(test);
out.close();
Reader in
= new InputStreamReader(new ByteArrayInputStream(bo.toByteArray()),
"UTF8");
StringBuffer sb = new StringBuffer();
char buf[] = new char[1000];
int n;
while ((n = in.read(buf, 0, buf.length)) >= 0) {
sb.append(buf, 0, n);
System.err.println(n);
}
if (! sb.toString().equals(test)) {
System.err.println("In: [" + test + "]");
System.err.println("Out: [" + sb.toString() + "]");
throw new RuntimeException("Output does not match input");
}
}
}

View file

@ -0,0 +1,59 @@
/*
* 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 4429227
* @summary Ensure that OutputStreamWriter works on whole multiples
* of its internal buffer size
*/
import java.io.*;
public class WriteLengths {
static PrintStream log = System.err;
static int failures = 0;
static ByteArrayOutputStream bos = new ByteArrayOutputStream(1 << 15);
static void go(int len, String enc) throws Exception {
bos.reset();
OutputStreamWriter osw = new OutputStreamWriter(bos, enc);
char[] cs = new char[len];
osw.write(cs);
osw.close();
byte[] ba = bos.toByteArray();
if (ba.length != len) {
log.println("FAIL: Wrote " + len + ", got " + ba.length
+ "; enc = " + enc);
failures++;
}
}
public static void main(String[] args) throws Exception {
for (int i = 0; i < (1 << 15); i += 1024)
go(i, "us-ascii");
}
}