Skip to content

Commit

Permalink
Lucene.Net.Store.BufferedIndexOutput: Allow double-dispose calls and …
Browse files Browse the repository at this point in the history
…guard against usage after Dispose(). See apache#265.
  • Loading branch information
NightOwl888 committed May 14, 2023
1 parent dd96404 commit 0321896
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/Lucene.Net/Store/BufferedIndexOutput.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Lucene.Net.Support;
using System;
using System.Runtime.CompilerServices;
using System.Threading;

namespace Lucene.Net.Store
{
Expand Down Expand Up @@ -34,6 +35,7 @@ public abstract class BufferedIndexOutput : IndexOutput
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
Expand Down Expand Up @@ -161,6 +163,8 @@ private void FlushBuffer(byte[] b, int len)
/// <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();
Expand All @@ -172,6 +176,7 @@ protected override void Dispose(bool disposing)
[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;
}
Expand All @@ -187,9 +192,22 @@ public override long Checksum
{
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.");
}
}
}
}

0 comments on commit 0321896

Please sign in to comment.