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,92 @@
/*
* @test /nodynamiccopyright/
* @bug 4209652 4363318
* @summary Basic test for chained exceptions & Exception.getStackTrace().
* @author Josh Bloch
*/
public class ChainedExceptions {
public static void main(String args[]) {
try {
a();
} catch(HighLevelException e) {
StackTraceElement[] highTrace = e.getStackTrace();
int depthTrim = highTrace.length - 2;
check(e, highTrace[0], "a", 48);
check(e, highTrace[1], "main", 11);
Throwable mid = e.getCause();
StackTraceElement[] midTrace = mid.getStackTrace();
if (midTrace.length - depthTrim != 4)
throw new RuntimeException("Mid depth");
check(mid, midTrace[0], "c", 58);
check(mid, midTrace[1], "b", 52);
check(mid, midTrace[2], "a", 46);
check(mid, midTrace[3], "main", 11);
Throwable low = mid.getCause();
StackTraceElement[] lowTrace = low.getStackTrace();
if (lowTrace.length - depthTrim != 6)
throw new RuntimeException("Low depth");
check(low, lowTrace[0], "e", 65);
check(low, lowTrace[1], "d", 62);
check(low, lowTrace[2], "c", 56);
check(low, lowTrace[3], "b", 52);
check(low, lowTrace[4], "a", 46);
check(low, lowTrace[5], "main", 11);
if (low.getCause() != null)
throw new RuntimeException("Low cause != null");
}
}
static void a() throws HighLevelException {
try {
b();
} catch(MidLevelException e) {
throw new HighLevelException(e);
}
}
static void b() throws MidLevelException {
c();
}
static void c() throws MidLevelException {
try {
d();
} catch(LowLevelException e) {
throw new MidLevelException(e);
}
}
static void d() throws LowLevelException {
e();
}
static void e() throws LowLevelException {
throw new LowLevelException();
}
private static final String OUR_CLASS = ChainedExceptions.class.getName();
private static final String OUR_FILE_NAME = "ChainedExceptions.java";
private static void check(Throwable t, StackTraceElement e, String methodName, int n) {
if (!e.getClassName().equals(OUR_CLASS))
throw new RuntimeException("Class: " + e, t);
if (!e.getMethodName().equals(methodName))
throw new RuntimeException("Method name: " + e, t);
if (!e.getFileName().equals(OUR_FILE_NAME))
throw new RuntimeException("File name: " + e, t);
if (e.getLineNumber() != n)
throw new RuntimeException("Line number: " + e, t);
}
}
class HighLevelException extends Exception {
HighLevelException(Throwable cause) { super(cause); }
}
class MidLevelException extends Exception {
MidLevelException(Throwable cause) { super(cause); }
}
class LowLevelException extends Exception {
}

View file

@ -0,0 +1,318 @@
/*
* Copyright (c) 2000, 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.
*/
import java.io.*;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.UndeclaredThrowableException;
import java.security.PrivilegedActionException;
import java.util.Base64;
import java.util.Map;
/**
* @test
* @bug 4385429 8004928 8210721
* @summary Certain legacy chained exceptions throw IllegalArgumentException
* upon deserialization if "causative exception" is null.
* @author Josh Bloch
*/
public class LegacyChainedExceptionSerialization {
private static Throwable[] broken = {
new ClassNotFoundException(),
new ClassNotFoundException("bar", new IOException("reading class file")),
new ExceptionInInitializerError(),
new ExceptionInInitializerError(new NullPointerException("foo")),
new java.lang.reflect.UndeclaredThrowableException(null),
new java.lang.reflect.UndeclaredThrowableException(new IllegalArgumentException("foo")),
new java.lang.reflect.InvocationTargetException(null),
new java.lang.reflect.InvocationTargetException(new Error("goo")),
new java.security.PrivilegedActionException(null),
new java.security.PrivilegedActionException(new IOException("foo")),
};
public static void main(String[] args) throws Exception {
for (int i=0; i<broken.length; i++)
test(broken[i]);
for (Map.Entry<String, Throwable> e : SERIALIZED_DATA.entrySet()) {
Throwable t = deserialize(e.getKey());
verify(t, e.getValue());
}
testOverriddenGetCause();
}
private static Throwable test(Throwable e) throws Exception {
ByteArrayOutputStream bout = new ByteArrayOutputStream();
ObjectOutputStream out = new ObjectOutputStream(bout);
out.writeObject(e);
out.flush();
ByteArrayInputStream bin =
new ByteArrayInputStream(bout.toByteArray());
ObjectInputStream in = new ObjectInputStream(bin);
Throwable clone = (Throwable) in.readObject();
return clone;
}
private static void testOverriddenGetCause() throws Exception {
SubClass sc = new SubClass(new NullPointerException());
SubClass clone = (SubClass)test(sc);
Throwable cause = clone.getException();
if (!(cause instanceof NullPointerException) || cause.getMessage() != null) {
throw new RuntimeException("unexpected cause: " + cause);
}
}
private static Throwable deserialize(String ser) throws Exception {
Base64.Decoder decoder = Base64.getDecoder();
try (ByteArrayInputStream bin = new ByteArrayInputStream(decoder.decode(ser));
ObjectInputStream ois = new ObjectInputStream(bin)) {
return (Throwable)ois.readObject();
}
}
/*
* Verify the deserialization of the serialized data from an old version.
* See SERIALIZED_DATA.
*/
private static void verify(Throwable t, Throwable expected) {
String msg = expected.getMessage();
Throwable cause = expected.getCause();
if (t.getMessage() != msg && msg != null && !msg.equals(t.getMessage())) {
throw new RuntimeException("unexpected message: " + t.getMessage()
+ " expected: " + msg);
}
Throwable e = t.getCause();
if (e.getClass() != cause.getClass()) {
throw new RuntimeException("unexpected cause: " + t.getCause());
}
String causedBy = cause.getMessage();
if (e.getMessage() != causedBy) {
if (e.getMessage() == null || causedBy == null || !causedBy.equals(e.getMessage())) {
throw new RuntimeException("unexpected message: " + t.getMessage()
+ " expected: " + causedBy);
}
}
Throwable exception = null;
if (t instanceof ExceptionInInitializerError) {
exception = ((ExceptionInInitializerError)t).getException();
} else if (t instanceof ClassNotFoundException) {
exception = ((ClassNotFoundException)t).getException();
} else if (t instanceof InvocationTargetException) {
exception = ((InvocationTargetException) t).getTargetException();
} else if (t instanceof UndeclaredThrowableException) {
exception = ((UndeclaredThrowableException) t).getUndeclaredThrowable();
} else if (t instanceof PrivilegedActionException) {
exception = ((PrivilegedActionException) t).getException();
} else {
// skip the cause == exception check below
e = null;
}
if (e != exception) {
throw new RuntimeException("unexpected exception: " + exception);
}
}
static class SubClass extends ExceptionInInitializerError {
public SubClass(Throwable t) {
super(t);
}
@Override
public Throwable getCause() {
return new Throwable("always new");
}
}
/*
* The following strings are base64-encoded serialized data generated
* by running the jdk10SerializeThrowable method with JDK 10 runtime.
*
* private static void jdk10SerializeThrowable(Throwable e) throws Exception {
* Base64.Encoder encoder = Base64.getEncoder();
* try (ByteArrayOutputStream os = new ByteArrayOutputStream();
* ObjectOutputStream out = new ObjectOutputStream(os)) {
* out.writeObject(e);
* out.flush();
* String s = encoder.encodeToString(os.toByteArray());
* for (int i=0; i < s.length();) {
* int end = Math.min(i+60, s.length());
* CharSequence seq = s.subSequence(i, end);
* System.out.format("\"%s\" +%n", seq);
* i = end;
* }
* }
* }
*/
private static final String EIIE_OLD_VERSION =
"rO0ABXNyACVqYXZhLmxhbmcuRXhjZXB0aW9uSW5Jbml0aWFsaXplckVycm9y" +
"FR400Amhk4ACAAFMAAlleGNlcHRpb250ABVMamF2YS9sYW5nL1Rocm93YWJs" +
"ZTt4cgAWamF2YS5sYW5nLkxpbmthZ2VFcnJvcjGtS1U0qEq6AgAAeHIAD2ph" +
"dmEubGFuZy5FcnJvckUdNlaLgg5WAgAAeHIAE2phdmEubGFuZy5UaHJvd2Fi" +
"bGXVxjUnOXe4ywMABEwABWNhdXNlcQB+AAFMAA1kZXRhaWxNZXNzYWdldAAS" +
"TGphdmEvbGFuZy9TdHJpbmc7W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XZhdAAEbWFpbnBwc3IAH2phdmEudXRpbC5Db2xsZWN0aW9ucyRFbXB0eUxp" +
"c3R6uBe0PKee3gIAAHhweHNyABNqYXZhLmlvLklPRXhjZXB0aW9ubIBzZGUl" +
"8KsCAAB4cQB+AAJxAH4AFHQAA2Zvb3VxAH4ACQAAAAFzcQB+AAsBAAAAF3EA" +
"fgANcQB+AA5xAH4AD3EAfgAQcHBxAH4AEng=";
private static Map<String, Throwable> SERIALIZED_DATA = Map.of(
EIIE_OLD_VERSION, new ExceptionInInitializerError(new NullPointerException("foo")),
CNFE_OLD_VERSION, new ClassNotFoundException("bar", new IOException("reading class file")),
ITE1_OLD_VERSION, new InvocationTargetException(new Error("foo"), "bar"),
ITE2_OLD_VERSION, new InvocationTargetException(new Error("goo")),
UTE1_OLD_VERSION, new UndeclaredThrowableException(new IllegalArgumentException("foo"), "bar"),
UTE2_OLD_VERSION, new UndeclaredThrowableException(new IllegalArgumentException("goo")),
PAE_OLD_VERSION, new PrivilegedActionException(new IOException("foo"))
);
}

View file

@ -0,0 +1,53 @@
/*
* Copyright (c) 1998, 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 4059406
@summary Throwable.toString() should call getLocalizedMessage()
@author Anand Palaniswamy
*/
public class LocalizedMessage {
static class LocalizedException extends Throwable {
boolean localizedMessageCalled = false;
LocalizedException() {
localizedMessageCalled = false;
}
public String getLocalizedMessage() {
localizedMessageCalled = true;
return new String("FOOBAR");
}
}
public static void main(String[] args) throws Exception {
LocalizedException e = new LocalizedException();
e.toString();
if (!e.localizedMessageCalled) {
throw new Exception("Throwable.toString() must call " +
"getLocalizedMessage()");
}
}
}

View file

@ -0,0 +1,81 @@
/*
* 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 4836801
* @summary Basic test for new chained exceptions added in 1.5
* @author Josh Bloch
*/
public class NewChainedExceptions {
public static void main(String args[]) {
Throwable interior = new Exception();
String message = "Good heavens!";
try {
throw new IllegalStateException(message, interior);
} catch(IllegalStateException e) {
if (!(e.getCause() == interior && e.getMessage() == message))
throw new RuntimeException("1");
}
try {
throw new IllegalStateException(interior);
} catch(IllegalStateException e) {
if (!(e.getCause() == interior &&
e.getMessage().equals(interior.toString())))
throw new RuntimeException("2");
}
try {
throw new IllegalArgumentException(message, interior);
} catch(IllegalArgumentException e) {
if (!(e.getCause() == interior && e.getMessage() == message))
throw new RuntimeException("3");
}
try {
throw new IllegalArgumentException(interior);
} catch(IllegalArgumentException e) {
if (!(e.getCause() == interior &&
e.getMessage().equals(interior.toString())))
throw new RuntimeException("4");
}
try {
throw new UnsupportedOperationException(message, interior);
} catch(UnsupportedOperationException e) {
if (!(e.getCause() == interior && e.getMessage() == message))
throw new RuntimeException("5");
}
try {
throw new UnsupportedOperationException(interior);
} catch(UnsupportedOperationException e) {
if (!(e.getCause() == interior &&
e.getMessage().equals(interior.toString())))
throw new RuntimeException("6");
}
}
}

View file

@ -0,0 +1,41 @@
/*
* Copyright (c) 2020, 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
* @summary Test Throwable::printStackTrace when fillInStackTrace is overridden
*/
public class OverrideFillInStackTrace {
public static void main(String[] args) {
Exception exc = new Exception() {
@Override
public Throwable fillInStackTrace() {
return this;
}
};
StackTraceElement[] stack = exc.getStackTrace();
if (stack.length != 0)
throw new RuntimeException("Unexpected stack trace");
exc.printStackTrace();
}
}

View file

@ -0,0 +1,186 @@
/*
* Copyright (c) 2000, 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.
*/
import java.io.*;
import java.util.*;
/*
* @test
* @bug 4202914 4363318 6991528 6998871
* @summary Basic test of serialization of stack trace information
* @author Josh Bloch
*/
public class StackTraceSerialization {
public static void main(String args[]) throws Exception {
testWithSetStackTrace();
testWithFillInStackTrace();
}
private static void testWithSetStackTrace() {
StackTraceElement[] stackTrace = {new StackTraceElement("foo", "bar", "baz", -1)};
Throwable t = new TestThrowable(true, false); // Immutable and empty stack
assertEmptyStackTrace(t);
// Verify fillInStackTrace is now a no-op.
t.fillInStackTrace();
assertEmptyStackTrace(t);
// Verify setStackTrace is now a no-op.
t.setStackTrace(stackTrace);
assertEmptyStackTrace(t);
// Verify null-handling
try {
t.setStackTrace(null);
throw new RuntimeException("No NPE on a null stack trace.");
} catch(NullPointerException npe) {
assertEmptyStackTrace(t);
}
try {
t.setStackTrace(new StackTraceElement[]{null});
throw new RuntimeException("No NPE on a null stack trace element.");
} catch(NullPointerException npe) {
assertEmptyStackTrace(t);
}
if (!equal(t, reconstitute(t)))
throw new RuntimeException("Unequal Throwables with set stacktrace");
Throwable t2 = new Throwable();
t2.setStackTrace(stackTrace);
if (!equal(t2, reconstitute(t2)))
throw new RuntimeException("Unequal Throwables with set stacktrace");
}
private static class TestThrowable extends Throwable {
public TestThrowable(boolean enableSuppression,
boolean writableStackTrace) {
super("the medium", null,
enableSuppression,
writableStackTrace);
}
}
private static void assertEmptyStackTrace(Throwable t) {
if (t.getStackTrace().length != 0)
throw new AssertionError("Nonempty stacktrace.");
}
private static void testWithFillInStackTrace() {
Throwable original = null;
try {
a();
} catch(HighLevelException e) {
original = e;
}
if (!equal(original, reconstitute(original)))
throw new RuntimeException("Unequal Throwables with filled-in stacktrace");
}
/**
* Serialize the argument and return the deserialized result.
*/
private static Throwable reconstitute(Throwable t) {
Throwable result = null;
try(ByteArrayOutputStream bout = new ByteArrayOutputStream();
ObjectOutputStream out = new ObjectOutputStream(bout)) {
out.writeObject(t);
out.flush();
try(ByteArrayInputStream bin =
new ByteArrayInputStream(bout.toByteArray());
ObjectInputStream in = new ObjectInputStream(bin)) {
result = (Throwable) in.readObject();
}
} catch(IOException | ClassNotFoundException e) {
throw new RuntimeException(e);
}
return result;
}
/**
* Returns true if e1 and e2 have equal stack traces and their
* causes are recursively equal (by the same definition) and their
* suppressed exception information is equals. Returns false or
* throws NullPointerException otherwise.
*/
private static boolean equal(Throwable t1, Throwable t2) {
return t1==t2 ||
(Arrays.equals(t1.getStackTrace(), t2.getStackTrace()) &&
equal(t1.getCause(), t2.getCause()) &&
Objects.equals(t1.getSuppressed(), t2.getSuppressed()));
}
static void a() throws HighLevelException {
try {
b();
} catch(MidLevelException e) {
throw new HighLevelException(e);
}
}
static void b() throws MidLevelException {
c();
}
static void c() throws MidLevelException {
try {
d();
} catch(LowLevelException e) {
throw new MidLevelException(e);
}
}
static void d() throws LowLevelException {
e();
}
static void e() throws LowLevelException {
throw new LowLevelException();
}
private static final String OUR_CLASS = StackTraceSerialization.class.getName();
private static final String OUR_FILE_NAME = "StackTraceSerialization.java";
private static void check(StackTraceElement e, String methodName, int n) {
if (!e.getClassName().equals(OUR_CLASS))
throw new RuntimeException("Class: " + e);
if (!e.getMethodName().equals(methodName))
throw new RuntimeException("Method name: " + e);
if (!e.getFileName().equals(OUR_FILE_NAME))
throw new RuntimeException("File name: " + e);
if (e.getLineNumber() != n)
throw new RuntimeException("Line number: " + e);
}
}
class HighLevelException extends Exception {
HighLevelException(Throwable cause) { super(cause); }
}
class MidLevelException extends Exception {
MidLevelException(Throwable cause) { super(cause); }
}
class LowLevelException extends Exception {
}

View file

@ -0,0 +1,245 @@
/*
* Copyright (c) 2010, 2025, 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.*;
/*
* @test
* @bug 6911258 6962571 6963622 6991528 7005628 8012044
* @summary Basic tests of suppressed exceptions
*/
public class SuppressedExceptions {
private static String message = "Bad suppressed exception information";
public static void main(String... args) throws Exception {
noSelfSuppression();
basicSupressionTest();
serializationTest();
selfReference();
noModification();
initCausePlumbing();
}
private static void noSelfSuppression() {
Throwable throwable = new Throwable();
try {
throwable.addSuppressed(throwable);
throw new RuntimeException("IllegalArgumentException for self-suppresion not thrown.");
} catch (IllegalArgumentException iae) {
// Expected to be here
if (iae.getCause() != throwable)
throw new RuntimeException("Bad cause after self-suppresion.");
}
}
private static void basicSupressionTest() {
Throwable throwable = new Throwable();
RuntimeException suppressed = new RuntimeException("A suppressed exception.");
AssertionError repressed = new AssertionError("A repressed error.");
Throwable[] t0 = throwable.getSuppressed();
if (t0.length != 0) {
throw new RuntimeException(message);
}
throwable.printStackTrace();
throwable.addSuppressed(suppressed);
Throwable[] t1 = throwable.getSuppressed();
if (t1.length != 1 ||
t1[0] != suppressed) {throw new RuntimeException(message);
}
throwable.printStackTrace();
throwable.addSuppressed(repressed);
Throwable[] t2 = throwable.getSuppressed();
if (t2.length != 2 ||
t2[0] != suppressed ||
t2[1] != repressed) {
throw new RuntimeException(message);
}
throwable.printStackTrace();
}
private static void serializationTest() throws Exception {
/*
* Bytes of the serial form of
*
* (new Throwable())setStackTrace(new StackTraceElement[0])
*
* from JDK 6; suppressedException field will be missing and
* thus default to null upon deserialization.
*/
byte[] bytes = {
(byte)0xac, (byte)0xed, (byte)0x00, (byte)0x05, (byte)0x73, (byte)0x72, (byte)0x00, (byte)0x13,
(byte)0x6a, (byte)0x61, (byte)0x76, (byte)0x61, (byte)0x2e, (byte)0x6c, (byte)0x61, (byte)0x6e,
(byte)0x67, (byte)0x2e, (byte)0x54, (byte)0x68, (byte)0x72, (byte)0x6f, (byte)0x77, (byte)0x61,
(byte)0x62, (byte)0x6c, (byte)0x65, (byte)0xd5, (byte)0xc6, (byte)0x35, (byte)0x27, (byte)0x39,
(byte)0x77, (byte)0xb8, (byte)0xcb, (byte)0x03, (byte)0x00, (byte)0x03, (byte)0x4c, (byte)0x00,
(byte)0x05, (byte)0x63, (byte)0x61, (byte)0x75, (byte)0x73, (byte)0x65, (byte)0x74, (byte)0x00,
(byte)0x15, (byte)0x4c, (byte)0x6a, (byte)0x61, (byte)0x76, (byte)0x61, (byte)0x2f, (byte)0x6c,
(byte)0x61, (byte)0x6e, (byte)0x67, (byte)0x2f, (byte)0x54, (byte)0x68, (byte)0x72, (byte)0x6f,
(byte)0x77, (byte)0x61, (byte)0x62, (byte)0x6c, (byte)0x65, (byte)0x3b, (byte)0x4c, (byte)0x00,
(byte)0x0d, (byte)0x64, (byte)0x65, (byte)0x74, (byte)0x61, (byte)0x69, (byte)0x6c, (byte)0x4d,
(byte)0x65, (byte)0x73, (byte)0x73, (byte)0x61, (byte)0x67, (byte)0x65, (byte)0x74, (byte)0x00,
(byte)0x12, (byte)0x4c, (byte)0x6a, (byte)0x61, (byte)0x76, (byte)0x61, (byte)0x2f, (byte)0x6c,
(byte)0x61, (byte)0x6e, (byte)0x67, (byte)0x2f, (byte)0x53, (byte)0x74, (byte)0x72, (byte)0x69,
(byte)0x6e, (byte)0x67, (byte)0x3b, (byte)0x5b, (byte)0x00, (byte)0x0a, (byte)0x73, (byte)0x74,
(byte)0x61, (byte)0x63, (byte)0x6b, (byte)0x54, (byte)0x72, (byte)0x61, (byte)0x63, (byte)0x65,
(byte)0x74, (byte)0x00, (byte)0x1e, (byte)0x5b, (byte)0x4c, (byte)0x6a, (byte)0x61, (byte)0x76,
(byte)0x61, (byte)0x2f, (byte)0x6c, (byte)0x61, (byte)0x6e, (byte)0x67, (byte)0x2f, (byte)0x53,
(byte)0x74, (byte)0x61, (byte)0x63, (byte)0x6b, (byte)0x54, (byte)0x72, (byte)0x61, (byte)0x63,
(byte)0x65, (byte)0x45, (byte)0x6c, (byte)0x65, (byte)0x6d, (byte)0x65, (byte)0x6e, (byte)0x74,
(byte)0x3b, (byte)0x78, (byte)0x70, (byte)0x71, (byte)0x00, (byte)0x7e, (byte)0x00, (byte)0x04,
(byte)0x70, (byte)0x75, (byte)0x72, (byte)0x00, (byte)0x1e, (byte)0x5b, (byte)0x4c, (byte)0x6a,
(byte)0x61, (byte)0x76, (byte)0x61, (byte)0x2e, (byte)0x6c, (byte)0x61, (byte)0x6e, (byte)0x67,
(byte)0x2e, (byte)0x53, (byte)0x74, (byte)0x61, (byte)0x63, (byte)0x6b, (byte)0x54, (byte)0x72,
(byte)0x61, (byte)0x63, (byte)0x65, (byte)0x45, (byte)0x6c, (byte)0x65, (byte)0x6d, (byte)0x65,
(byte)0x6e, (byte)0x74, (byte)0x3b, (byte)0x02, (byte)0x46, (byte)0x2a, (byte)0x3c, (byte)0x3c,
(byte)0xfd, (byte)0x22, (byte)0x39, (byte)0x02, (byte)0x00, (byte)0x00, (byte)0x78, (byte)0x70,
(byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x78, (byte)0xac, (byte)0xed, (byte)0x00,
(byte)0x05, (byte)0x73, (byte)0x72, (byte)0x00, (byte)0x13, (byte)0x6a, (byte)0x61, (byte)0x76,
(byte)0x61, (byte)0x2e, (byte)0x6c, (byte)0x61, (byte)0x6e, (byte)0x67, (byte)0x2e, (byte)0x54,
(byte)0x68, (byte)0x72, (byte)0x6f, (byte)0x77, (byte)0x61, (byte)0x62, (byte)0x6c, (byte)0x65,
(byte)0xd5, (byte)0xc6, (byte)0x35, (byte)0x27, (byte)0x39, (byte)0x77, (byte)0xb8, (byte)0xcb,
(byte)0x03, (byte)0x00, (byte)0x03, (byte)0x4c, (byte)0x00, (byte)0x05, (byte)0x63, (byte)0x61,
(byte)0x75, (byte)0x73, (byte)0x65, (byte)0x74, (byte)0x00, (byte)0x15, (byte)0x4c, (byte)0x6a,
(byte)0x61, (byte)0x76, (byte)0x61, (byte)0x2f, (byte)0x6c, (byte)0x61, (byte)0x6e, (byte)0x67,
(byte)0x2f, (byte)0x54, (byte)0x68, (byte)0x72, (byte)0x6f, (byte)0x77, (byte)0x61, (byte)0x62,
(byte)0x6c, (byte)0x65, (byte)0x3b, (byte)0x4c, (byte)0x00, (byte)0x0d, (byte)0x64, (byte)0x65,
(byte)0x74, (byte)0x61, (byte)0x69, (byte)0x6c, (byte)0x4d, (byte)0x65, (byte)0x73, (byte)0x73,
(byte)0x61, (byte)0x67, (byte)0x65, (byte)0x74, (byte)0x00, (byte)0x12, (byte)0x4c, (byte)0x6a,
(byte)0x61, (byte)0x76, (byte)0x61, (byte)0x2f, (byte)0x6c, (byte)0x6e, (byte)0x67, (byte)0x3b,
(byte)0x61, (byte)0x6e, (byte)0x67, (byte)0x2f, (byte)0x53, (byte)0x74, (byte)0x72, (byte)0x69,
(byte)0x5b, (byte)0x00, (byte)0x0a, (byte)0x73, (byte)0x74, (byte)0x61, (byte)0x63, (byte)0x6b,
(byte)0x54, (byte)0x72, (byte)0x61, (byte)0x63, (byte)0x65, (byte)0x74, (byte)0x00, (byte)0x1e,
(byte)0x5b, (byte)0x4c, (byte)0x6a, (byte)0x61, (byte)0x76, (byte)0x61, (byte)0x2f, (byte)0x6c,
(byte)0x61, (byte)0x6e, (byte)0x67, (byte)0x2f, (byte)0x53, (byte)0x74, (byte)0x61, (byte)0x63,
(byte)0x6b, (byte)0x54, (byte)0x72, (byte)0x61, (byte)0x63, (byte)0x65, (byte)0x45, (byte)0x6c,
(byte)0x65, (byte)0x6d, (byte)0x65, (byte)0x6e, (byte)0x74, (byte)0x3b, (byte)0x78, (byte)0x70,
(byte)0x71, (byte)0x00, (byte)0x7e, (byte)0x00, (byte)0x04, (byte)0x70, (byte)0x75, (byte)0x72,
(byte)0x00, (byte)0x1e, (byte)0x5b, (byte)0x4c, (byte)0x6a, (byte)0x61, (byte)0x76, (byte)0x61,
(byte)0x2e, (byte)0x6c, (byte)0x61, (byte)0x6e, (byte)0x67, (byte)0x2e, (byte)0x53, (byte)0x74,
(byte)0x61, (byte)0x63, (byte)0x6b, (byte)0x54, (byte)0x72, (byte)0x61, (byte)0x63, (byte)0x65,
(byte)0x45, (byte)0x6c, (byte)0x65, (byte)0x6d, (byte)0x65, (byte)0x6e, (byte)0x74, (byte)0x3b,
(byte)0x02, (byte)0x46, (byte)0x2a, (byte)0x3c, (byte)0x3c, (byte)0xfd, (byte)0x22, (byte)0x39,
(byte)0x02, (byte)0x00, (byte)0x00, (byte)0x78, (byte)0x70,
};
try(ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
ObjectInputStream ois = new ObjectInputStream(bais)) {
Object o = ois.readObject();
Throwable throwable = (Throwable) o;
System.err.println("TESTING SERIALIZED EXCEPTION");
Throwable[] t0 = throwable.getSuppressed();
if (t0.length != 0) { // Will fail if t0 is null.
throw new RuntimeException(message);
}
throwable.printStackTrace();
}
}
private static void selfReference() {
Throwable throwable1 = new RuntimeException();
Throwable throwable2 = new AssertionError();
throwable1.initCause(throwable2);
throwable2.initCause(throwable1);
throwable1.printStackTrace();
throwable1.addSuppressed(throwable2);
throwable2.addSuppressed(throwable1);
throwable1.printStackTrace();
}
private static void noModification() {
Throwable t = new NoSuppression(false);
Throwable[] t0 = t.getSuppressed();
if (t0.length != 0)
throw new RuntimeException("Bad nonzero length of suppressed exceptions.");
t.addSuppressed(new ArithmeticException());
// Make sure a suppressed exception did *not* get added.
t0 = t.getSuppressed();
if (t0.length != 0)
throw new RuntimeException("Bad nonzero length of suppressed exceptions.");
Throwable suppressed = new ArithmeticException();
t = new NoSuppression(true); // Suppression enabled
// Make sure addSuppressed(null) throws an NPE
try {
t.addSuppressed(null);
throw new RuntimeException("NPE not thrown!");
} catch(NullPointerException e) {
; // Expected
}
t.addSuppressed(suppressed);
t0 = t.getSuppressed();
if (t0.length != 1 || t0[0] != suppressed)
throw new RuntimeException("Expected suppression did not occur.");
}
private static class NoSuppression extends Throwable {
public NoSuppression(boolean enableSuppression) {
super("The medium.", null, enableSuppression, true);
}
}
private static void initCausePlumbing() {
Throwable t1 = new Throwable();
Throwable t2 = new Throwable("message", t1);
Throwable t3 = new Throwable();
try {
t2.initCause(t3);
throw new RuntimeException("Shouldn't reach.");
} catch (IllegalStateException ise) {
if (ise.getCause() != t2)
throw new RuntimeException("Unexpected cause in ISE", ise);
Throwable[] suppressed = ise.getSuppressed();
if (suppressed.length != 0)
throw new RuntimeException("Bad suppression in ISE", ise);
}
try {
t2.initCause(null);
throw new RuntimeException("Shouldn't reach.");
} catch (IllegalStateException ise) {
; // Expected; don't want an NPE.
}
try {
t3.initCause(t3);
throw new RuntimeException("Shouldn't reach.");
} catch (IllegalArgumentException iae) {
if (iae.getCause() != t3)
throw new RuntimeException("Unexpected cause in ISE", iae);
}
}
}