Skip to content

Commit 5a9f22f

Browse files
committed
fix(pool): POOL005 is a VIEW over-read only — drop Array.Clear, require zero start
Review feedback on #72 (Codex + CodeRabbit), all precision tightening on an already-green PR: 1. Codex: do not flag Array.Clear(buf, 0, buf.Length). It is a WRITE/wipe — it only zeros the pooled tail (a safe clear-before-Return idiom) and exposes nothing; the over-read bug is READING/COPYING the tail through a view. Removed ArrayClearOverOwner entirely; EmitOverspans now flags only read-capable VIEWS (FullViewOwner). Renamed the corpus case arraypool-clear-past-length -> arraypool-length-overread and made it the `.Length` VIEW over-read (buf.AsSpan(0, buf.Length)); after.cs bounds by n. 2. CodeRabbit: the `.Length` view match now also requires the START/index to be the constant 0 (IsZeroInt via the SemanticModel) — buf.AsSpan(k, buf.Length) for k != 0 is out of range, not this pattern. Applies to AsSpan/AsMemory(0, buf.Length) and new Span<T>(buf, 0, buf.Length). 3. CodeRabbit (spec nitpick): B9 now notes the oversize principle covers MemoryPool too (IMemoryOwner.Memory wraps an oversized backing via .Memory.Length), and that a write/wipe is not flagged. NOT changed: --min-recall stays 16. CodeRabbit flagged it as "impossible (corpus is 14)" but that miscounts — the benchmark scores corpus/real-world (14) AND corpus/wpf (4) = 18 total, and --min-recall is the CAUGHT floor, not the corpus size. #72's benchmark is green at 16 (16 caught of 18); the `.Length` VIEW form still catches arraypool-length- overread after dropping Array.Clear. spec/P-007/ci.yml comments updated to match. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Rg8kSk1YT14x7A1vo5zgED
1 parent b3bf5b1 commit 5a9f22f

13 files changed

Lines changed: 141 additions & 150 deletions

File tree

‎.github/workflows/ci.yml‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -789,9 +789,10 @@ jobs:
789789
# after `Return(buf)` trips OWN002 — including RETURNING a `Memory<T>` view (which, unlike a
790790
# ref-struct `Span`, can ESCAPE the method), a dangling borrow handed to the caller. A
791791
# view of a pooled buffer reaches past the rented length into the oversized tail -> OWN025
792-
# (P-007 POOL005, the over-read): the unbounded `buf.AsSpan()` AND the `.Length` spelling
793-
# (`buf.AsSpan(0, buf.Length)`, `Array.Clear(buf, 0, buf.Length)`) — the `arraypool-fullspan-
794-
# overread` / `arraypool-clear-past-length` cases. MemoryPool is now tracked too: a
792+
# (P-007 POOL005, the over-read): the unbounded `buf.AsSpan()` AND the `.Length` view spelling
793+
# (`buf.AsSpan(0, buf.Length)`) — the `arraypool-fullspan-overread` / `arraypool-length-
794+
# overread` cases (a write/wipe like `Array.Clear(buf, 0, buf.Length)` is not flagged).
795+
# MemoryPool is now tracked too: a
795796
# `MemoryPool<T>.Rent` IMemoryOwner is released by Dispose, so its leak / double-dispose /
796797
# use-after-dispose ride the flow (POOL001/002/003 — the `memorypool-double-dispose` case ->
797798
# OWN003). Remaining backlog: a FIELD-mediated cross-method use-after-dispose, a view stored in

‎corpus/real-world/arraypool-clear-past-length/after.cs‎

Lines changed: 0 additions & 21 deletions
This file was deleted.

‎corpus/real-world/arraypool-clear-past-length/before.cs‎

Lines changed: 0 additions & 28 deletions
This file was deleted.

‎corpus/real-world/arraypool-clear-past-length/case.own‎

Lines changed: 0 additions & 18 deletions
This file was deleted.

‎corpus/real-world/arraypool-clear-past-length/notes.md‎

Lines changed: 0 additions & 30 deletions
This file was deleted.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// AFTER (fixed). The view is bound to the logical length `n` — `buf.AsSpan(0, n)` —
2+
// so only the `n` valid payload bytes are read; the oversized `[n, Length)` tail is
3+
// never touched. No `.Length`-spelled view remains, so the extractor emits no
4+
// `overspan` fact and the checker is silent.
5+
using System;
6+
using System.Buffers;
7+
8+
static class PoolLengthOverread
9+
{
10+
static void Frame(int n)
11+
{
12+
byte[] buf = ArrayPool<byte>.Shared.Rent(n);
13+
Fill(buf, n);
14+
Emit(buf.AsSpan(0, n)); // bounded by the rented length n
15+
ArrayPool<byte>.Shared.Return(buf);
16+
}
17+
18+
static void Fill(byte[] b, int n) { for (int i = 0; i < n; i++) b[i] = (byte)i; }
19+
static void Emit(ReadOnlySpan<byte> data) { }
20+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// BEFORE (buggy). POOL005, the `.Length` spelling of the over-read. A rented array
2+
// is OVERSIZED: `ArrayPool<T>.Shared.Rent(n)` returns an array of `Length >= n`.
3+
// Viewing it with `buf.Length` — the oversized backing length — as the bound,
4+
// `buf.AsSpan(0, buf.Length)`, spans the WHOLE array, so a consumer reads the `n`
5+
// valid bytes PLUS the stale `[n, Length)` tail a previous renter left: a wrong-
6+
// length read and an information disclosure. The fix is `buf.AsSpan(0, n)` (after.cs).
7+
//
8+
// Note: an over-CLEAR like `Array.Clear(buf, 0, buf.Length)` is deliberately NOT
9+
// flagged — it only overwrites the pooled tail with zeros (a safe clear-before-Return
10+
// idiom) and exposes nothing. The bug is READING/COPYING the tail, which the view
11+
// spelling does.
12+
//
13+
// Wrapped in a class so the extractor's per-class flow pass visits it; helpers stubbed.
14+
using System;
15+
using System.Buffers;
16+
17+
static class PoolLengthOverread
18+
{
19+
static void Frame(int n)
20+
{
21+
byte[] buf = ArrayPool<byte>.Shared.Rent(n);
22+
Fill(buf, n); // valid payload is buf[0..n]
23+
Emit(buf.AsSpan(0, buf.Length)); // <-- BUG: length is buf.Length, not n -> reads the stale tail
24+
ArrayPool<byte>.Shared.Return(buf);
25+
}
26+
27+
static void Fill(byte[] b, int n) { for (int i = 0; i < n; i++) b[i] = (byte)i; }
28+
static void Emit(ReadOnlySpan<byte> data) { }
29+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// OwnLang model of the POOL005 `.Length` over-read. `acquire` == ArrayPool.Rent
2+
// (oversized: Length >= n), `release` == ArrayPool.Return. `overspan buf` models a
3+
// view whose length is the buffer's OWN oversized `.Length` (`buf.AsSpan(0, buf.Length)`),
4+
// which spans the whole array — a consumer reads past the rented `n` into the stale
5+
// tail. Bounding by `n` takes no full view, so the fix has no `overspan` and is silent.
6+
// -> OWN025. The buffer is still returned, so there is no leak (OWN001).
7+
module Corpus
8+
resource Buffer {
9+
acquire rent
10+
release give
11+
kind "pooled buffer"
12+
}
13+
fn frame(n: int) {
14+
let buf = acquire Buffer(n); // ArrayPool.Rent (Length >= n)
15+
overspan buf; // buf.AsSpan(0, buf.Length) — spans the whole array -> OWN025
16+
release buf; // ArrayPool.Return
17+
}

corpus/real-world/arraypool-clear-past-length/expected-diagnostics.txt renamed to corpus/real-world/arraypool-length-overread/expected-diagnostics.txt

File renamed without changes.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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

Comments
 (0)