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
95
test/jdk/java/nio/charset/CharsetEncoder/CanEncode.java
Normal file
95
test/jdk/java/nio/charset/CharsetEncoder/CanEncode.java
Normal file
|
|
@ -0,0 +1,95 @@
|
|||
/*
|
||||
* Copyright (c) 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 4821213
|
||||
* @summary Unit test for CharsetEncoder.canEncode methods
|
||||
*/
|
||||
|
||||
import java.io.*;
|
||||
import java.nio.*;
|
||||
import java.nio.charset.*;
|
||||
|
||||
|
||||
public class CanEncode {
|
||||
|
||||
private static int errors = 0;
|
||||
private static PrintStream out = System.err;
|
||||
|
||||
private static void wrong(CharsetEncoder ce, boolean can, String what) {
|
||||
out.println(ce.charset().name()
|
||||
+ ": Wrong answer for " + what
|
||||
+ ": " + !can);
|
||||
errors++;
|
||||
}
|
||||
|
||||
private static void ck(CharsetEncoder ce, char c, boolean can)
|
||||
throws Exception
|
||||
{
|
||||
if (ce.canEncode(c) != can)
|
||||
wrong(ce, can,
|
||||
("'" + c + "' (0x"
|
||||
+ Integer.toHexString(c & 0xffff) + ")"));
|
||||
}
|
||||
|
||||
private static void ck(CharsetEncoder ce, String s, boolean can)
|
||||
throws Exception
|
||||
{
|
||||
if (ce.canEncode(CharBuffer.wrap(s.toCharArray())) != can)
|
||||
wrong(ce, can, "array \"" + s + "\"");
|
||||
if (ce.canEncode(CharBuffer.wrap(s)) != can)
|
||||
wrong(ce, can, "buffer \"" + s + "\"");
|
||||
}
|
||||
|
||||
private static void test(String csn) throws Exception {
|
||||
|
||||
Charset cs = Charset.forName(csn);
|
||||
CharsetEncoder ce = cs.newEncoder();
|
||||
|
||||
if (cs.name().equals("US-ASCII")) {
|
||||
ck(ce, 'x', true);
|
||||
ck(ce, '\u00B6', false);
|
||||
ck(ce, "x", true);
|
||||
ck(ce, "\u00B6", false);
|
||||
ck(ce, "xyzzy", true);
|
||||
ck(ce, "xy\u00B6", false);
|
||||
}
|
||||
|
||||
// Unpaired surrogates should never be encodable
|
||||
ck(ce, '\ud800', false);
|
||||
ck(ce, '\ud801', false);
|
||||
ck(ce, '\udffe', false);
|
||||
ck(ce, '\udfff', false);
|
||||
ck(ce, "\ud800", false);
|
||||
ck(ce, "\ud801", false);
|
||||
ck(ce, "\udffe", false);
|
||||
ck(ce, "\udfff", false);
|
||||
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
test("US-ASCII");
|
||||
test("UTF-8");
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,51 @@
|
|||
/*
|
||||
* Copyright (c) 2020, 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 8253832
|
||||
* @run junit CoderMalfunctionErrorTest
|
||||
* @summary Check CoderMalfunctionError is thrown for any RuntimeException
|
||||
* on CharsetEncoder.encodeLoop() invocation.
|
||||
*/
|
||||
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.CharBuffer;
|
||||
import java.nio.charset.*;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
public class CoderMalfunctionErrorTest {
|
||||
@Test
|
||||
public void testEncodeLoop() {
|
||||
assertThrows(CoderMalfunctionError.class,
|
||||
() -> new CharsetEncoder(StandardCharsets.US_ASCII, 1, 1) {
|
||||
@Override
|
||||
protected CoderResult encodeLoop(CharBuffer charBuffer, ByteBuffer byteBuffer) {
|
||||
throw new RuntimeException("This exception should be wrapped in CoderMalfunctionError");
|
||||
}
|
||||
}.encode(null, null, true));
|
||||
}
|
||||
}
|
||||
114
test/jdk/java/nio/charset/CharsetEncoder/Flush.java
Normal file
114
test/jdk/java/nio/charset/CharsetEncoder/Flush.java
Normal file
|
|
@ -0,0 +1,114 @@
|
|||
/*
|
||||
* Copyright (c) 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 6227608
|
||||
* @summary Test proper handling of flush()
|
||||
* @modules jdk.charsets
|
||||
* @author Martin Buchholz
|
||||
*/
|
||||
|
||||
import java.util.*;
|
||||
import java.io.*;
|
||||
import java.nio.*;
|
||||
import java.nio.charset.*;
|
||||
|
||||
public class Flush {
|
||||
private static byte[] contents(ByteBuffer bb) {
|
||||
byte[] contents = new byte[bb.position()];
|
||||
bb.duplicate().flip().get(contents);
|
||||
return contents;
|
||||
}
|
||||
|
||||
private static ByteBuffer extend(ByteBuffer bb) {
|
||||
ByteBuffer x = ByteBuffer.allocate(2*bb.capacity()+10);
|
||||
bb.flip();
|
||||
x.put(bb);
|
||||
return x;
|
||||
}
|
||||
|
||||
private static void realMain(String[] args) throws Throwable {
|
||||
// A japanese character should decode as a 3-byte
|
||||
// switch-to-japanese escape sequence, followed by a 2-byte
|
||||
// encoding of the char itself, followed by a 3-byte return to
|
||||
// ASCII escape sequence.
|
||||
char[] jis0208 = {'\u3001'};
|
||||
CharBuffer cb = CharBuffer.wrap(jis0208);
|
||||
ByteBuffer bb = ByteBuffer.allocate(6);
|
||||
CharsetEncoder enc = Charset.forName("ISO-2022-JP").newEncoder();
|
||||
|
||||
check(enc.encode(cb, bb, true).isUnderflow());
|
||||
|
||||
System.out.println(Arrays.toString(contents(bb)));
|
||||
check(! cb.hasRemaining());
|
||||
equal(contents(bb).length, 3 + 2);
|
||||
equal(bb.get(0), (byte)0x1b);
|
||||
|
||||
//----------------------------------------------------------------
|
||||
// We must be able to recover if flush() returns OVERFLOW
|
||||
//----------------------------------------------------------------
|
||||
check(enc.flush(bb).isOverflow());
|
||||
check(enc.flush(bb).isOverflow());
|
||||
equal(contents(bb).length, 3 + 2);
|
||||
|
||||
bb = extend(bb);
|
||||
|
||||
check(enc.flush(bb).isUnderflow());
|
||||
equal(bb.get(3 + 2), (byte)0x1b);
|
||||
System.out.println(Arrays.toString(contents(bb)));
|
||||
equal(contents(bb).length, 3 + 2 + 3);
|
||||
|
||||
//----------------------------------------------------------------
|
||||
// A final redundant flush() is a no-op
|
||||
//----------------------------------------------------------------
|
||||
check(enc.flush(bb).isUnderflow());
|
||||
check(enc.flush(bb).isUnderflow());
|
||||
equal(contents(bb).length, 3 + 2 + 3);
|
||||
|
||||
//----------------------------------------------------------------
|
||||
// CharsetEncoder.encode(ByteBuffer) must call flush(ByteBuffer)
|
||||
//----------------------------------------------------------------
|
||||
bb = enc.encode(CharBuffer.wrap(jis0208));
|
||||
byte[] expected = "\u001b$B!\"\u001b(B".getBytes("ASCII");
|
||||
byte[] contents = new byte[bb.limit()]; bb.get(contents);
|
||||
check(Arrays.equals(contents, expected));
|
||||
}
|
||||
|
||||
//--------------------- Infrastructure ---------------------------
|
||||
static volatile int passed = 0, failed = 0;
|
||||
static void pass() { passed++; }
|
||||
static void fail() { failed++; Thread.dumpStack(); }
|
||||
static void fail(String msg) { System.out.println(msg); fail(); }
|
||||
static void unexpected(Throwable t) { failed++; t.printStackTrace(); }
|
||||
static void check(boolean cond) { if (cond) pass(); else fail(); }
|
||||
static void equal(Object x, Object y) {
|
||||
if (x == null ? y == null : x.equals(y)) pass();
|
||||
else {System.out.println(x + " not equal to " + y); fail(); }}
|
||||
|
||||
public static void main(String[] args) throws Throwable {
|
||||
try { realMain(args); } catch (Throwable t) { unexpected(t); }
|
||||
|
||||
System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
|
||||
if (failed > 0) throw new Exception("Some tests failed");
|
||||
}
|
||||
}
|
||||
69
test/jdk/java/nio/charset/CharsetEncoder/NaNinCtor.java
Normal file
69
test/jdk/java/nio/charset/CharsetEncoder/NaNinCtor.java
Normal file
|
|
@ -0,0 +1,69 @@
|
|||
/*
|
||||
* Copyright (c) 2018, 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 8210285
|
||||
* @summary NaN arguments to the constructor should be rejected
|
||||
*/
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.CharBuffer;
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.charset.CharsetEncoder;
|
||||
import java.nio.charset.CoderResult;
|
||||
|
||||
public class NaNinCtor {
|
||||
|
||||
public static void main(String[] args) throws Throwable {
|
||||
Charset ascii = Charset.forName("US-ASCII");
|
||||
|
||||
// sanity check
|
||||
new MyEncoder(ascii, 0.5f, 1.5f);
|
||||
|
||||
// various combinations of invalid arguments
|
||||
test(() -> new MyEncoder(ascii, 0.0f, 1.0f));
|
||||
test(() -> new MyEncoder(ascii, 1.0f, 0.0f));
|
||||
test(() -> new MyEncoder(ascii, -1.0f, 1.0f));
|
||||
test(() -> new MyEncoder(ascii, 1.0f, -1.0f));
|
||||
test(() -> new MyEncoder(ascii, Float.NaN, 1.0f));
|
||||
test(() -> new MyEncoder(ascii, 1.0f, Float.NaN));
|
||||
test(() -> new MyEncoder(ascii, 1.5f, 0.5f));
|
||||
}
|
||||
|
||||
static void test(Runnable r) {
|
||||
try {
|
||||
r.run();
|
||||
throw new RuntimeException("IllegalArgumentException not thrown");
|
||||
} catch (IllegalArgumentException expected) {
|
||||
}
|
||||
}
|
||||
|
||||
static class MyEncoder extends CharsetEncoder {
|
||||
public MyEncoder(Charset cs, float avg, float max) {
|
||||
super(cs, avg, max);
|
||||
}
|
||||
protected CoderResult encodeLoop(CharBuffer in, ByteBuffer out) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue