nissl-lab / npoi

a .NET library that can read/write Office formats without Microsoft Office installed. No COM+, no interop.
Apache License 2.0
5.65k stars 1.42k forks source link

fixed AutoFilter record size with multibyte strings. #1367

Open htoyama opened 2 months ago

htoyama commented 2 months ago

NPOI Version

2.7.0

File Type

Upload the Excel File

Please attach your original Excel File to help us reproduce the issue

Reproduce Steps

Issue Description

CheckPosition throw exception. NPOI.Util.RuntimeException: 'Buffer overrun i=4;endIndex=125635;writeIndex=125633'

diff --git a/main/HSSF/Record/AutoFilter/AutoFilterRecord.cs b/main/HSSF/Record/AutoFilter/AutoFilterRecord.cs
index 227a4b1e..c43b9ca6 100644
--- a/main/HSSF/Record/AutoFilter/AutoFilterRecord.cs
+++ b/main/HSSF/Record/AutoFilter/AutoFilterRecord.cs
@@ -130,10 +130,25 @@ protected override int DataSize
             {
                 int recSize = 2 + 2 + 10 + 10;
                 if (field_3_doper1.LengthOfString > 0)
-                    recSize += 1 + field_5_rgch1.Length;
+                    if (rgch1_multibyte)
+                    {
+                        recSize += 1 + (field_5_rgch1.Length * 2); // UTF16LE
+                    }
+                    else
+                    {
+                        recSize += 1 + field_5_rgch1.Length;
+                    }

                 if(field_4_doper2.LengthOfString > 0)
-                    recSize += 1+ field_6_rgch2.Length;
+                    if (rgch2_multibyte)
+                    {
+                        recSize += 1+ (field_6_rgch2.Length * 2); // UTF16LE
+
+                    }
+                    else
+                    {
+                        recSize += 1+ field_6_rgch2.Length;
+                    }
                 return recSize;
             }
         }
htoyama commented 2 months ago

Book1.xls New serialize occur exception. Workbook.CloneSheet("Sheet1") Workbook.Write(New_File)