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,102 @@
/*
* Copyright (c) 2014, 2015, 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 8031694
@summary [macosx] TwentyThousandTest test intermittently hangs
@author Oleg Pekhovskiy
@modules java.desktop/sun.awt
@modules java.desktop/java.awt:open
@run main EDTShutdownTest
*/
import java.awt.EventQueue;
import java.awt.Toolkit;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import sun.awt.AWTAccessor;
public class EDTShutdownTest {
private static boolean passed = false;
public static void main(String[] args) {
// Force EDT start with InvocationEvent
EventQueue.invokeLater(() -> {
// EventQueue is empty now
EventQueue queue = Toolkit.getDefaultToolkit()
.getSystemEventQueue();
Thread thread = AWTAccessor.getEventQueueAccessor()
.getDispatchThread(queue);
try {
/*
* Clear EventDispatchThread.doDispatch flag to break message
* loop in EventDispatchThread.pumpEventsForFilter()
*/
Method stopDispatching = thread.getClass()
.getDeclaredMethod("stopDispatching", null);
stopDispatching.setAccessible(true);
stopDispatching.invoke(thread, null);
/*
* Post another InvocationEvent that must be handled by another
* instance of EDT
*/
EventQueue.invokeLater(() -> {
passed = true;
});
}
catch (InvocationTargetException | NoSuchMethodException
| IllegalAccessException e) {
}
});
// Wait for EDT shutdown
EventQueue queue = Toolkit.getDefaultToolkit().getSystemEventQueue();
Thread thread = AWTAccessor.getEventQueueAccessor()
.getDispatchThread(queue);
try {
thread.join();
/*
* Wait for another EDT instance to handle the InvocationEvent
* and shutdown
*/
thread = AWTAccessor.getEventQueueAccessor()
.getDispatchThread(queue);
if (thread != null) {
thread.join();
}
}
catch (InterruptedException e) {
}
if (passed) {
System.out.println("Test PASSED!");
}
else {
throw new RuntimeException("Test FAILED!");
}
}
}

View file

@ -0,0 +1,160 @@
/*
* Copyright (c) 2010, 2016, 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
@key headful
@bug 6304473 6727884
@summary Tests that an exception on EDT is handled with ThreadGroup.uncaughtException()
@author artem.ananiev: area=awt.eventdispatching
@library ../../regtesthelpers
@build Util
@run main HandleExceptionOnEDT
*/
import java.awt.*;
import java.awt.event.*;
import test.java.awt.regtesthelpers.Util;
public class HandleExceptionOnEDT
{
private final static String EXCEPTION_MESSAGE = "A1234567890";
private static volatile boolean exceptionHandled = false;
private static volatile boolean mousePressed = false;
public static void main(String[] args)
{
final Thread.UncaughtExceptionHandler eh = new Thread.UncaughtExceptionHandler()
{
@Override
public void uncaughtException(Thread t, Throwable e)
{
if (e.getMessage().equals(EXCEPTION_MESSAGE))
{
exceptionHandled = true;
}
}
};
Frame f = new Frame("F");
f.setBounds(100, 100, 400, 300);
// set exception handler for EDT
f.addWindowListener(new WindowAdapter()
{
@Override
public void windowOpened(WindowEvent we)
{
Thread edt = Thread.currentThread();
edt.setUncaughtExceptionHandler(eh);
}
});
f.setVisible(true);
Robot r = Util.createRobot();
Util.waitForIdle(r);
// check exception without modal dialog
MouseListener exceptionListener = new MouseAdapter()
{
@Override
public void mousePressed(MouseEvent me)
{
throw new RuntimeException(EXCEPTION_MESSAGE);
}
};
f.addMouseListener(exceptionListener);
exceptionHandled = false;
Point fp = f.getLocationOnScreen();
r.mouseMove(fp.x + f.getWidth() / 2, fp.y + f.getHeight() / 2);
Util.waitForIdle(r);
r.mousePress(InputEvent.BUTTON1_DOWN_MASK);
Util.waitForIdle(r);
r.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
f.removeMouseListener(exceptionListener);
if (!exceptionHandled)
{
throw new RuntimeException("Test FAILED: exception is not handled for frame");
}
// check exception with modal dialog
final Dialog d = new Dialog(f, "D", true);
d.setBounds(fp.x + 100, fp.y + 100, 400, 300);
d.addMouseListener(exceptionListener);
EventQueue.invokeLater(new Runnable()
{
@Override
public void run()
{
d.setVisible(true);
}
});
Util.waitForIdle(r);
exceptionHandled = false;
Point dp = d.getLocationOnScreen();
r.mouseMove(dp.x + d.getWidth() / 2, dp.y + d.getHeight() / 2);
Util.waitForIdle(r);
r.mousePress(InputEvent.BUTTON1_DOWN_MASK);
Util.waitForIdle(r);
r.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
d.removeMouseListener(exceptionListener);
if (!exceptionHandled)
{
throw new RuntimeException("Test FAILED: exception is not handled for modal dialog");
}
// check the dialog is still modal
MouseListener pressedListener = new MouseAdapter()
{
@Override
public void mousePressed(MouseEvent me)
{
mousePressed = true;
}
};
f.addMouseListener(pressedListener);
mousePressed = false;
r.mouseMove(fp.x + 50, fp.y + 50);
Util.waitForIdle(r);
r.mousePress(InputEvent.BUTTON1_DOWN_MASK);
Util.waitForIdle(r);
r.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
Util.waitForIdle(r);
f.removeMouseListener(pressedListener);
if (mousePressed)
{
throw new RuntimeException("Test FAILED: modal dialog is not modal or visible after exception");
}
// test is passed
d.dispose();
f.dispose();
}
}

View file

@ -0,0 +1,167 @@
/*
* Copyright (c) 1998, 2013, 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 4023283
* @key headful
* @summary Checks that an Error which propogates up to the EventDispatch
* loop does not crash AWT.
* @author Andrei Dmitriev: area=awt.event
* @library ../../regtesthelpers
* @modules java.desktop/sun.awt
* @build Util
* @run main LoopRobustness
*/
import java.awt.*;
import java.awt.event.*;
import sun.awt.SunToolkit;
import test.java.awt.regtesthelpers.Util;
public class LoopRobustness {
final static long TIMEOUT = 5000;
final static Object LOCK = new Object();
public static int clicks = 0;
public static volatile boolean notifyOccured = false;
public static volatile boolean otherExceptionsCaught = false;
public static void main(String [] args) throws Exception {
SunToolkit.createNewAppContext();
ThreadGroup mainThreadGroup = Thread.currentThread().getThreadGroup();
long at;
//wait for a TIMEOUT giving a chance to a new Thread above to accomplish its stuff.
synchronized (LoopRobustness.LOCK) {
new Thread(new TestThreadGroup(mainThreadGroup, "TestGroup"), new Impl()).start();
at = System.currentTimeMillis();
try {
while (!notifyOccured && (System.currentTimeMillis() - at < TIMEOUT)) {
LoopRobustness.LOCK.wait(1000);
}
} catch (InterruptedException e) {
throw new RuntimeException("Test interrupted.", e);
}
}
if (!notifyOccured) {
//notify doesn't occur after a reasonable time.
throw new RuntimeException("Test FAILED: second thread hasn't notified MainThread");
}
//now wait for two clicks
at = System.currentTimeMillis();
while(System.currentTimeMillis() - at < TIMEOUT && clicks < 2) {
try {
Thread.sleep(100);
} catch(InterruptedException e) {
throw new RuntimeException("Test interrupted.", e);
}
}
if (clicks != 2) {
throw new RuntimeException("Test FAILED: robot should press button twice");
}
if (otherExceptionsCaught) {
throw new RuntimeException("Test FAILED: unexpected exceptions caught");
}
}
}
class Impl implements Runnable{
static Robot robot;
public void run() {
SunToolkit.createNewAppContext();
Button b = new Button("Press me to test the AWT-Event Queue thread");
Frame lr = new Frame("ROBUST FRAME");
lr.setBounds(100, 100, 300, 100);
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
LoopRobustness.clicks++;
//throwing an exception in Static Initializer
System.out.println(HostileCrasher.aStaticMethod());
}
});
lr.add(b);
lr.setVisible(true);
try {
robot = new Robot();
} catch (AWTException e) {
throw new RuntimeException("Test interrupted.", e);
}
Util.waitForIdle(robot);
synchronized (LoopRobustness.LOCK){
LoopRobustness.LOCK.notify();
LoopRobustness.notifyOccured = true;
}
int i = 0;
while (i < 2) {
robot.mouseMove(b.getLocationOnScreen().x + b.getWidth()/2,
b.getLocationOnScreen().y + b.getHeight()/2);
Util.waitForIdle(robot);
robot.mousePress(InputEvent.BUTTON1_MASK);
Util.waitForIdle(robot);
robot.mouseRelease(InputEvent.BUTTON1_MASK);
Util.waitForIdle(robot);
i++;
}
}
}
class TestThreadGroup extends ThreadGroup {
TestThreadGroup(ThreadGroup threadGroup, String name) {
super(threadGroup, name);
}
public void uncaughtException(Thread thread, Throwable e) {
System.out.println("Exception caught: " + e);
e.printStackTrace(System.out);
System.out.flush();
if ((e instanceof ExceptionInInitializerError) ||
(e instanceof NoClassDefFoundError))
{
// These two are expected
return;
}
LoopRobustness.otherExceptionsCaught = true;
}
}
class HostileCrasher {
static {
if (Math.random() >= 0.0) {
throw new RuntimeException("Die, AWT-Event Queue thread!");
}
}
public static String aStaticMethod() {
return "hello, world";
}
}

View file

@ -0,0 +1,225 @@
/*
* Copyright (c) 2010, 2016, 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
@key headful
@bug 6424157
@author Artem Ananiev: area=eventqueue
@run main PreserveDispatchThread
*/
import java.awt.*;
import java.awt.event.*;
public class PreserveDispatchThread {
private static volatile Frame f;
private static volatile Dialog d;
private static volatile boolean isEDT = true;
public static void main(String[] args) throws Exception {
f = new Frame("F");
f.setSize(320, 340);
f.setLocationRelativeTo(null);
f.setVisible(true);
try {
test1();
if (!isEDT) {
throw new RuntimeException("Test FAILED (test1): event dispatch thread is changed");
}
test2();
if (!isEDT) {
throw new RuntimeException("Test FAILED (test2): event dispatch thread is changed");
}
test3();
if (!isEDT) {
throw new RuntimeException("Test FAILED (test3): event dispatch thread is changed");
}
} finally {
if (d != null) {
d.dispose();
}
f.dispose();
}
}
/*
* Tests that push/pop doesn't change the dispatch thread if
* called on EDT.
*/
private static void test1() throws Exception {
EventQueue.invokeAndWait(new Runnable() {
@Override
public void run() {
TestEventQueue teq = new TestEventQueue();
EventQueue seq = Toolkit.getDefaultToolkit().getSystemEventQueue();
try {
seq.push(teq);
d = new TestDialog();
d.setVisible(true);
checkEDT();
} finally {
teq.pop();
}
checkEDT();
}
});
}
/*
* Tests that push/pop doesn't change the dispatch thread if
* called on the main thread.
*/
private static void test2() throws Exception {
TestEventQueue teq = new TestEventQueue();
EventQueue seq = Toolkit.getDefaultToolkit().getSystemEventQueue();
try {
seq.push(teq);
EventQueue.invokeAndWait(new Runnable() {
@Override
public void run() {
checkEDT();
d = new TestDialog();
d.setVisible(true);
checkEDT();
}
});
} finally {
teq.pop();
}
}
private static final Object test3Lock = new Object();
private static boolean test3Sync = false;
/*
* A complex test: several nested invokeLater() are called and
* in every runnable a check for EDT is performed. At the ent
* of the test we wait for all the runnables to be processed
* and the dialog is disposed; otherwise the last EDT check can
* be later than this method returns and the whole test is passed.
*/
private static void test3() throws Exception {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
d = new Dialog(f, true);
d.setSize(240, 180);
d.setLocationRelativeTo(f);
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
d.setVisible(true);
checkEDT();
}
});
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
TestEventQueue teq = new TestEventQueue();
EventQueue seq = Toolkit.getDefaultToolkit().getSystemEventQueue();
try {
seq.push(teq);
checkEDT();
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
d.dispose();
checkEDT();
synchronized (test3Lock) {
test3Sync = true;
test3Lock.notify();
}
}
});
} finally {
teq.pop();
}
checkEDT();
}
});
checkEDT();
}
});
synchronized (test3Lock) {
while (!test3Sync) {
try {
test3Lock.wait();
} catch (InterruptedException ie) {
break;
}
}
}
// Make sure all the nested invokeLater/invokeAndWait are processed
EventQueue.invokeAndWait(new Runnable() {
@Override
public void run() {
}
});
}
private static void checkEDT() {
isEDT = isEDT && EventQueue.isDispatchThread();
}
private static class TestEventQueue extends EventQueue {
public TestEventQueue() {
super();
}
public void pop() {
super.pop();
}
}
private static class TestDialog extends Dialog {
private volatile boolean dialogShown = false;
private volatile boolean paintCalled = false;
public TestDialog() {
super(f, true);
setSize(240, 180);
setLocationRelativeTo(f);
addComponentListener(new ComponentAdapter() {
@Override
public void componentShown(ComponentEvent e) {
if (paintCalled) {
dispose();
}
dialogShown = true;
}
});
}
@Override
public void paint(Graphics g) {
if (dialogShown) {
dispose();
}
paintCalled = true;
}
}
}

View file

@ -0,0 +1,118 @@
/*
* Copyright (c) 2003, 2023, 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 4848555
@summary Popping an event queue could cause its thread to restart inadvertently
@run main StoppingEdtOnPushPopTest
*/
import java.awt.AWTEvent;
import java.awt.ActiveEvent;
import java.awt.EventQueue;
import java.awt.Toolkit;
public class StoppingEdtOnPushPopTest implements Runnable {
public void start() {
int before = countEventQueues();
try {
for (int i = 0; i < 10; i++) {
EventQueue.invokeAndWait(this);
}
} catch (InterruptedException e) {
e.printStackTrace();
throw new RuntimeException("Test was interrupted");
} catch (java.lang.reflect.InvocationTargetException e) {
e.printStackTrace();
throw new RuntimeException("InvocationTargetException occurred");
}
pause(1000);
int after = countEventQueues();
if (before < after && after > 1) {
throw new RuntimeException("Test failed (before=" + before
+ "; after=" + after + ")");
}
System.out.println("Test passed");
}
public void run() {
System.out.println("push/pop");
MyEventQueue queue = new MyEventQueue();
Toolkit.getDefaultToolkit().getSystemEventQueue().push(queue);
Toolkit.getDefaultToolkit().getSystemEventQueue()
.postEvent(new EmptyEvent());
queue.pop();
}
public int countEventQueues() {
int count = 0;
System.out.println("All threads currently running in the system");
Thread threads[] = new Thread[Thread.activeCount()];
Thread.enumerate(threads);
for (int i = 0; i < threads.length; ++i) {
Thread thread = threads[i];
if (thread != null) {
System.out.println(thread.getName());
if (thread.getName().startsWith("AWT-EventQueue")) {
count++;
}
}
}
return count;
}
public void pause(long aMillis) {
try {
Thread.sleep(aMillis);
} catch (InterruptedException e) {
e.printStackTrace();
throw new RuntimeException("Test was interrupted");
}
}
public static void main(String[] args) {
StoppingEdtOnPushPopTest test = new StoppingEdtOnPushPopTest();
test.start();
}
}
class MyEventQueue extends EventQueue {
public MyEventQueue() {
super();
}
public void pop() {
super.pop();
}
}
class EmptyEvent extends AWTEvent implements ActiveEvent {
public EmptyEvent() {
super(new Object(), 0);
}
public void dispatch() {
System.out.println("one more EmptyEvent");
}
}