Browse Source

bug 61300 -- prevent really long (infinite?) loop on corrupt file

pull/1595/head
Antony Liu 4 weeks ago
parent
commit
39f8571501
  1. 8
      main/POIFS/FileSystem/NDocumentInputStream.cs
  2. 5
      main/POIFS/FileSystem/ODocumentInputStream.cs
  3. 4
      main/Util/IOUtils.cs
  4. 18
      testcases/main/HSSF/UserModel/TestBugs.cs
  5. BIN
      testcases/test-data/spreadsheet/61300.xls

8
main/POIFS/FileSystem/NDocumentInputStream.cs

@ -69,6 +69,10 @@ namespace NPOI.POIFS.FileSystem
_marked_offset_count = 0; _marked_offset_count = 0;
_document_size = document.Size; _document_size = document.Size;
_closed = false; _closed = false;
if (_document_size < 0)
{
// throw new RecordFormatException("document_size cannot be < 0");
}
DocumentProperty property = (DocumentProperty)doc.Property; DocumentProperty property = (DocumentProperty)doc.Property;
_document = new NPOIFSDocument( _document = new NPOIFSDocument(
@ -277,6 +281,10 @@ namespace NPOI.POIFS.FileSystem
public override void ReadFully(byte[] buf, int off, int len) public override void ReadFully(byte[] buf, int off, int len)
{ {
if (len < 0)
{
throw new RuntimeException("Can't read negative number of bytes");
}
CheckAvaliable(len); CheckAvaliable(len);
int read = 0; int read = 0;

5
main/POIFS/FileSystem/ODocumentInputStream.cs

@ -19,6 +19,7 @@
using System; using System;
using NPOI.POIFS.Storage; using NPOI.POIFS.Storage;
using System.IO; using System.IO;
using NPOI.Util;
namespace NPOI.POIFS.FileSystem namespace NPOI.POIFS.FileSystem
{ {
@ -71,6 +72,10 @@ namespace NPOI.POIFS.FileSystem
_current_offset = 0; _current_offset = 0;
_marked_offset = 0; _marked_offset = 0;
_document_size = document.Size; _document_size = document.Size;
if (_document_size < 0)
{
throw new RecordFormatException("document_size cannot be < 0");
}
_closed = false; _closed = false;
_document = documentNode.Document; _document = documentNode.Document;
_currentBlock = GetDataInputBlock(0); _currentBlock = GetDataInputBlock(0);

4
main/Util/IOUtils.cs

@ -373,6 +373,10 @@ namespace NPOI.Util
int count; int count;
while ((count = inp.Read(buff, 0, buff.Length)) >0) while ((count = inp.Read(buff, 0, buff.Length)) >0)
{ {
if (count < -1)
{
throw new RecordFormatException("Can't have read < -1 bytes");
}
out1.Write(buff, 0, count); out1.Write(buff, 0, count);
} }
} }

18
testcases/main/HSSF/UserModel/TestBugs.cs

@ -46,6 +46,7 @@ namespace TestCases.HSSF.UserModel
using NPOI.HSSF; using NPOI.HSSF;
using System.Net; using System.Net;
using SixLabors.ImageSharp; using SixLabors.ImageSharp;
using NPOI.HPSF;
/** /**
* Testcases for bugs entered in bugzilla * Testcases for bugs entered in bugzilla
@ -2369,8 +2370,8 @@ namespace TestCases.HSSF.UserModel
ClassicAssert.AreEqual(3, wb.NumberOfSheets); ClassicAssert.AreEqual(3, wb.NumberOfSheets);
// Find the SST record // Find the SST record
UnicodeString withExt = wb.Workbook.GetSSTString(0);
UnicodeString withoutExt = wb.Workbook.GetSSTString(31);
NPOI.HSSF.Record.UnicodeString withExt = wb.Workbook.GetSSTString(0);
NPOI.HSSF.Record.UnicodeString withoutExt = wb.Workbook.GetSSTString(31);
ClassicAssert.AreEqual("O:Alloc:Qty", withExt.String); ClassicAssert.AreEqual("O:Alloc:Qty", withExt.String);
ClassicAssert.IsTrue((withExt.OptionFlags & 0x0004) == 0x0004); ClassicAssert.IsTrue((withExt.OptionFlags & 0x0004) == 0x0004);
@ -3510,6 +3511,19 @@ namespace TestCases.HSSF.UserModel
wb.Close(); wb.Close();
} }
[Test]
public void Test61300()
{
ClassicAssert.Throws<RuntimeException>(()=>{
NPOIFSFileSystem npoifs = new NPOIFSFileSystem(HSSFTestDataSamples.OpenSampleFileStream("61300.xls"));
DocumentEntry entry =
(DocumentEntry) npoifs.Root.GetEntry(SummaryInformation.DEFAULT_STREAM_NAME);
PropertySet properties =
new PropertySet(new DocumentInputStream(entry));
});
}
// follow https://svn.apache.org/viewvc?view=revision&revision=1896552 to write a unit test for this fix. // follow https://svn.apache.org/viewvc?view=revision&revision=1896552 to write a unit test for this fix.
[Test] [Test]
public void Test52447() public void Test52447()

BIN
testcases/test-data/spreadsheet/61300.xls

Loading…
Cancel
Save