|
| 1 | +using System; |
| 2 | +using System.IO.Pipelines; |
| 3 | +using System.Threading; |
| 4 | +using System.Threading.Tasks; |
| 5 | + |
| 6 | +// Mined false-positive regression guard (Pipelines.Sockets.Unofficial). `System.IO.Pipelines` |
| 7 | +// PipeReader/PipeWriter END WITH "Reader"/"Writer", so the field-disposable NAME heuristic used to |
| 8 | +// classify them as IDisposable and flag an undisposed PipeReader/PipeWriter FIELD as a leak. But they |
| 9 | +// are NOT IDisposable — they finish via Complete(), not Dispose(). A class that constructs such fields |
| 10 | +// and never disposes them must produce NO finding (mirrors SocketConnection._input/_output, which the |
| 11 | +// checker wrongly reported before IsNonDisposableReaderWriter excluded these types). |
| 12 | +public sealed class PipeHolder |
| 13 | +{ |
| 14 | + private readonly PipeReader _input; |
| 15 | + private readonly PipeWriter _output; |
| 16 | + |
| 17 | + public PipeHolder() |
| 18 | + { |
| 19 | + _input = new NullReader(); // constructed (a `new`) -> a disposable-field candidate before the fix |
| 20 | + _output = new NullWriter(); |
| 21 | + } |
| 22 | + |
| 23 | + // No Dispose: PipeReader/PipeWriter are completed by the consumer, not disposed -> not a leak. |
| 24 | +} |
| 25 | + |
| 26 | +// Minimal PipeReader/PipeWriter implementations so the sample is self-contained (the abstract BCL |
| 27 | +// types resolve from the framework reference set). PipeReader/PipeWriter expose Complete(), not |
| 28 | +// Dispose() — they are not IDisposable. |
| 29 | +internal sealed class NullReader : PipeReader |
| 30 | +{ |
| 31 | + public override void AdvanceTo(SequencePosition consumed) => throw new NotImplementedException(); |
| 32 | + public override void AdvanceTo(SequencePosition consumed, SequencePosition examined) => throw new NotImplementedException(); |
| 33 | + public override void CancelPendingRead() => throw new NotImplementedException(); |
| 34 | + public override void Complete(Exception exception = null) => throw new NotImplementedException(); |
| 35 | + public override ValueTask<ReadResult> ReadAsync(CancellationToken cancellationToken = default) => throw new NotImplementedException(); |
| 36 | + public override bool TryRead(out ReadResult result) => throw new NotImplementedException(); |
| 37 | +} |
| 38 | + |
| 39 | +internal sealed class NullWriter : PipeWriter |
| 40 | +{ |
| 41 | + public override void Advance(int bytes) => throw new NotImplementedException(); |
| 42 | + public override void CancelPendingFlush() => throw new NotImplementedException(); |
| 43 | + public override void Complete(Exception exception = null) => throw new NotImplementedException(); |
| 44 | + public override ValueTask<FlushResult> FlushAsync(CancellationToken cancellationToken = default) => throw new NotImplementedException(); |
| 45 | + public override Memory<byte> GetMemory(int sizeHint = 0) => throw new NotImplementedException(); |
| 46 | + public override Span<byte> GetSpan(int sizeHint = 0) => throw new NotImplementedException(); |
| 47 | +} |
0 commit comments