I have a file generated with Excel 2010 (too large to attach) that, upon loading then saving with EPPlus without any changes, results in a "Index out of range" exception and the file is not saved.
CompoundDocument::GetChunk() is responsible for the error. It uses a buffer of size 4098 to hold a copy of the chunked data to return to the caller. __There are no checks to ensure the buffer is large enough to hold all the tokens being copied!__ The chosen hard-coded size is insufficient for my file, as the code attempts to index beyond byte 4098. For my particular file, a buffer size of at least 4109 is required to avoid this crash, but this is not necessarily the limit.
To fix this specific problem, change line 240 of CompoundDocument.cs from:
```
byte[] buffer = new byte[4098];
```
to:
```
byte[] buffer = new byte[4109];
```
Ideally, the buffer size allocated would be determined by counting the required bytes in the source data.
Comments: I can replicate this issue with version 3.1.3 and 4.0 beta. I have a document with macros that is saved by Excel 2013. If then open it with EPPlus, save it, open it again and save it a second time, this problem will occur on the second save; therefore, it might be a bug in the CompessChunk function from the first time it was saved. It depends in the exact sequence of characters in one of the modules - it we edit that module - even just insert a comment, the bug no longer occurs. The code to replicate is below and I will email the file to epplusdocs@gmail.com ``` class Program { static void Main(string[] args) { const string infile="C:\\Src\\Test\\EppPlusTest\\Infile.xlsm"; const string outfile="C:\\Src\\Test\\EppPlusTest\\Outfile.xlsm"; ExcelPackage ep; using (FileStream fs = File.OpenRead(infile)) { ep = new ExcelPackage(fs); } using (FileStream fs = File.OpenWrite(outfile)) { ep.SaveAs(fs); } using (FileStream fs = File.OpenRead(outfile)) { ep = new ExcelPackage(fs); } using (FileStream fs = File.OpenWrite(outfile)) { ep.SaveAs(fs); } } } ```
CompoundDocument::GetChunk() is responsible for the error. It uses a buffer of size 4098 to hold a copy of the chunked data to return to the caller. __There are no checks to ensure the buffer is large enough to hold all the tokens being copied!__ The chosen hard-coded size is insufficient for my file, as the code attempts to index beyond byte 4098. For my particular file, a buffer size of at least 4109 is required to avoid this crash, but this is not necessarily the limit.
To fix this specific problem, change line 240 of CompoundDocument.cs from:
```
byte[] buffer = new byte[4098];
```
to:
```
byte[] buffer = new byte[4109];
```
Ideally, the buffer size allocated would be determined by counting the required bytes in the source data.
Comments: I can replicate this issue with version 3.1.3 and 4.0 beta. I have a document with macros that is saved by Excel 2013. If then open it with EPPlus, save it, open it again and save it a second time, this problem will occur on the second save; therefore, it might be a bug in the CompessChunk function from the first time it was saved. It depends in the exact sequence of characters in one of the modules - it we edit that module - even just insert a comment, the bug no longer occurs. The code to replicate is below and I will email the file to epplusdocs@gmail.com ``` class Program { static void Main(string[] args) { const string infile="C:\\Src\\Test\\EppPlusTest\\Infile.xlsm"; const string outfile="C:\\Src\\Test\\EppPlusTest\\Outfile.xlsm"; ExcelPackage ep; using (FileStream fs = File.OpenRead(infile)) { ep = new ExcelPackage(fs); } using (FileStream fs = File.OpenWrite(outfile)) { ep.SaveAs(fs); } using (FileStream fs = File.OpenRead(outfile)) { ep = new ExcelPackage(fs); } using (FileStream fs = File.OpenWrite(outfile)) { ep.SaveAs(fs); } } } ```