[Code scan] RDF helpers mutate sel_type and poison later default calls
bugreproduced
This issue is part of a Codex global repository code scan.
The RDF functions use mutable `sel_type=[None, None]` defaults and `_compute_rdf_1frame()` rewrites the list in place. A default call can therefore change `compute_rdf.__defaults__`, causing later default calls on a different system to reuse stale atom types and return invalid values.
Affected code:
https://github.com/deepmodeling/dpdata/blob/a7a50bfc096e1a65470af6f1c48152dc0275ec1c/dpdata/md/rdf.py#L43-L50
https://github.com/deepmodeling/dpdata/blob/a7a50bfc096e1a65470af6f1c48152dc0275ec1c/dpdata/md/rdf.py#L61-L70
Minimal reproducer:
```python
import numpy as np
from dpdata.md import rdf as rdfmod
box = np.eye(3).reshape(1, 3, 3) * 10
pos = np.array([[[0.0, 0.0, 0.0], [1.0, 0.0, 0.0]]])
rdfmod.compute_rdf(box, pos, np.array([0, 1]))
print(rdfmod.compute_rdf.__defaults__)
_, stat, _ = rdfmod.compute_rdf(box, pos, np.array([2, 2]))
print(np.isfinite(stat).all(), stat[:3])
```
Current behavior:
```text
([[np.int64(0), np.int64(1)], [np.int64(0), np.int64(1)]], 5, 100)
False [nan nan nan]
```
The function should not mutate either its default object or a caller-provided `sel_type` list. A common fix is to default to `None` and construct a local normalized selection list per call/frame.
关闭于 2026-07-22 0 条评论