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,216 @@
/*
* Copyright (c) 2012, 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.
*/
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertTrue;
import static org.testng.Assert.fail;
/**
* @library
* CollectionAssert -- assertion methods for lambda test cases
*/
public class CollectionAsserts {
private CollectionAsserts() {
// no instances
}
public static void assertCountSum(Iterable<? super Integer> it, int count, int sum) {
assertCountSum(it.iterator(), count, sum);
}
public static void assertCountSum(Iterator<? super Integer> it, int count, int sum) {
int c = 0;
int s = 0;
while (it.hasNext()) {
int i = (Integer) it.next();
c++;
s += i;
}
assertEquals(c, count);
assertEquals(s, sum);
}
public static void assertConcat(Iterator<Character> it, String result) {
StringBuilder sb = new StringBuilder();
while (it.hasNext()) {
sb.append(it.next());
}
assertEquals(result, sb.toString());
}
public static<T extends Comparable<? super T>> void assertSorted(Iterator<T> i) {
if (!i.hasNext())
return;
T last = i.next();
while (i.hasNext()) {
T t = i.next();
assertTrue(last.compareTo(t) <= 0);
assertTrue(t.compareTo(last) >= 0);
last = t;
}
}
public static<T> void assertSorted(Iterator<T> i, Comparator<? super T> comp) {
if (!i.hasNext())
return;
T last = i.next();
while (i.hasNext()) {
T t = i.next();
assertTrue(comp.compare(last, t) <= 0);
assertTrue(comp.compare(t, last) >= 0);
last = t;
}
}
public static<T extends Comparable<? super T>> void assertSorted(Iterable<T> iter) {
assertSorted(iter.iterator());
}
public static<T> void assertSorted(Iterable<T> iter, Comparator<? super T> comp) {
assertSorted(iter.iterator(), comp);
}
public static <T> void assertUnique(Iterable<T> iter) {
assertUnique(iter.iterator());
}
public static<T> void assertUnique(Iterator<T> iter) {
if (!iter.hasNext()) {
return;
}
Set<T> uniq = new HashSet<>();
while (iter.hasNext()) {
T each = iter.next();
assertTrue(!uniq.contains(each));
uniq.add(each);
}
}
public static<T> void assertContents(Iterable<T> actual, Iterable<T> expected) {
assertContents(actual, expected, null);
}
public static<T> void assertContents(Iterable<T> actual, Iterable<T> expected, String msg) {
assertContents(actual.iterator(), expected.iterator(), msg);
}
public static<T> void assertContents(Iterator<T> actual, Iterator<T> expected) {
assertContents(actual, expected, null);
}
public static<T> void assertContents(Iterator<T> actual, Iterator<T> expected, String msg) {
List<T> history = new ArrayList<>();
while (expected.hasNext()) {
if (!actual.hasNext()) {
List<T> expectedData = new ArrayList<>(history);
while (expected.hasNext())
expectedData.add(expected.next());
fail(String.format("%s Premature end of data; expected=%s, found=%s",
(msg == null ? "" : msg), expectedData, history));
}
T a = actual.next();
T e = expected.next();
history.add(a);
if (!Objects.equals(a, e))
fail(String.format("%s Data mismatch; preceding=%s, nextExpected=%s, nextFound=%s",
(msg == null ? "" : msg), history, e, a));
}
if (actual.hasNext()) {
List<T> rest = new ArrayList<>();
while (actual.hasNext())
rest.add(actual.next());
fail(String.format("%s Unexpected data %s after %s",
(msg == null ? "" : msg), rest, history));
}
}
@SafeVarargs
@SuppressWarnings("varargs")
public static<T> void assertContents(Iterator<T> actual, T... expected) {
assertContents(actual, Arrays.asList(expected).iterator());
}
public static<T extends Comparable<? super T>> void assertContentsUnordered(Iterable<T> actual, Iterable<T> expected) {
assertContentsUnordered(actual, expected, null);
}
public static<T extends Comparable<? super T>> void assertContentsUnordered(Iterable<T> actual, Iterable<T> expected, String msg) {
List<T> allExpected = new ArrayList<>();
for (T t : expected) {
allExpected.add(t);
}
for (T t : actual) {
assertTrue(allExpected.remove(t), msg + " element '" + String.valueOf(t) + "' not found");
}
assertTrue(allExpected.isEmpty(), msg + "expected contained additional elements");
}
static <T> void assertSplitContents(Iterable<Iterable<T>> splits, Iterable<T> list) {
Iterator<Iterable<T>> mI = splits.iterator();
Iterator<T> pI = null;
Iterator<T> lI = list.iterator();
while (lI.hasNext()) {
if (pI == null)
pI = mI.next().iterator();
while (!pI.hasNext()) {
if (!mI.hasNext()) {
break;
}
else {
pI = mI.next().iterator();
}
}
assertTrue(pI.hasNext());
T pT = pI.next();
T lT = lI.next();
assertEquals(pT, lT);
}
if (pI != null) {
assertTrue(!pI.hasNext());
}
while (mI.hasNext()) {
pI = mI.next().iterator();
assertTrue(!pI.hasNext());
}
}
}

View file

@ -0,0 +1,250 @@
/*
* Copyright (c) 2012, 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.
*/
import java.lang.Exception;
import java.lang.Integer;
import java.lang.Iterable;
import java.lang.Override;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;
import org.testng.TestException;
import static org.testng.Assert.assertTrue;
import java.util.Collection;
import java.util.Collections;
import java.util.function.Function;
import java.util.function.Supplier;
/**
* @library
* @summary A Supplier of test cases for Collection tests
*/
public final class CollectionSupplier<C extends Collection<Integer>> implements Supplier<Iterable<CollectionSupplier.TestCase<C>>> {
private final List<Function<Collection<Integer>, C>> suppliers;
private final int size;
/**
* A Collection test case.
*/
public static final class TestCase<C extends Collection<Integer>> {
/**
* The name of the test case.
*/
public final String name;
/**
* The supplier of a collection
*/
public Function<Collection<Integer>, C> supplier;
/**
* Unmodifiable reference collection, useful for comparisons.
*/
public final List<Integer> expected;
/**
* A modifiable test collection.
*/
public final C collection;
/**
* Create a Collection test case.
*
* @param name name of the test case
* @param collection the modifiable test collection
*/
public TestCase(String name, Function<Collection<Integer>, C> supplier, C collection) {
this.name = name;
this.supplier = supplier;
this.expected = Collections.unmodifiableList(
Arrays.asList(collection.toArray(new Integer[0])));
this.collection = collection;
}
@Override
public String toString() {
return name + " " + collection.getClass().toString();
}
}
/**
* Shuffle a list using a PRNG with known seed for repeatability
*
* @param list the list to be shuffled
*/
public static <E> void shuffle(final List<E> list) {
// PRNG with known seed for repeatable tests
final Random prng = new Random(13);
final int size = list.size();
for (int i = 0; i < size; i++) {
// random index in interval [i, size)
final int j = i + prng.nextInt(size - i);
// swap elements at indices i & j
final E e = list.get(i);
list.set(i, list.get(j));
list.set(j, e);
}
}
/**
* Create a {@code CollectionSupplier} that creates instances of specified
* collection suppliers of the specified size.
*
* @param suppliers the suppliers names that supply {@code Collection}
* instances
* @param size the desired size of each collection
*/
public CollectionSupplier(List<Function<Collection<Integer>, C>> suppliers, int size) {
this.suppliers = suppliers;
this.size = size;
}
@Override
public Iterable<TestCase<C>> get() {
final Collection<TestCase<C>> cases = new LinkedList<>();
for (final Function<Collection<Integer>, C> supplier : suppliers)
try {
cases.add(new TestCase<>("empty", supplier, supplier.apply(Collections.emptyList())));
cases.add(new TestCase<>("single", supplier, supplier.apply(Arrays.asList(42))));
final Collection<Integer> regular = new ArrayList<>();
for (int i = 0; i < size; i++) {
regular.add(i);
}
cases.add(new TestCase<>("regular", supplier, supplier.apply(regular)));
final Collection<Integer> reverse = new ArrayList<>();
for (int i = size; i >= 0; i--) {
reverse.add(i);
}
cases.add(new TestCase<>("reverse", supplier, supplier.apply(reverse)));
final Collection<Integer> odds = new ArrayList<>();
for (int i = 0; i < size; i++) {
odds.add((i * 2) + 1);
}
cases.add(new TestCase<>("odds", supplier, supplier.apply(odds)));
final Collection<Integer> evens = new ArrayList<>();
for (int i = 0; i < size; i++) {
evens.add(i * 2);
}
cases.add(new TestCase<>("evens", supplier, supplier.apply(evens)));
final Collection<Integer> fibonacci = new ArrayList<>();
int prev2 = 0;
int prev1 = 1;
for (int i = 0; i < size; i++) {
final int n = prev1 + prev2;
if (n < 0) { // stop on overflow
break;
}
fibonacci.add(n);
prev2 = prev1;
prev1 = n;
}
cases.add(new TestCase<>("fibonacci", supplier, supplier.apply(fibonacci)));
boolean isStructurallyModifiable = false;
try {
C t = supplier.apply(Collections.emptyList());
t.add(1);
isStructurallyModifiable = true;
} catch (UnsupportedOperationException e) { }
if (!isStructurallyModifiable)
continue;
// variants where the size of the backing storage != reported size
// created by removing half of the elements
final C emptyWithSlack = supplier.apply(Collections.emptyList());
emptyWithSlack.add(42);
assertTrue(emptyWithSlack.remove(42));
cases.add(new TestCase<>("emptyWithSlack", supplier, emptyWithSlack));
final C singleWithSlack = supplier.apply(Collections.emptyList());
singleWithSlack.add(42);
singleWithSlack.add(43);
assertTrue(singleWithSlack.remove(43));
cases.add(new TestCase<>("singleWithSlack", supplier, singleWithSlack));
final C regularWithSlack = supplier.apply(Collections.emptyList());
for (int i = 0; i < (2 * size); i++) {
regularWithSlack.add(i);
}
assertTrue(regularWithSlack.removeIf(x -> x < size));
cases.add(new TestCase<>("regularWithSlack", supplier, regularWithSlack));
final C reverseWithSlack = supplier.apply(Collections.emptyList());
for (int i = 2 * size; i >= 0; i--) {
reverseWithSlack.add(i);
}
assertTrue(reverseWithSlack.removeIf(x -> x < size));
cases.add(new TestCase<>("reverseWithSlack", supplier, reverseWithSlack));
final C oddsWithSlack = supplier.apply(Collections.emptyList());
for (int i = 0; i < 2 * size; i++) {
oddsWithSlack.add((i * 2) + 1);
}
assertTrue(oddsWithSlack.removeIf(x -> x >= size));
cases.add(new TestCase<>("oddsWithSlack", supplier, oddsWithSlack));
final C evensWithSlack = supplier.apply(Collections.emptyList());
for (int i = 0; i < 2 * size; i++) {
evensWithSlack.add(i * 2);
}
assertTrue(evensWithSlack.removeIf(x -> x >= size));
cases.add(new TestCase<>("evensWithSlack", supplier, evensWithSlack));
final C fibonacciWithSlack = supplier.apply(Collections.emptyList());
prev2 = 0;
prev1 = 1;
for (int i = 0; i < size; i++) {
final int n = prev1 + prev2;
if (n < 0) { // stop on overflow
break;
}
fibonacciWithSlack.add(n);
prev2 = prev1;
prev1 = n;
}
assertTrue(fibonacciWithSlack.removeIf(x -> x < 20));
cases.add(new TestCase<>("fibonacciWithSlack", supplier, fibonacciWithSlack));
}
catch (Exception failed) {
throw new TestException(failed);
}
return cases;
}
}

View file

@ -0,0 +1,84 @@
/*
* Copyright (c) 2012, 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.
*/
import java.util.AbstractCollection;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.function.Supplier;
/**
* @library
*
* A simple mutable collection implementation that provides only default
* implementations of all methods. ie. none of the Collection interface default
* methods have overridden implementations.
*
* @param <E> type of collection elements
*/
public class ExtendsAbstractCollection<E> extends AbstractCollection<E> {
protected final Collection<E> coll;
public ExtendsAbstractCollection() {
this(ArrayList<E>::new);
}
public ExtendsAbstractCollection(Collection<E> source) {
this();
coll.addAll(source);
}
protected ExtendsAbstractCollection(Supplier<Collection<E>> backer) {
this.coll = backer.get();
}
public boolean add(E element) {
return coll.add(element);
}
public boolean remove(Object element) {
return coll.remove(element);
}
public Iterator<E> iterator() {
return new Iterator<E>() {
Iterator<E> source = coll.iterator();
public boolean hasNext() {
return source.hasNext();
}
public E next() {
return source.next();
}
public void remove() {
source.remove();
}
};
}
public int size() {
return coll.size();
}
}

View file

@ -0,0 +1,101 @@
/*
* Copyright (c) 2012, 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.
*/
import java.util.ArrayList;
import java.util.AbstractList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.function.Supplier;
/**
* @library
*
* A simple mutable list implementation that provides only default
* implementations of all methods. ie. none of the List interface default
* methods have overridden implementations.
*
* @param <E> type of list elements
*/
public class ExtendsAbstractList<E> extends AbstractList<E> {
protected final List<E> list;
public ExtendsAbstractList() {
this(ArrayList<E>::new);
}
protected ExtendsAbstractList(Supplier<List<E>> supplier) {
this.list = supplier.get();
}
public ExtendsAbstractList(Collection<E> source) {
this();
addAll(source);
}
public boolean add(E element) {
return list.add(element);
}
public E get(int index) {
return list.get(index);
}
public boolean remove(Object element) {
return list.remove(element);
}
public E set(int index, E element) {
return list.set(index, element);
}
public void add(int index, E element) {
list.add(index, element);
}
public E remove(int index) {
return list.remove(index);
}
public Iterator<E> iterator() {
return new Iterator<E>() {
Iterator<E> source = list.iterator();
public boolean hasNext() {
return source.hasNext();
}
public E next() {
return source.next();
}
public void remove() {
source.remove();
}
};
}
public int size() {
return list.size();
}
}

View file

@ -0,0 +1,85 @@
/*
* Copyright (c) 2012, 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.
*/
import java.util.HashSet;
import java.util.AbstractSet;
import java.util.Collection;
import java.util.Iterator;
import java.util.Set;
import java.util.function.Supplier;
/**
* @library
*
* A simple mutable set implementation that provides only default
* implementations of all methods. ie. none of the Set interface default methods
* have overridden implementations.
*
* @param <E> type of set members
*/
public class ExtendsAbstractSet<E> extends AbstractSet<E> {
protected final Set<E> set;
public ExtendsAbstractSet() {
this(HashSet<E>::new);
}
public ExtendsAbstractSet(Collection<E> source) {
this();
addAll(source);
}
protected ExtendsAbstractSet(Supplier<Set<E>> backer) {
this.set = backer.get();
}
public boolean add(E element) {
return set.add(element);
}
public boolean remove(Object element) {
return set.remove(element);
}
public Iterator<E> iterator() {
return new Iterator<E>() {
Iterator<E> source = set.iterator();
public boolean hasNext() {
return source.hasNext();
}
public E next() {
return source.next();
}
public void remove() {
source.remove();
}
};
}
public int size() {
return set.size();
}
}