Skip to content

Commit dfb0835

Browse files
committed
extractor: treat StringWriter/StringReader fields as dispose-optional
A StringWriter is StringBuilder-backed and a StringReader reads a string — neither holds an OS handle or unmanaged resource, so Dispose() frees nothing real and an undisposed owning field of these is not a leak. Add System.IO StringWriter/StringReader to IsDisposeOptional (the same lever already used for Task/ValueTask/DataTable/DataSet/ DataView), so the owned-field detector stays silent on them. MemoryStream is deliberately NOT exempted — it can own a real buffer and still warns. Surfaced by the oracle on Newtonsoft.Json (field-notes #8): TraceJsonReader/TraceJsonWriter own undisposed StringWriter/JsonTextWriter fields; CodeQL/Infer# correctly stay silent on the StringWriter (not a real resource), Own.NET over-reported. Adds a HolderWithStringWriter control to ResolvedDisposableSample + a CI assertion it stays silent. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_011YMngnax1jsy378fSAZb9r
1 parent 7ab1009 commit dfb0835

3 files changed

Lines changed: 27 additions & 1 deletion

File tree

‎.github/workflows/ci.yml‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,12 @@ jobs:
257257
if echo "$out" | grep -q "HolderWithDisposeOptional"; then
258258
echo "FAIL: a dispose-optional (Task/DataTable) field was wrongly flagged"; exit 1
259259
fi
260+
# the same rule for string-backed reader/writer fields (field-notes #8, Newtonsoft
261+
# TraceJsonReader/Writer): a new'd, undisposed StringWriter/StringReader holds no
262+
# unmanaged resource -> must stay SILENT (IsDisposeOptional, System.IO).
263+
if echo "$out" | grep -q "HolderWithStringWriter"; then
264+
echo "FAIL: a dispose-optional (StringWriter/StringReader) field was wrongly flagged"; exit 1
265+
fi
260266
# field release recognition (mined: ImageSharp). #2 null-conditional dispose
261267
# `field?.Dispose()` must be recognized -> silent; the undisposed control still warns.
262268
if echo "$out" | grep -q "DisposesViaConditional"; then

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -714,7 +714,14 @@ static bool IsDisposeOptional(ITypeSymbol t)
714714
{
715715
var ns = t.ContainingNamespace?.ToString();
716716
return (ns == "System.Threading.Tasks" && t.Name is "Task" or "ValueTask")
717-
|| (ns == "System.Data" && t.Name is "DataTable" or "DataSet" or "DataView");
717+
|| (ns == "System.Data" && t.Name is "DataTable" or "DataSet" or "DataView")
718+
// System.IO string-backed reader/writer: a StringWriter wraps a StringBuilder
719+
// and a StringReader reads a string — neither holds an OS handle / unmanaged
720+
// resource, so Dispose() frees nothing real and an undisposed one is not a leak.
721+
// (field-notes #8, mined on Newtonsoft.Json's TraceJsonReader/TraceJsonWriter,
722+
// where these are owning fields the trace helpers never dispose.) MemoryStream is
723+
// deliberately NOT here — it can own a real buffer, so it still warns.
724+
|| (ns == "System.IO" && t.Name is "StringWriter" or "StringReader");
718725
}
719726

720727
// A type that is System.Windows.Forms.Form or derives from it (semantic, walks the

‎frontend/roslyn/samples/ResolvedDisposableSample.cs‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,16 @@ public sealed class HolderWithDisposeOptional
5252

5353
public int Use() => this.task.Id + this.table.Columns.Count;
5454
}
55+
56+
// String-backed reader/writer dispose-optional control (field-notes #8, mined on
57+
// Newtonsoft.Json's TraceJsonReader/TraceJsonWriter): a StringWriter wraps a
58+
// StringBuilder and a StringReader reads a string — no unmanaged resource, so Dispose()
59+
// frees nothing real. A new'd, undisposed field of these must stay SILENT (IsDisposeOptional).
60+
// Contrast HolderWithRealDisposable's MemoryStream above, which still warns.
61+
public sealed class HolderWithStringWriter
62+
{
63+
private readonly StringWriter writer = new();
64+
private readonly StringReader reader = new("x");
65+
66+
public string Use() => this.writer.ToString() + this.reader.ReadToEnd();
67+
}

0 commit comments

Comments
 (0)