This is an automated email from the ASF dual-hosted git repository. nightowl888 pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/lucenenet.git
commit 3979c4ab3b05882595d796dc0fb42bcdd1409cf2 Author: Shad Storhaug <[email protected]> AuthorDate: Sun May 14 23:01:43 2023 +0700 Lucene.Net.Store.BufferedIndexOutput: Allow double-dispose calls and guard against usage after Dispose(). See #265. --- src/Lucene.Net/Store/BufferedIndexOutput.cs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/Lucene.Net/Store/BufferedIndexOutput.cs b/src/Lucene.Net/Store/BufferedIndexOutput.cs index 6867c0a40..6be6f077f 100644 --- a/src/Lucene.Net/Store/BufferedIndexOutput.cs +++ b/src/Lucene.Net/Store/BufferedIndexOutput.cs @@ -1,6 +1,7 @@ using Lucene.Net.Support; using System; using System.Runtime.CompilerServices; +using System.Threading; namespace Lucene.Net.Store { @@ -34,6 +35,7 @@ namespace Lucene.Net.Store private long bufferStart = 0; // position in file of buffer private int bufferPosition = 0; // position in buffer private readonly CRC32 crc; + private int disposed = 0; // LUCENENET specific - allow double-dispose /// <summary> /// Creates a new <see cref="BufferedIndexOutput"/> with the default buffer size @@ -161,6 +163,8 @@ namespace Lucene.Net.Store /// <inheritdoc/> protected override void Dispose(bool disposing) { + if (0 != Interlocked.CompareExchange(ref this.disposed, 1, 0)) return; // LUCENENET specific - allow double-dispose + if (disposing) { Flush(); @@ -172,6 +176,7 @@ namespace Lucene.Net.Store [Obsolete("(4.1) this method will be removed in Lucene 5.0")] public override void Seek(long pos) { + EnsureOpen(); // LUCENENET specific - ensure we can't be abused after dispose Flush(); bufferStart = pos; } @@ -187,9 +192,22 @@ namespace Lucene.Net.Store { get { + EnsureOpen(); // LUCENENET specific - ensure we can't be abused after dispose Flush(); return crc.Value; } } + + // LUCENENET specific - ensure we can't be abused after dispose + private bool IsOpen => Interlocked.CompareExchange(ref this.disposed, 0, 0) == 0 ? true : false; + + // LUCENENET specific - ensure we can't be abused after dispose + private void EnsureOpen() + { + if (!IsOpen) + { + throw AlreadyClosedException.Create(this.GetType().FullName, "this IndexOutput is disposed."); + } + } } } \ No newline at end of file
