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,71 @@
/*
* Copyright (c) 2006, 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 6436220
* @summary SelectionKey.attach should atomically set and return the attachment
*/
import java.nio.channels.*;
import java.util.concurrent.atomic.*;
public class AtomicAttachTest {
public static void main(String[] args) throws Exception {
Selector selector = Selector.open();
Pipe pipe = Pipe.open();
SelectableChannel channel = pipe.sink().configureBlocking(false);
final SelectionKey key = channel.register(selector, 0);
key.attach(new AtomicBoolean());
final AtomicInteger errorCount = new AtomicInteger();
Thread t = new Thread() {
public void run() {
AtomicBoolean att = new AtomicBoolean();
for (int i=0; i<(10*1000*1000); i++) {
att = (AtomicBoolean)key.attach(att);
// We should have exclusive ownership of att.
if (!att.compareAndSet(false, true) ||
!att.compareAndSet(true, false))
{
errorCount.incrementAndGet();
}
}
}
{
start();
run();
}
};
t.join();
pipe.sink().close();
pipe.source().close();
selector.close();
int count = errorCount.get();
if (count > 0) {
throw new RuntimeException("Error count:" + count);
}
}
}

View file

@ -0,0 +1,184 @@
/*
* Copyright (c) 2018, 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 6350055
* @run junit AtomicUpdates
* @summary Unit test for SelectionKey interestOpsOr and interestOpsAnd
*/
import java.io.Closeable;
import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.nio.channels.CancelledKeyException;
import java.nio.channels.SelectableChannel;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import org.junit.jupiter.api.Test;
import static java.nio.channels.SelectionKey.OP_READ;
import static java.nio.channels.SelectionKey.OP_WRITE;
import static java.nio.channels.SelectionKey.OP_CONNECT;
import static java.nio.channels.SelectionKey.OP_ACCEPT;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.fail;
public class AtomicUpdates {
private SelectionKey keyFor(SocketChannel sc) {
return new SelectionKey() {
private int ops;
private boolean invalid;
private void ensureValid() {
if (!isValid())
throw new CancelledKeyException();
}
@Override
public SelectableChannel channel() {
return sc;
}
@Override
public Selector selector() {
throw new RuntimeException();
}
@Override
public boolean isValid() {
return !invalid;
}
@Override
public void cancel() {
invalid = true;
}
@Override
public int interestOps() {
ensureValid();
return ops;
}
@Override
public SelectionKey interestOps(int ops) {
ensureValid();
if ((ops & ~channel().validOps()) != 0)
throw new IllegalArgumentException();
this.ops = ops;
return this;
}
@Override
public int readyOps() {
ensureValid();
return 0;
}
};
}
private void test(SelectionKey key) {
assertInstanceOf(SocketChannel.class, key.channel());
key.interestOps(0);
// 0 -> 0
int previous = key.interestOpsOr(0);
assertEquals(0, previous);
assertEquals(0, key.interestOps());
// 0 -> OP_CONNECT
previous = key.interestOpsOr(OP_CONNECT);
assertEquals(0, previous);
assertEquals(OP_CONNECT, key.interestOps());
// OP_CONNECT -> OP_CONNECT
previous = key.interestOpsOr(0);
assertEquals(OP_CONNECT, previous);
assertEquals(OP_CONNECT, key.interestOps());
// OP_CONNECT -> OP_CONNECT | OP_READ | OP_WRITE
previous = key.interestOpsOr(OP_READ | OP_WRITE);
assertEquals(OP_CONNECT, previous);
assertEquals(OP_CONNECT | OP_READ | OP_WRITE, key.interestOps());
// OP_CONNECT | OP_READ | OP_WRITE -> OP_CONNECT
previous = key.interestOpsAnd(~(OP_READ | OP_WRITE));
assertEquals(OP_CONNECT | OP_READ | OP_WRITE, previous);
assertEquals(OP_CONNECT, key.interestOps());
// OP_CONNECT -> 0
previous = key.interestOpsAnd(~OP_CONNECT);
assertEquals(OP_CONNECT, previous);
assertEquals(0, key.interestOps());
// OP_READ | OP_WRITE -> OP_READ | OP_WRITE
key.interestOps(OP_READ | OP_WRITE);
previous = key.interestOpsAnd(~OP_ACCEPT);
assertEquals(OP_READ | OP_WRITE, previous);
assertEquals(OP_READ | OP_WRITE, key.interestOps());
// OP_READ | OP_WRITE -> 0
previous = key.interestOpsAnd(0);
assertEquals(OP_READ | OP_WRITE, previous);
assertEquals(0, key.interestOps());
// 0 -> 0
previous = key.interestOpsAnd(0);
assertEquals(0, previous);
assertEquals(0, key.interestOps());
assertThrows(IllegalArgumentException.class,
() -> key.interestOpsOr(OP_ACCEPT));
key.cancel();
assertThrows(CancelledKeyException.class,
() -> key.interestOpsOr(OP_READ));
assertThrows(CancelledKeyException.class,
() -> key.interestOpsAnd(~OP_READ));
}
/**
* Test default implementation of interestOpsOr/interestOpsAnd
*/
@Test
public void testDefaultImplementation() throws Exception {
try (SocketChannel sc = SocketChannel.open()) {
SelectionKey key = keyFor(sc);
test(key);
}
}
/**
* Test the default provider implementation of SelectionKey.
*/
@Test
public void testNioImplementation() throws Exception {
try (SocketChannel sc = SocketChannel.open();
Selector sel = Selector.open()) {
sc.configureBlocking(false);
SelectionKey key = sc.register(sel, 0);
test(key);
}
}
}

View file

@ -0,0 +1,71 @@
/*
* Copyright (c) 2012, 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 7132889
* @summary Test that register does not return a valid SelectionKey when
* invoked at around the time that the channel is closed
*/
import java.nio.channels.*;
import java.util.concurrent.*;
import java.util.Random;
import java.io.IOException;
public class RacyRegister {
public static void main(String[] args) throws Exception {
ExecutorService pool = Executors.newFixedThreadPool(1);
try (Selector sel = Selector.open()) {
int count = 100;
while (count-- > 0) {
final SocketChannel sc = SocketChannel.open();
sc.configureBlocking(false);
// close channel asynchronously
Future<Void> result = pool.submit(new Callable<Void>() {
public Void call() throws IOException {
sc.close();
return null;
}
});
// attempt to register channel with Selector
SelectionKey key = null;
try {
key = sc.register(sel, SelectionKey.OP_READ);
} catch (ClosedChannelException ignore) {
}
// ensure close is done
result.get();
// if we have a key then it should be invalid
if (key != null && key.isValid())
throw new RuntimeException("Key is valid");
}
} finally {
pool.shutdown();
}
}
}