Add 88 new defect entries to HIGH and MEDIUM tables:
HIGH: mysql-0001/0002, mariadb-0001, redis-0001/0002, valkey-0001/0002, openvpn-0001,
vlc-0001, prometheus-0001, otel-collector-0001, cockroachdb-0001..0004,
tidb-0001..0008, kubernetes-0001/0002, go-0001, kotlin-0002, scala-0001,
allegro5-0001, sdl2-0001, grafana-0001, clickhouse-0001, duckdb-0001,
mongodb-0001, envoy-0001, istio-0001, cilium-0001, linkerd2-0001,
linux-0001/0002/0003, tor-0002/0003, curl-0001, julia-0001, lua-0001,
perl5-0001, nats-0001, spring-0003/0004, tomcat-0001, onos-0002, odl-0002
MEDIUM: helm-0001, mariadb-0002, openssl-0001/0002, memcached-0001,
cassandra-0001..0004, flink-0001, storm-0001/0002, zookeeper-0001..0003,
pip-0001, gradle-0001, nginx-0001, haproxy-0001, caddy-0001, varnish-0001,
ffmpeg-0001, gstreamer-0001, raylib-0001, love2d-0001, php-0001/0002,
r-source-0001, cpython-0002, ruby-0001, rabbitmq-0003/0004, activemq-0001,
ovs-0001, onos-0003, odl-0002, jetty-0001
PDF: 976K
114 lines
3.9 KiB
Java
114 lines
3.9 KiB
Java
package unit;
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.HashMap;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
|
|
/**
|
|
* Love2dJoystickLookupTest — CWE-407 love2d-0001
|
|
*
|
|
* Models JoystickModule::getJoystickFromID(int instanceid):
|
|
* slow() = O(n) linear scan of activeSticks vector (current defect)
|
|
* fast() = O(1) HashMap lookup keyed on instanceID (patch)
|
|
*
|
|
* Assert: slowOps > fastOps * Nx at N=64 joysticks.
|
|
*/
|
|
public class Love2dJoystickLookupTest {
|
|
|
|
static class Joystick {
|
|
final int instanceID;
|
|
Joystick(int id) { this.instanceID = id; }
|
|
int getInstanceID() { return instanceID; }
|
|
}
|
|
|
|
static long slowOps;
|
|
static long fastOps;
|
|
|
|
/**
|
|
* slow: O(n) linear scan — models love2d getJoystickFromID defect.
|
|
*/
|
|
static Joystick getJoystickFromIDSlow(List<Joystick> activeSticks, int instanceid) {
|
|
for (Joystick stick : activeSticks) {
|
|
slowOps++;
|
|
if (stick.getInstanceID() == instanceid)
|
|
return stick;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
/**
|
|
* fast: O(1) hash map lookup — models patched getJoystickFromID.
|
|
*/
|
|
static Joystick getJoystickFromIDFast(Map<Integer, Joystick> sticksById, int instanceid) {
|
|
fastOps++;
|
|
return sticksById.get(instanceid);
|
|
}
|
|
|
|
public static void main(String[] args) {
|
|
final int N = 64; // number of active joysticks
|
|
final int NX = 5; // minimum required speedup factor
|
|
final int EVENTS = 5000; // SDL events dispatched per measurement
|
|
|
|
// Build joystick list (instanceIDs 1..N)
|
|
List<Joystick> activeSticks = new ArrayList<>();
|
|
Map<Integer, Joystick> sticksById = new HashMap<>();
|
|
for (int i = 1; i <= N; i++) {
|
|
Joystick j = new Joystick(i);
|
|
activeSticks.add(j);
|
|
sticksById.put(i, j);
|
|
}
|
|
|
|
// Worst-case: always look up the last joystick (forces full scan for slow)
|
|
int targetID = N;
|
|
|
|
slowOps = 0;
|
|
fastOps = 0;
|
|
|
|
// Each event calls getJoystickFromID once (7 event types in love2d, simulate 1)
|
|
for (int e = 0; e < EVENTS; e++) {
|
|
getJoystickFromIDSlow(activeSticks, targetID);
|
|
}
|
|
long totalSlowOps = slowOps;
|
|
|
|
for (int e = 0; e < EVENTS; e++) {
|
|
getJoystickFromIDFast(sticksById, targetID);
|
|
}
|
|
long totalFastOps = fastOps;
|
|
|
|
// Correctness check
|
|
Joystick slowResult = getJoystickFromIDSlow(activeSticks, targetID);
|
|
Joystick fastResult = getJoystickFromIDFast(sticksById, targetID);
|
|
boolean correctnessOk = (slowResult != null && fastResult != null
|
|
&& slowResult.getInstanceID() == fastResult.getInstanceID());
|
|
|
|
boolean speedupOk = totalSlowOps > totalFastOps * NX;
|
|
|
|
System.out.printf("N=%d joysticks, target instanceID=%d, EVENTS=%d%n", N, targetID, EVENTS);
|
|
System.out.printf("slow (linear) ops: %d%n", totalSlowOps);
|
|
System.out.printf("fast (hashmap) ops: %d%n", totalFastOps);
|
|
System.out.printf("speedup ratio: %.1fx (required >%dx)%n",
|
|
(double) totalSlowOps / totalFastOps, NX);
|
|
|
|
int passed = 0, total = 2;
|
|
if (correctnessOk) {
|
|
System.out.println("1/2 PASS correctness: both return same joystick");
|
|
passed++;
|
|
} else {
|
|
System.out.printf("1/2 FAIL correctness: slow=%s fast=%s%n",
|
|
slowResult == null ? "null" : slowResult.getInstanceID(),
|
|
fastResult == null ? "null" : fastResult.getInstanceID());
|
|
}
|
|
if (speedupOk) {
|
|
System.out.printf("2/2 PASS speedup: %d > %d * %d%n",
|
|
totalSlowOps, totalFastOps, NX);
|
|
passed++;
|
|
} else {
|
|
System.out.printf("2/2 FAIL speedup: %d not > %d * %d%n",
|
|
totalSlowOps, totalFastOps, NX);
|
|
}
|
|
|
|
System.out.printf("%d/%d PASS%n", passed, total);
|
|
if (passed < total) System.exit(1);
|
|
}
|
|
}
|