numpy/read-uninitialized
| Pack | Severity | Fix | Versions |
|---|---|---|---|
| NumPy | error | unsafe, with xpd check --fix --unsafe-fixes |
All versions |
np.empty leaves the values unset, so computing with them uses whatever bytes were in memory. Use np.zeros or fill the array before you read it.
total = np.empty(n) + offsettotal = np.zeros(n) + offsetWhat the rule reports
Section titled “What the rule reports”The rule reports np.empty(n).sum() and np.empty_like(x) + 1, which compute with whatever bytes were in memory.
The rule leaves np.empty(n) * np.nan alone, because it gives NaN everywhere.
An explicit zero dimension is also left alone because the array has no values to read.
How to fix it
Section titled “How to fix it”xpd check --fix --unsafe-fixes applies the fixes of this rule. Check the result, because an unsafe fix can change what the code does.
The unsafe fix writes np.zeros(n) and np.zeros_like(a) with the same arguments, so the code computes on zeros.
Diagnostic message
Section titled “Diagnostic message”$F leaves the values unset, so this computes with whatever bytes were in memory. Write $Z($$$ARGS), which starts from zeros.See all rules in the NumPy pack.