diff --git a/src/documentation/content/xdocs/changes.xml b/src/documentation/content/xdocs/changes.xml
index 721f2559c2..8776750d61 100644
--- a/src/documentation/content/xdocs/changes.xml
+++ b/src/documentation/content/xdocs/changes.xml
@@ -67,6 +67,7 @@
Created a common interface for handling Excel files, irrespective of if they are .xls or .xlsx
+ 45777 - Throw an exception if HSSF Footer or Header is attemped to be set too long, rather than having it break during writing out
45844 - Addtional diagnostics for HSLF SlideShowRecordDumper
45829 - HSSFPicture.getImageDimension() failed when DPI of image is zero
45815 - Bit mask values in StyleTextPropAtom were not preserved across read-write
diff --git a/src/documentation/content/xdocs/status.xml b/src/documentation/content/xdocs/status.xml
index b93115bd6b..2eaea06fac 100644
--- a/src/documentation/content/xdocs/status.xml
+++ b/src/documentation/content/xdocs/status.xml
@@ -64,6 +64,7 @@
Created a common interface for handling Excel files, irrespective of if they are .xls or .xlsx
+ 45777 - Throw an exception if HSSF Footer or Header is attemped to be set too long, rather than having it break during writing out
45844 - Addtional diagnostics for HSLF SlideShowRecordDumper
45829 - HSSFPicture.getImageDimension() failed when DPI of image is zero
45815 - Bit mask values in StyleTextPropAtom were not preserved across read-write
diff --git a/src/java/org/apache/poi/hssf/record/FooterRecord.java b/src/java/org/apache/poi/hssf/record/FooterRecord.java
index 6a1aa8624b..6ccc082801 100644
--- a/src/java/org/apache/poi/hssf/record/FooterRecord.java
+++ b/src/java/org/apache/poi/hssf/record/FooterRecord.java
@@ -118,6 +118,18 @@ public class FooterRecord
field_4_footer = footer;
field_3_unicode_flag =
(byte) (StringUtil.hasMultibyte(field_4_footer) ? 1 : 0);
+ // Check it'll fit into the space in the record
+
+ if(field_4_footer == null) return;
+ if(field_3_unicode_flag == 1) {
+ if(field_4_footer.length() > 127) {
+ throw new IllegalArgumentException("Footer string too long (limit is 127 for unicode strings)");
+ }
+ } else {
+ if(field_4_footer.length() > 255) {
+ throw new IllegalArgumentException("Footer string too long (limit is 255 for non-unicode strings)");
+ }
+ }
}
/**
diff --git a/src/java/org/apache/poi/hssf/record/HeaderRecord.java b/src/java/org/apache/poi/hssf/record/HeaderRecord.java
index cd58437028..e8190364a8 100644
--- a/src/java/org/apache/poi/hssf/record/HeaderRecord.java
+++ b/src/java/org/apache/poi/hssf/record/HeaderRecord.java
@@ -117,6 +117,18 @@ public class HeaderRecord
field_4_header = header;
field_3_unicode_flag =
(byte) (StringUtil.hasMultibyte(field_4_header) ? 1 : 0);
+
+ // Check it'll fit into the space in the record
+ if(field_4_header == null) return;
+ if(field_3_unicode_flag == 1) {
+ if(field_4_header.length() > 127) {
+ throw new IllegalArgumentException("Header string too long (limit is 127 for unicode strings)");
+ }
+ } else {
+ if(field_4_header.length() > 255) {
+ throw new IllegalArgumentException("Header string too long (limit is 255 for non-unicode strings)");
+ }
+ }
}
/**
diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFFooter.java b/src/java/org/apache/poi/hssf/usermodel/HSSFFooter.java
index 4b84dd2f1a..422af62c7f 100644
--- a/src/java/org/apache/poi/hssf/usermodel/HSSFFooter.java
+++ b/src/java/org/apache/poi/hssf/usermodel/HSSFFooter.java
@@ -71,6 +71,11 @@ public class HSSFFooter extends HeaderFooter implements Footer {
right = newRight;
createFooterString();
}
+
+ protected String getRawFooter() {
+ return footerRecord.getFooter();
+ }
+
/**
* Creates the complete footer string based on the left, center, and middle
diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFHeader.java b/src/java/org/apache/poi/hssf/usermodel/HSSFHeader.java
index 76bfdbd86b..c5247525a1 100644
--- a/src/java/org/apache/poi/hssf/usermodel/HSSFHeader.java
+++ b/src/java/org/apache/poi/hssf/usermodel/HSSFHeader.java
@@ -78,6 +78,10 @@ public class HSSFHeader extends HeaderFooter implements Header {
right = newRight;
createHeaderString();
}
+
+ protected String getRawHeader() {
+ return headerRecord.getHeader();
+ }
/**
* Creates the complete header string based on the left, center, and middle
diff --git a/src/testcases/org/apache/poi/hssf/usermodel/TestBugs.java b/src/testcases/org/apache/poi/hssf/usermodel/TestBugs.java
index e62795f56a..378b993d09 100644
--- a/src/testcases/org/apache/poi/hssf/usermodel/TestBugs.java
+++ b/src/testcases/org/apache/poi/hssf/usermodel/TestBugs.java
@@ -1416,4 +1416,64 @@ public final class TestBugs extends TestCase {
assertFalse(nwb.isSheetHidden(2));
assertTrue(nwb.isSheetVeryHidden(2));
}
+
+ /**
+ * header / footer text too long
+ */
+ public void test45777() {
+ HSSFWorkbook wb = new HSSFWorkbook();
+ HSSFSheet s = wb.createSheet();
+
+ String s248 = "";
+ for(int i=0; i<248; i++) {
+ s248 += "x";
+ }
+ String s249 = s248 + "1";
+ String s250 = s248 + "12";
+ String s251 = s248 + "123";
+ assertEquals(248, s248.length());
+ assertEquals(249, s249.length());
+ assertEquals(250, s250.length());
+ assertEquals(251, s251.length());
+
+
+ // Try on headers
+ s.getHeader().setCenter(s248);
+ assertEquals(254, s.getHeader().getRawHeader().length());
+ writeOutAndReadBack(wb);
+
+ s.getHeader().setCenter(s249);
+ assertEquals(255, s.getHeader().getRawHeader().length());
+ writeOutAndReadBack(wb);
+
+ try {
+ s.getHeader().setCenter(s250); // 256
+ fail();
+ } catch(IllegalArgumentException e) {}
+
+ try {
+ s.getHeader().setCenter(s251); // 257
+ fail();
+ } catch(IllegalArgumentException e) {}
+
+
+ // Now try on footers
+ s.getFooter().setCenter(s248);
+ assertEquals(254, s.getFooter().getRawFooter().length());
+ writeOutAndReadBack(wb);
+
+ s.getFooter().setCenter(s249);
+ assertEquals(255, s.getFooter().getRawFooter().length());
+ writeOutAndReadBack(wb);
+
+ try {
+ s.getFooter().setCenter(s250); // 256
+ fail();
+ } catch(IllegalArgumentException e) {}
+
+ try {
+ s.getFooter().setCenter(s251); // 257
+ fail();
+ } catch(IllegalArgumentException e) {}
+ }
}