Skip to content

Commit 4ce410a

Browse files
committed
fix(p1a): pin the ADO factory match to an ADO receiver, not just the return type
The ADO branch matched on method name + the RETURN type's System.Data interface only, so a non-ADO helper exposing an `ExecuteReader`/`CreateCommand`/ `BeginTransaction` that returns a borrowed/cached IDataReader/IDbCommand/ IDbTransaction would be minted as an owned acquire -> a false OWN001 when the caller correctly does not dispose the borrowed object. (Codex P2.) Pin BOTH ends, like every other factory branch verifies its declaring type: the RECEIVER (containing type) must implement the matching System.Data interface too -- IDbCommand for ExecuteReader, IDbConnection for CreateCommand/BeginTransaction -- in addition to the return-type interface. Still provider-agnostic (DbCommand / SqlCommand / NpgsqlCommand all implement IDbCommand). The corpus fixture is unaffected (DbCommand implements IDbCommand): case.own -> OWN001 still holds (corpus 21/21), full suite green, mypy --strict, ruff. C# branch verified by CI (golden C# / leak extractor / corpus benchmark). Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_019846YSZ35c7CdkWQ1qX5gm
1 parent 963af37 commit 4ce410a

2 files changed

Lines changed: 26 additions & 15 deletions

File tree

  • corpus/real-world/ado-executereader-leak
  • frontend/roslyn/OwnSharp.Extractor

‎corpus/real-world/ado-executereader-leak/notes.md‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@ until finalization — the single most common real-world ADO.NET resource leak.
99
- **after.cs** — `using var reader = …` disposes it on every path → clean.
1010

1111
Recognised by the extractor's `IsOwningFactory` (P1a, ADO.NET tranche): matched by method
12-
name + the resolved return type implementing `System.Data.IDataReader`, so it covers every
12+
name + **both** the receiver and the return type implementing the `System.Data` contract
13+
interfaces — the receiver an `IDbCommand` and the return an `IDataReader` — so it covers every
1314
provider (`SqlDataReader`, `NpgsqlDataReader`, …), the abstract `DbDataReader`, and the
14-
interface. Sibling members `CreateCommand` (→ `IDbCommand`) and `BeginTransaction`
15-
(→ `IDbTransaction`) are recognised the same way.
15+
interface, while a non-ADO helper that merely exposes an `ExecuteReader` returning a borrowed
16+
reader is not mistaken for an owned factory. Sibling members `CreateCommand`
17+
(`IDbConnection` → `IDbCommand`) and `BeginTransaction` (`IDbConnection` → `IDbTransaction`)
18+
are recognised the same way.

‎frontend/roslyn/OwnSharp.Extractor/Program.cs‎

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2300,21 +2300,29 @@ static bool IsOwningFactory(ExpressionSyntax? e, SemanticModel model)
23002300
&& !AnyDisposableArgument(i, model))
23012301
return true;
23022302
// ADO.NET owned-returning members — the canonical real-world disposable leak. These are
2303-
// INSTANCE methods on a connection/command/transaction, but the acquire path (op="acquire")
2304-
// does not care static-vs-instance, and the receiver is not an argument so it is never
2305-
// dropped. Provider types vary (SqlCommand / NpgsqlCommand / SqliteCommand / ...), so match
2306-
// by method name + the RESOLVED return type implementing the System.Data contract interface,
2307-
// which covers every provider, the abstract base (DbDataReader/DbCommand/DbTransaction), and
2308-
// the interface itself:
2309-
// * ExecuteReader() -> a DbDataReader the caller must dispose (also frees the cursor)
2310-
// * CreateCommand() -> a DbCommand the caller must dispose
2311-
// * BeginTransaction() -> a DbTransaction the caller must dispose
2303+
// INSTANCE methods on a connection/command, but the acquire path (op="acquire") does not care
2304+
// static-vs-instance, and the receiver is not an argument so it is never dropped. Provider
2305+
// types vary (SqlCommand / NpgsqlCommand / SqliteCommand / ...), so match by method name + the
2306+
// RESOLVED types implementing the System.Data contract interfaces, which covers every provider,
2307+
// the abstract base (DbDataReader/DbCommand/DbTransaction), and the interface itself. BOTH the
2308+
// RECEIVER and the RETURN are pinned (like every other factory branch verifies its declaring
2309+
// type), so a non-ADO helper that merely exposes an `ExecuteReader` returning a borrowed/cached
2310+
// IDataReader is NOT mistaken for an owned factory (Codex):
2311+
// * IDbCommand.ExecuteReader() -> a DbDataReader the caller must dispose (frees the cursor)
2312+
// * IDbConnection.CreateCommand() -> a DbCommand the caller must dispose
2313+
// * IDbConnection.BeginTransaction() -> a DbTransaction the caller must dispose
23122314
// Arguments are non-disposable (CommandBehavior / IsolationLevel enums); the guard keeps any
23132315
// odd overload from dropping a disposable input.
23142316
if (!AnyDisposableArgument(i, model)
2315-
&& ((sym.Name == "ExecuteReader" && ImplementsSystemDataInterface(sym.ReturnType, "IDataReader"))
2316-
|| (sym.Name == "CreateCommand" && ImplementsSystemDataInterface(sym.ReturnType, "IDbCommand"))
2317-
|| (sym.Name == "BeginTransaction" && ImplementsSystemDataInterface(sym.ReturnType, "IDbTransaction"))))
2317+
&& ((sym.Name == "ExecuteReader"
2318+
&& ImplementsSystemDataInterface(sym.ContainingType, "IDbCommand")
2319+
&& ImplementsSystemDataInterface(sym.ReturnType, "IDataReader"))
2320+
|| (sym.Name == "CreateCommand"
2321+
&& ImplementsSystemDataInterface(sym.ContainingType, "IDbConnection")
2322+
&& ImplementsSystemDataInterface(sym.ReturnType, "IDbCommand"))
2323+
|| (sym.Name == "BeginTransaction"
2324+
&& ImplementsSystemDataInterface(sym.ContainingType, "IDbConnection")
2325+
&& ImplementsSystemDataInterface(sym.ReturnType, "IDbTransaction"))))
23182326
return true;
23192327
return false;
23202328
}

0 commit comments

Comments
 (0)