# PyTorch Geometric — CWE-407 Disclosure Brief
**2026-03-27 · Patch available — awaiting upstream merge**

## Finding

One O(n²) defect in PyTorch Geometric's SMILES molecular graph converter. `from_rdmol()` calls `x_map[key].index(val)` 9 times per atom and 3 times per bond — O(L) list scans — resulting in 491 million list traversals for the QM9 dataset. Patch ready for upstream review.

## The Defects

**pyg-0001 (PATCHED — HIGH):** `torch_geometric/utils/smiles.py:96–118`

```python
# from_rdmol() — per atom (9 calls) and per bond (3 calls):
atomic_num_idx = x_map['atomic_num'].index(atom.GetAtomicNum())
# x_map['atomic_num'] is a 119-element list — O(119) per atom
# Called 9× per atom: O(9 × 119) = O(1071) per atom
# For QM9 dataset: 130k molecules × 18 atoms × O(M×A×L)
```

`x_map[key].index(val)` performs O(L) list scan where L is up to 119 elements. Called 9× per atom and 3× per bond. For M=130,000 molecules, A=18 atoms, L=119 list length: **491 million list traversals** for QM9. **Measured ratio: 8×.**

## Complexity Proof

For the QM9 benchmark (130k molecules, 18 atoms/molecule, L=119):
- Per molecule: 9×A×L = 9×18×119 = 19,278 list comparisons
- Per dataset: 130,000 × 19,278 = 2.5 billion ops (atoms only)
- Fixed: pre-built `x_idx`/`e_idx` dicts → 9×A = 162 dict lookups per molecule
- **8× measured ratio on benchmark.**

## Impact

All PyTorch Geometric users processing SMILES/RDKit molecular data — drug discovery, molecular property prediction, graph neural networks for chemistry. `from_rdmol()` is called once per molecule during dataset preprocessing. QM9 (130k molecules) and larger drug discovery datasets (millions of molecules) hit the worst case. PyTorch Geometric is the leading Python library for graph neural networks; chemistry/drug discovery is a primary use case.

## The Fix

Pre-build `x_idx` and `e_idx` dicts before the per-atom loop:

```python
# Before
atomic_num_idx = x_map['atomic_num'].index(atom.GetAtomicNum())  # O(L) per atom

# After
# CWE-407 fix: pre-built dicts for O(1) lookup instead of O(L) list.index() scan.
x_idx = {key: {v: i for i, v in enumerate(vals)} for key, vals in x_map.items()}
e_idx = {key: {v: i for i, v in enumerate(vals)} for key, vals in e_map.items()}

atomic_num_idx = x_idx['atomic_num'][atom.GetAtomicNum()]  # O(1)
```

## Patch

`defects/pytorch-geometric/patch/pyg-0001-smiles-index-dict.patch`

## What We Ask

1. Confirm receipt and assign a GitHub Security Advisory or issue reference.
2. Validate the patch against your SMILES and molecular graph test suite.
3. Assess CVE eligibility — 8× overhead per molecule across entire dataset preprocessing.
4. Coordinate a disclosure date — we are targeting 90 days from first contact.

Contact: see cover email. This brief is confidential until coordinated disclosure.
