From e9c47b2dea45097a2761ebf2b3695adc456e8f26 Mon Sep 17 00:00:00 2001 From: Shad Storhaug Date: Sun, 14 May 2023 23:04:14 +0700 Subject: [PATCH] Lucene.Net.Store.OutputStreamDataOutput: Allow double-dispose calls and guard against usage after Dispose(). See #265. --- src/Lucene.Net/Store/OutputStreamDataOutput.cs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/Lucene.Net/Store/OutputStreamDataOutput.cs b/src/Lucene.Net/Store/OutputStreamDataOutput.cs index f635a2b366..7a96de6a83 100644 --- a/src/Lucene.Net/Store/OutputStreamDataOutput.cs +++ b/src/Lucene.Net/Store/OutputStreamDataOutput.cs @@ -1,5 +1,6 @@ using System; using System.IO; +using System.Threading; namespace Lucene.Net.Store { @@ -26,6 +27,7 @@ namespace Lucene.Net.Store public class OutputStreamDataOutput : DataOutput, IDisposable { private readonly Stream _os; + private int disposed = 0; // LUCENENET specific - allow double-dispose public OutputStreamDataOutput(Stream os) { @@ -60,6 +62,8 @@ public void Dispose() // LUCENENET specific - implemented proper dispose pattern protected virtual void Dispose(bool disposing) { + if (0 != Interlocked.CompareExchange(ref this.disposed, 1, 0)) return; // LUCENENET specific - allow double-dispose + if (disposing) { _os.Dispose();