java-topology/defects/clickhouse/unit/ClickHouseReplaceTransformerAlgorithm.java
russell@unturf.com 9934133dcf whitepaper: 312 sites / 151 ecosystems — wave2+3 defect tables and PDF rebuild
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
2026-03-27 15:23:43 -04:00

95 lines
3.5 KiB
Java

package unit;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Unit test for CWE-407 defect in ClickHouse ReplaceColumnTransformerNode::findReplacementExpression.
*
* Defect: replacements_names is a std::vector<std::string>. findReplacementExpression() does
* std::find (O(n)) over it. It is called inside a double loop:
* for each column (C) → for each transformer (T) → findReplacementExpression() → O(R)
* Total: O(C * T * R). For wide-table queries with compound REPLACE lists this is measurable.
*
* Fix: add std::unordered_map<std::string, size_t> replacements_index alongside replacements_names.
* findReplacementExpression() becomes O(1) via map lookup.
*
* This test models slow (list scan) vs fast (map lookup) and asserts
* slow ops > fast ops * 10x at N=200 replacements, 500 column lookups.
*/
public class ClickHouseReplaceTransformerAlgorithm {
// -----------------------------------------------------------------------
// slow(): models std::find scan over Names (vector<string>).
// Returns total comparison ops.
// -----------------------------------------------------------------------
static Result slow(int numReplacements, int numLookups) {
List<String> names = new ArrayList<>();
for (int i = 0; i < numReplacements; i++) {
names.add("col_" + i);
}
long ops = 0;
// Simulate findReplacementExpression called numLookups times,
// always looking for the last element (worst case).
String target = "col_" + (numReplacements - 1);
for (int q = 0; q < numLookups; q++) {
for (int j = 0; j < names.size(); j++) {
ops++;
if (names.get(j).equals(target)) {
break;
}
}
}
return new Result(ops);
}
// -----------------------------------------------------------------------
// fast(): models unordered_map<string, size_t> lookup → O(1).
// Returns total lookup ops (1 per call).
// -----------------------------------------------------------------------
static Result fast(int numReplacements, int numLookups) {
Map<String, Integer> index = new HashMap<>();
for (int i = 0; i < numReplacements; i++) {
index.put("col_" + i, i);
}
long ops = 0;
String target = "col_" + (numReplacements - 1);
for (int q = 0; q < numLookups; q++) {
ops++; // O(1) hash lookup
@SuppressWarnings("unused")
Integer idx = index.get(target);
}
return new Result(ops);
}
// -----------------------------------------------------------------------
static class Result {
final long ops;
Result(long ops) { this.ops = ops; }
}
// -----------------------------------------------------------------------
public static void main(String[] args) {
int N = 200;
int lookups = 500;
int NX = 10;
Result s = slow(N, lookups);
Result f = fast(N, lookups);
System.out.printf("slow ops=%d fast ops=%d ratio=%.1fx%n",
s.ops, f.ops, (double) s.ops / f.ops);
if (s.ops <= f.ops * NX) {
System.out.printf("FAIL: expected slow(%d) > fast(%d) * %d%n", s.ops, f.ops, NX);
System.exit(1);
}
System.out.printf("1/1 PASS (slow=%d >> fast=%d, N=%d lookups=%d)%n",
s.ops, f.ops, N, lookups);
}
}