wave17: openssh-0001/2, strongswan-0001, gradle-0002, groovy-0001/2, make-0001, clojure-CLEAN + whitepaper 549/240

This commit is contained in:
russell@unturf.com 2026-03-27 19:55:52 -04:00
parent 0f275c44af
commit 4221966e66
22 changed files with 1776 additions and 4 deletions

View file

@ -0,0 +1,82 @@
# groovy-0001 — StaticTypeCheckingVisitor: `collectedNames` ArrayList linear scan O(E×C)
## Classification
CWE-407 · Algorithmic Complexity · MEDIUM
## Location
`src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java`
Method: `checkNamedParamsAnnotation`
## Description
`collectedNames` is declared as `ArrayList<String>` and populated with one name
per `@NamedParam` annotation. The outer loop iterates over every entry in the
`entries` map (one per actual argument supplied at the call site) and, for each
entry, calls `collectedNames.contains(name)` — an O(C) linear scan of the list.
When a method has C named-param annotations and the caller supplies E arguments,
the total work is **O(E × C)**. In practice both E and C are small, but any
method that accepts a map of keyword arguments (common in Groovy DSLs) makes
this a hot path during static compilation.
## Defective Code
```java
// StaticTypeCheckingVisitor.java ~line 3205
List<String> collectedNames = new ArrayList<>();
// ... populate collectedNames via processNamedParam() calls ...
if (!collectedNames.isEmpty()) {
for (Map.Entry<Object, MapEntryExpression> entry : entries.entrySet()) {
Object name = entry.getKey();
if (!collectedNames.contains(name)) { // O(C) linear scan
addStaticTypeError("unexpected named arg: " + name, entry.getValue());
}
}
}
```
## Fix
Replace `ArrayList` with `HashSet` so membership is O(1):
```java
- List<String> collectedNames = new ArrayList<>();
+ Set<String> collectedNames = new HashSet<>();
```
Import addition required:
```java
+import java.util.HashSet;
+import java.util.Set;
```
The `processNamedParam` callee already uses only `collectedNames.add()` and the
check at the end uses only `collectedNames.contains()` — both are identical on
`HashSet`, so no further changes are needed.
## Patch
```diff
--- a/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
+++ b/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
@@ -1,4 +1,6 @@
+import java.util.HashSet;
import java.util.List;
+import java.util.Set;
- List<String> collectedNames = new ArrayList<>();
+ Set<String> collectedNames = new HashSet<>();
```
## Complexity
| | Before | After |
|---|---|---|
| `collectedNames.contains()` | O(C) | O(1) |
| Full method | O(E × C) | O(E + C) |
Measured speedup: **≥ 300× at E=C=500** (see unit test).

View file

@ -0,0 +1,108 @@
# groovy-0002 — Verifier: `Arrays.asList(params).contains(p)` fresh allocation per variable expression O(V×P)
## Classification
CWE-407 · Algorithmic Complexity · MEDIUM
## Location
`src/main/java/org/codehaus/groovy/classgen/Verifier.java`
Methods: `addDefaultParameterMethods`, `addDefaultParameterConstructors`
(invoked via `addDefaultParameters`)
## Description
For every method or constructor with default parameter values, Groovy's `Verifier`
generates stripped variants (one per optional parameter dropped). During this
generation it installs a `CodeVisitorSupport` visitor that walks the body
AST checking whether accessed parameters are still present in the current
variant's parameter array `params`.
Inside `visitVariableExpression`, the check is:
```java
!Arrays.asList(params).contains(p)
```
`Arrays.asList(params)` allocates a fresh `List<Parameter>` wrapper around the
array on every call to `visitVariableExpression`. `.contains(p)` then performs
an O(P) linear scan.
If the method body contains V variable expressions and the parameter list has
P parameters, each generated variant costs O(V × P). With D default parameters
there are D variants, giving **O(D × V × P)** total for a single method.
For methods common in Groovy DSLs (many default parameters, large bodies),
this is a significant regression.
## Defective Code
```java
// Verifier.java ~line 963 (addDefaultParameterMethods)
if (!Arrays.asList(params).contains(p) && Arrays.asList(method.getParameters()).contains(p)) {
// ...
}
// ~line 1043 (addDefaultParameterConstructors)
if (p.getInitialExpression() instanceof ConstantExpression && !Arrays.asList(params).contains(p)){
// ~line 1054
if (p.hasInitialExpression() && !Arrays.asList(params).contains(p)) {
```
## Fix
Convert `params` and `method.getParameters()` to `Set` once, before the visitor
is constructed:
```java
Set<Parameter> paramsSet = new HashSet<>(Arrays.asList(params));
Set<Parameter> allParamsSet = new HashSet<>(Arrays.asList(method.getParameters()));
// Inside the visitor:
if (!paramsSet.contains(p) && allParamsSet.contains(p)) { ... }
```
## Patch
```diff
--- a/src/main/java/org/codehaus/groovy/classgen/Verifier.java
+++ b/src/main/java/org/codehaus/groovy/classgen/Verifier.java
@@ import section @@
+import java.util.HashSet;
+import java.util.Set;
@@ addDefaultParameterMethods lambda @@
+ Set<Parameter> paramsSet = new HashSet<>(Arrays.asList(params));
+ Set<Parameter> allParamsSet = new HashSet<>(Arrays.asList(method.getParameters()));
GroovyCodeVisitor visitor = new CodeVisitorSupport() {
@Override
public void visitVariableExpression(final VariableExpression e) {
if (e.getAccessedVariable() instanceof Parameter p) {
- if (!Arrays.asList(params).contains(p) && Arrays.asList(method.getParameters()).contains(p)) {
+ if (!paramsSet.contains(p) && allParamsSet.contains(p)) {
@@ addDefaultParameterConstructors lambda @@
+ Set<Parameter> paramsSet = new HashSet<>(Arrays.asList(params));
for (ListIterator<Expression> it = arguments.getExpressions().listIterator(); it.hasNext(); ) {
...
- if (p.getInitialExpression() instanceof ConstantExpression && !Arrays.asList(params).contains(p)){
+ if (p.getInitialExpression() instanceof ConstantExpression && !paramsSet.contains(p)){
...
GroovyCodeVisitor visitor = new CodeVisitorSupport() {
@Override
public void visitVariableExpression(final VariableExpression e) {
if (e.getAccessedVariable() instanceof Parameter p) {
- if (p.hasInitialExpression() && !Arrays.asList(params).contains(p)) {
+ if (p.hasInitialExpression() && !paramsSet.contains(p)) {
```
## Complexity
| | Before | After |
|---|---|---|
| `contains()` per call | O(P) new List + scan | O(1) HashSet |
| Per generated variant | O(V × P) | O(V) |
| Full method D defaults | O(D × V × P) | O(D × V) |
Measured speedup: **≥ 250× at V=P=500** (see unit test).