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,189 @@
/*
* Copyright (c) 2007, 2011, 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 6571655 6571881 6574585 6571297
* @summary Test various args to task scheduling methods
*/
import java.util.*;
import java.util.concurrent.*;
import static java.util.concurrent.TimeUnit.*;
public class Args {
static final long DELAY_MS = 30 * 1000L;
void schedule(final Timer t, final TimerTask task, final Date d) {
t.schedule(task, d);
assertThrows
(IllegalStateException.class,
() -> t.schedule(task, d));
}
void schedule(final Timer t, final TimerTask task, final Date d, final
long period) {
t.schedule(task, d, period);
assertThrows
(IllegalStateException.class,
() -> t.schedule(task, d, period));
}
void scheduleAtFixedRate(final Timer t, final TimerTask task, final
Date d, final long period) {
t.scheduleAtFixedRate(task, d, period);
assertThrows
(IllegalStateException.class,
() -> t.scheduleAtFixedRate(task, d, period));
}
TimerTask counter(final CountDownLatch latch) {
return new TimerTask() { public void run() {
if (latch.getCount() == 0)
fail(String.format("Latch counted down too many times: " +
latch));
latch.countDown();
}};
}
TimerTask nop() {
return new TimerTask() { public void run() { }};
}
void test(String[] args) throws Throwable {
final Timer t = new Timer();
try {
test(t);
} finally {
// Ensure this test doesn't interfere with subsequent
// tests even in case of failure.
t.cancel();
}
// Attempts to schedule tasks on a cancelled Timer result in ISE.
final Date past = new Date(System.currentTimeMillis() - DELAY_MS);
final Date future = new Date(System.currentTimeMillis() + DELAY_MS);
assertThrows
(IllegalStateException.class,
() -> t.schedule(nop(), 42),
() -> t.schedule(nop(), 42),
() -> t.schedule(nop(), past),
() -> t.schedule(nop(), 42, 42),
() -> t.schedule(nop(), past, 42),
() -> t.scheduleAtFixedRate(nop(), 42, 42),
() -> t.scheduleAtFixedRate(nop(), past, 42),
() -> t.scheduleAtFixedRate(nop(), future, 42));
}
void test(Timer t) throws Throwable {
final TimerTask x = new TimerTask() { public void run() {}};
assertThrows
(IllegalArgumentException.class,
() -> t.schedule(x, -42),
() -> t.schedule(x, new Date(-42)),
() -> t.schedule(x, Long.MAX_VALUE),
() -> t.schedule(x, -42, 42),
() -> t.schedule(x, new Date(-42), 42),
() -> t.schedule(x, Long.MAX_VALUE, 42),
() -> t.schedule(x, 42, 0),
() -> t.schedule(x, new Date(42), 0),
() -> t.schedule(x, 42, -42),
() -> t.schedule(x, new Date(42), -42),
() -> t.scheduleAtFixedRate(x, -42, 42),
() -> t.scheduleAtFixedRate(x, new Date(-42), 42),
() -> t.scheduleAtFixedRate(x, Long.MAX_VALUE, 42),
() -> t.scheduleAtFixedRate(x, 42, 0),
() -> t.scheduleAtFixedRate(x, new Date(42), 0),
() -> t.scheduleAtFixedRate(x, 42, -42),
() -> t.scheduleAtFixedRate(x, new Date(42), -42));
assertThrows
(NullPointerException.class,
() -> t.schedule(null, 42),
() -> t.schedule(x, (Date)null),
() -> t.schedule(null, 42, 42),
() -> t.schedule(x, (Date)null, 42),
() -> t.scheduleAtFixedRate(null, 42, 42),
() -> t.scheduleAtFixedRate(x, (Date)null, 42));
// Create local classes for clearer diagnostics in case of failure
class OneShotLatch extends CountDownLatch {
OneShotLatch() { super(1); }
}
class FixedDelayLatch extends CountDownLatch {
FixedDelayLatch() { super(1); }
}
class FixedRateLatch extends CountDownLatch {
FixedRateLatch() { super(11); }
}
final CountDownLatch y1 = new OneShotLatch();
final CountDownLatch y2 = new FixedDelayLatch();
final CountDownLatch y3 = new FixedRateLatch();
final long start = System.currentTimeMillis();
final Date past = new Date(start - (10 * DELAY_MS + DELAY_MS / 2));
schedule( t, counter(y1), past);
schedule( t, counter(y2), past, DELAY_MS);
scheduleAtFixedRate(t, counter(y3), past, DELAY_MS);
check(y1.await(DELAY_MS / 4, MILLISECONDS));
check(y2.await(DELAY_MS / 4, MILLISECONDS));
check(y3.await(DELAY_MS / 4, MILLISECONDS));
final long elapsed = System.currentTimeMillis() - start;
if (elapsed >= DELAY_MS / 2)
fail(String.format("Test took too long: elapsed=%d%n",
elapsed));
}
//--------------------- Infrastructure ---------------------------
volatile int passed = 0, failed = 0;
void pass() {passed++;}
void fail() {failed++; Thread.dumpStack();}
void fail(String msg) {System.err.println(msg); fail();}
void unexpected(Throwable t) {failed++; t.printStackTrace();}
void check(boolean cond) {if (cond) pass(); else fail();}
void equal(Object x, Object y) {
if (x == null ? y == null : x.equals(y)) pass();
else fail(x + " not equal to " + y);}
public static void main(String[] args) throws Throwable {
new Args().instanceMain(args);}
void instanceMain(String[] args) throws Throwable {
try {test(args);} catch (Throwable t) {unexpected(t);}
System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
if (failed > 0) throw new AssertionError("Some tests failed");}
interface F { void f() throws Throwable; }
void assertThrows(Class<? extends Throwable> k, F... fs) {
for (F f : fs)
try {f.f(); fail("Expected " + k.getName() + " not thrown");}
catch (Throwable t) {
if (k.isAssignableFrom(t.getClass())) pass();
else unexpected(t);}}
}

View file

@ -0,0 +1,84 @@
/*
* Copyright (c) 2021, 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 8263903
* @requires vm.gc != "Epsilon"
* @summary Discarding a Timer causes the Timer thread to stop.
*/
import java.util.Timer;
import java.util.TimerTask;
import java.lang.ref.Reference;
public class AutoStop {
static final Object wakeup = new Object();
static Thread tdThread = null;
static volatile int counter = 0;
static final int COUNTER_LIMIT = 10;
public static void main(String[] args) throws Exception {
Timer t = new Timer();
// Run an event that records the timer thread.
t.schedule(new TimerTask() {
public void run() {
synchronized(wakeup) {
tdThread = Thread.currentThread();
wakeup.notify();
}
}
}, 0);
// Wait for the thread to be accessible.
try {
synchronized(wakeup) {
while (tdThread == null) {
wakeup.wait();
}
}
} catch (InterruptedException e) {
}
// Schedule some events that increment the counter.
for (int i = 0; i < COUNTER_LIMIT; ++i) {
t.schedule(new TimerTask() {
public void run() {
++counter;
}
}, 100);
}
// Ensure the timer is accessible at least until here.
Reference.reachabilityFence(t);
t = null; // Remove the reference to the timer.
System.gc(); // Run GC to trigger cleanup.
tdThread.join(); // Wait for thread to stop.
int finalCounter = counter;
if (finalCounter != COUNTER_LIMIT) {
throw new RuntimeException("Unrun events: counter = " + finalCounter);
}
}
}

View file

@ -0,0 +1,116 @@
/*
* Copyright (c) 2008, 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 6730507
* @summary java.util.Timer schedule delay Long.MAX_VALUE causes task to execute multiple times
* @author Chris Hegarty
* @author Martin Buchholz
* @key randomness
*/
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;
public class DelayOverflow
{
void scheduleNow(Timer timer, TimerTask task, int how) {
switch (how) {
case 0 :
timer.schedule(task, new Date(), Long.MAX_VALUE);
break;
case 1:
timer.schedule(task, 0L, Long.MAX_VALUE);
break;
case 2:
timer.scheduleAtFixedRate(task, new Date(), Long.MAX_VALUE);
break;
case 3:
timer.scheduleAtFixedRate(task, 0L, Long.MAX_VALUE);
break;
default:
fail(String.valueOf(how));
}
}
void sleep(long millis) {
try { Thread.sleep(millis); }
catch (Throwable t) { unexpected(t); }
}
/** Checks that scheduledExecutionTime returns a "recent" time. */
void checkScheduledExecutionTime(TimerTask task) {
long t = System.currentTimeMillis()
- task.scheduledExecutionTime();
check(t >= 0 && t < 1000 * 600);
}
void test(String[] args) throws Throwable {
for (int how=0; how<4; how++) {
final CountDownLatch done = new CountDownLatch(1);
final AtomicInteger count = new AtomicInteger(0);
final Timer timer = new Timer();
final TimerTask task = new TimerTask() {
@Override
public void run() {
checkScheduledExecutionTime(this);
count.incrementAndGet();
done.countDown();
}};
scheduleNow(timer, task, how);
done.await();
equal(count.get(), 1);
checkScheduledExecutionTime(task);
if (new java.util.Random().nextBoolean())
sleep(10);
check(task.cancel());
timer.cancel();
checkScheduledExecutionTime(task);
}
}
//--------------------- Infrastructure ---------------------------
volatile int passed = 0, failed = 0;
void pass() {passed++;}
void fail() {failed++; Thread.dumpStack();}
void fail(String msg) {System.err.println(msg); fail();}
void unexpected(Throwable t) {failed++; t.printStackTrace();}
void check(boolean cond) {if (cond) pass(); else fail();}
void equal(Object x, Object y) {
if (x == null ? y == null : x.equals(y)) pass();
else fail(x + " not equal to " + y);}
public static void main(String[] args) throws Throwable {
Class<?> k = new Object(){}.getClass().getEnclosingClass();
try {k.getMethod("instanceMain",String[].class)
.invoke( k.newInstance(), (Object) args);}
catch (Throwable e) {throw e.getCause();}}
public void instanceMain(String[] args) throws Throwable {
try {test(args);} catch (Throwable t) {unexpected(t);}
System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
if (failed > 0) throw new AssertionError("Some tests failed");}
}

View file

@ -0,0 +1,67 @@
/*
* Copyright (c) 1999, 2011, 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 4288198
* @summary Killing a Timer thread causes the Timer to fail silently on
* subsequent use.
*/
import java.util.*;
public class KillThread {
static volatile Thread tdThread;
public static void main (String[] args) throws Exception {
Timer t = new Timer();
// Start a mean event that kills the timer thread
t.schedule(new TimerTask() {
public void run() {
tdThread = Thread.currentThread();
throw new ThreadDeath();
}
}, 0);
// Wait for mean event to do the deed and thread to die.
try {
do {
Thread.sleep(100);
} while(tdThread == null);
} catch(InterruptedException e) {
}
tdThread.join();
// Try to start another event
try {
// Timer thread is dead now
t.schedule(new TimerTask() {
public void run() {
}
}, 0);
throw new Exception("We failed silently");
} catch(IllegalStateException e) {
// Killing the Timer thread is equivalent to cancelling the Timer
}
}
}

View file

@ -0,0 +1,64 @@
/*
* Copyright (c) 2003, 2014, 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 4279061 8056313
* @summary Basic test for constructors with thread name
*/
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.LinkedTransferQueue;
public class NameConstructors {
private static final String NAME1 = "Norm D. Plume";
private static final String NAME2 = "Ann Onymous";
public static void main (String[] args) throws InterruptedException {
test(new Timer(NAME1), NAME1);
test(new Timer(NAME2, true), NAME2);
}
public static void test(Timer timer, String expected) throws InterruptedException {
try {
LinkedTransferQueue<String> queue = new LinkedTransferQueue<>();
TimerTask task = new TimerTask() {
public void run() {
queue.put(Thread.currentThread().getName());
}
};
timer.schedule(task, 0L); // immediately
String actual = queue.take();
if (!expected.equals(actual)) {
throw new AssertionError(
String.format("expected='%s', actual='%s'", expected, actual));
}
} finally {
timer.cancel();
}
}
}

View file

@ -0,0 +1,58 @@
/*
* 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 4481072
* @summary Basic test for purge method
* @key randomness
*/
import java.util.*;
public class Purge {
public static void main (String[] args) {
Random rnd = new Random();
Timer timer = new Timer();
int cancelled = 0;
for (long i = 1; i <= 1000; i++) {
TimerTask task = new TimerTask() {
public void run() {
System.out.println("How come no one ever calls me?");
}
};
timer.schedule(task, i * 60*60*1000); // i hrs. hence.
if (i != 1 && rnd.nextBoolean()) {
task.cancel();
cancelled++;
}
}
int purged = timer.purge();
if (purged != cancelled)
throw new RuntimeException(purged + " != " + cancelled);
purged = timer.purge();
if (purged != 0)
throw new RuntimeException(purged + " nonzero");
timer.cancel();
}
}