ChaoMaWisetech opened a new issue, #1158:
URL: https://github.com/apache/lucenenet/issues/1158

   ### Is there an existing issue for this?
   
   - [x] I have searched the existing issues
   
   ### Describe the bug
   
   The readCount might be negative in [ IndexInputStream.Read ] 
(https://github.com/apache/lucenenet/blob/master/src/Lucene.Net.Replicator/IndexInputInputStream.cs#L66C1-L72C10)
 and throw exception
   
   - Message :Non-negative number required. Parameter name: length
   - StackTrace: 
      at J2N.IO.MemoryMappedFiles.MemoryMappedViewByteBuffer.Get(Byte[] 
destination, Int32 offset, Int32 length), 
      at Lucene.Net.Replicator.IndexInputStream.Read(Byte[] buffer, Int32 
offset, Int32 count), 
   
   ```
   public override int Read(byte[] buffer, int offset, int count)
   {
        int remaining = (int) (input.Length - input.Position); // remaining 
might be a negative number due to (int) longNum
        int readCount = Math.Min(remaining, count); //  readCount might be a 
negative number
        input.ReadBytes(buffer, offset, readCount);
        return readCount;
   }
   ```
   
   a possible fix could be 
   ```
   public override int Read(byte[] buffer, int offset, int count)
   {
        long remaining = input.Length - input.Position; 
        int readCount = (int)Math.Min(remaining, count);
        input.ReadBytes(buffer, offset, readCount);
        return readCount;
   }
   ```
   
   
   ### Expected Behavior
   
   _No response_
   
   ### Steps To Reproduce
   
   ```
   [Test]
   public void IndexInputStream_ReadIssueWhenInputLengthGreaterThanIntMax()
   {
        var mockIndexInput = new Mock<IndexInput>(MockBehavior.Strict, "abc");
        mockIndexInput.Setup(x => x.Length).Returns(long.MaxValue).Verifiable();
        mockIndexInput.Setup(x => x.Position).Returns(0).Verifiable();
        mockIndexInput.Setup(x => x.ReadBytes(It.IsAny<byte[]>(), 
It.IsAny<int>(), It.Is<int>(v => v < 0)))
                .Throws(new ArgumentException("length cannot be negative"))
                .Verifiable();
   
        var indexInputStream = new IndexInputStream(mockIndexInput.Object);
   
        Assert.That(() => indexInputStream.Read(new byte[1], 0, 1), 
Throws.InstanceOf<ArgumentException>());
        mockIndexInput.VerifyAll();
   }
   ```
   
   ### Exceptions (if any)
   
   _No response_
   
   ### Lucene.NET Version
   
   _No response_
   
   ### .NET Version
   
   _No response_
   
   ### Operating System
   
   _No response_
   
   ### Anything else?
   
   _No response_


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscr...@lucenenet.apache.org.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to