]> source.dussan.org Git - poi.git/commitdiff
Fix bug with record indexes for HSSF Workbooks
authorMarius Volkhart <mariusvolkhart@apache.org>
Sat, 27 Feb 2021 00:43:56 +0000 (00:43 +0000)
committerMarius Volkhart <mariusvolkhart@apache.org>
Sat, 27 Feb 2021 00:43:56 +0000 (00:43 +0000)
We have encountered workbooks that do not have a TabIdRecord (see 55982.xls). However, the WorkbookRecordList#updateRecordPos() method would still increment the position of the TabIdRecord for such workbooks. Changing the default position of the record from 0 to -1 indicates that the record position has now been set.

This bug was discovered while adding support for editing pictures in HSSF documents.

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1886963 13f79535-47bb-0310-9956-ffa450edef68

src/java/org/apache/poi/hssf/model/InternalWorkbook.java
src/java/org/apache/poi/hssf/model/WorkbookRecordList.java
src/testcases/org/apache/poi/hssf/model/TestWorkbookRecordList.java [new file with mode: 0644]

index 58f86ef36c3158b390b976e784e16b1b692a5fa8..b6d60ec4716ce3f572748492fd7254f9737f83a9 100644 (file)
@@ -776,7 +776,6 @@ public final class InternalWorkbook {
      * make the tabid record look like the current situation.
      */
     private void fixTabIdRecord() {
-        Record rec = records.get(records.getTabpos());
 
         // see bug 55982, quite a number of documents do not have a TabIdRecord and
         // thus there is no way to do the fixup here,
@@ -785,6 +784,8 @@ public final class InternalWorkbook {
             return;
         }
 
+        Record rec = records.get(records.getTabpos());
+
         TabIdRecord tir = ( TabIdRecord ) rec;
         short[]     tia = new short[ boundsheets.size() ];
 
index c86a8655989d72f6f8cbff12f12680e113927102..2f97ed016345442e1cc6da696c990592751aed74 100644 (file)
@@ -30,7 +30,7 @@ public final class WorkbookRecordList {
     /** holds the position of the last bound sheet */
        private int bspos;
     /** holds the position of the tabid record */
-       private int tabpos;
+       private int tabpos = -1;
     /** hold the position of the last font record */
        private int fontpos;
     /** hold the position of the last extended font record */
diff --git a/src/testcases/org/apache/poi/hssf/model/TestWorkbookRecordList.java b/src/testcases/org/apache/poi/hssf/model/TestWorkbookRecordList.java
new file mode 100644 (file)
index 0000000..53fd549
--- /dev/null
@@ -0,0 +1,26 @@
+package org.apache.poi.hssf.model;
+
+import org.apache.poi.hssf.record.chart.ChartRecord;
+import org.apache.poi.hssf.usermodel.HSSFWorkbook;
+import org.junit.jupiter.api.Test;
+
+import java.io.IOException;
+
+import static org.apache.poi.hssf.HSSFTestDataSamples.openSampleWorkbook;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+public class TestWorkbookRecordList {
+
+    @Test
+    public void tabposIsOnlyUpdatedIfWorkbookHasTabIdRecord() throws IOException {
+        try (HSSFWorkbook wb = openSampleWorkbook("55982.xls")) {
+            WorkbookRecordList records = wb.getInternalWorkbook().getWorkbookRecordList();
+            assertEquals(-1, records.getTabpos());
+
+            // Add an arbitrary record to the front of the list
+            records.add(0, new ChartRecord());
+
+            assertEquals(-1, records.getTabpos());
+        }
+    }
+}