|
| 1 | +# ArrayPool over-read — the `.Length` spelling (POOL005) |
| 2 | + |
| 3 | +**Pattern:** a pooled array from `ArrayPool<T>.Shared.Rent(n)` is *oversized* |
| 4 | +(`Length >= n`). A view that uses `buf.Length` — the oversized backing length — as |
| 5 | +its bound (`buf.AsSpan(0, buf.Length)`, `new Span<T>(buf, 0, buf.Length)`) spans the |
| 6 | +whole array, so reading or copying through it processes the `n` valid bytes **plus** |
| 7 | +the stale `[n, Length)` tail a previous renter left: a wrong-length read and a |
| 8 | +potential information disclosure. The fix is to bound by the logical length: |
| 9 | +`buf.AsSpan(0, n)`. |
| 10 | + |
| 11 | +This is the `.Length` sibling of `arraypool-fullspan-overread` (which catches the |
| 12 | +unbounded `buf.AsSpan()`): same OWN025, a different way of writing the oversized |
| 13 | +length. The extractor recognises the `.Length`-as-length argument on the buffer's |
| 14 | +own view (start `0`, resolved BCL symbols) and emits an `overspan` fact. |
| 15 | + |
| 16 | +**Not flagged: the over-clear.** `Array.Clear(buf, 0, buf.Length)` (and the single-arg |
| 17 | +`Array.Clear(buf)`) only *overwrite* the pooled tail with zeros — a safe |
| 18 | +clear-before-`Return` idiom that exposes nothing. POOL005 is about *reading/copying* |
| 19 | +too much, not *writing* too much (Codex review); only the **view** is flagged. |
| 20 | + |
| 21 | +**What the checker says:** **OWN025** `[resource: pooled buffer]` at the view. The |
| 22 | +buffer is still `Return`ed, so there is no OWN001 leak. |
| 23 | + |
| 24 | +**Honesty / scope.** `case.own` is a faithful hand reduction (not C# ingested by the |
| 25 | +checker); `before.cs` / `after.cs` are representative of the bug and its fix. |
| 26 | + |
| 27 | +Reference: [P-007](../../../docs/proposals/P-007-arraypool-span.md); replay target |
| 28 | +AiDotNet.Tensors pooled-buffer over-read. |
0 commit comments