Commit db331b4
authored
[mypyc] Fix reference leak in mypyc bytes concatenation (#21469)
Fixes [Mypyc #1192](mypyc/mypyc#1192)
Fix a reference leak in mypyc-generated code for `bytes + bytes`.
The `bytes + bytes` primitive for `CPyBytes_Concat` was marked as
stealing the
left operand with `steals=[True, False]`. However, `CPyBytes_Concat`
does not
steal or decref either argument; it allocates and returns a new `bytes`
object.
Because of this mismatch, the refcount pass skipped emitting a `dec_ref`
for
owned left operands. For code such as:
```python
return (1).to_bytes(4, "big") + (2).to_bytes(4, "big")
````
the generated IR decrefed only the right operand after
`CPyBytes_Concat`, leaving
the owned left operand alive. Chained concatenations amplified the leak
because
intermediate concat results could also become leaked left operands.
This PR removes the incorrect `steals=[True, False]` annotation from the
`bytes + bytes` primitive, so both operands are treated as non-stolen
and owned
operands are decrefed normally after the call.
The tests verify that the generated IR decrefs both owned operands after
`CPyBytes_Concat`.1 parent d62a508 commit db331b4
2 files changed
Lines changed: 28 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
64 | 64 | | |
65 | 65 | | |
66 | 66 | | |
67 | | - | |
68 | 67 | | |
69 | 68 | | |
70 | 69 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2125 | 2125 | | |
2126 | 2126 | | |
2127 | 2127 | | |
| 2128 | + | |
| 2129 | + | |
| 2130 | + | |
| 2131 | + | |
| 2132 | + | |
| 2133 | + | |
| 2134 | + | |
| 2135 | + | |
| 2136 | + | |
| 2137 | + | |
| 2138 | + | |
| 2139 | + | |
| 2140 | + | |
| 2141 | + | |
| 2142 | + | |
| 2143 | + | |
| 2144 | + | |
| 2145 | + | |
| 2146 | + | |
| 2147 | + | |
| 2148 | + | |
| 2149 | + | |
| 2150 | + | |
| 2151 | + | |
| 2152 | + | |
| 2153 | + | |
| 2154 | + | |
| 2155 | + | |
0 commit comments